未执行变量的 Puppet exec 命令

kai*_*ser 1 git puppet vagrant

我有一个非常简单的 Puppet(子)模块,它应该使用 Git 从远程位置克隆存储库:

class wppuppet::git(
  $location = '/var/www/wp'
) {

  file { $location:
    ensure => 'directory',
    mode   => '0755',
  }

  exec { 'git-wp':
    command => 'git clone https://github.com/WordPress/WordPress ${location}',
    require => Package['git'],
  }

  Package['git']
  -> File[ $location ]
  -> Exec['git-wp']
}
Run Code Online (Sandbox Code Playgroud)

由于某种原因,它经常失败并出现以下错误:

Error: git clone https://github.com/WordPress/WordPress ${location} returned 128 instead of one of [0]
Error: /Stage[main]/Wppuppet::Git/Exec[git-wp]/returns: change from notrun to 0 failed: 
git clone https://github.com/WordPress/WordPress ${location} returned 128 instead one of [0]
Run Code Online (Sandbox Code Playgroud)

我用${location}和尝试过$location,但结果保持不变。

lar*_*sks 9

您的第一个问题是您的command参数用单引号 ( ')括起来,这会抑制变量扩展。如果你有:

$location = "/path/to/target"
Run Code Online (Sandbox Code Playgroud)

然后:

file { '$location':
  ensure => directory,
}
Run Code Online (Sandbox Code Playgroud)

将尝试创建一个名为“ $location”的目录,而这个:

file { "$location":
  ensure => directory,
}
Run Code Online (Sandbox Code Playgroud)

实际上会尝试创建/path/to/target.

考虑到这一点,您的exec资源可能看起来像:

exec { 'git-wp':
  command => "git clone https://github.com/WordPress/WordPress ${location}",
  require => Package['git'],
}
Run Code Online (Sandbox Code Playgroud)

此外,没有必要预先创建目标目录;git会为你做这件事。

您可以运行 puppet with--debug以查看git.