我可以使用 Puppet 运行 shell 内置命令吗?

qua*_*nta 5 shell puppet command execution

我想要~/.bashrc将是source每当改变其内容。我用这样的东西创建了一个 bashrc 类:

file { "/root/.bashrc":
    ensure  => present,
    owner   => root,
    group   => root,
    mode    => 0644,
    source  => "puppet:///bashrc/root/.bashrc"
}

exec { "root_bashrc":
    command     => "source /root/.bashrc",
    subscribe   => File["/root/.bashrc"],
}
Run Code Online (Sandbox Code Playgroud)

但如您所知,它source是一个 shell 内置命令,因此在运行代理时出现以下错误:

# puppet agent --no-daemonize --verbose
notice: Starting Puppet client version 2.7.1
info: Caching catalog for svr051-4170
info: Applying configuration version '1311563901'
err: /Stage[main]/Bashrc/Exec[root_bashrc]/returns: change from notrun to 0 failed: Could not find command 'source'
notice: Finished catalog run in 2.28 seconds
notice: Caught INT; calling stop
Run Code Online (Sandbox Code Playgroud)

有什么解决方法可以做到这一点吗?

wom*_*ble 7

在 Puppet中重新source创建 new是没有意义的.bashrc,因为它将在子 shell 中运行并且更改不会传播到您当前的 shell(我假设,这就是您正在尝试做的)。你不能做(我认为)你想做的事。


小智 5

您还可以经常在命令前加上true &&或 使用provider => shell

请参阅以进行更多讨论。

这应该是:

file { "/root/.bashrc":
    ensure  => present,
    owner   => root,
    group   => root,
    mode    => 0644,
    source  => "puppet:///bashrc/root/.bashrc" }

exec { "root_bashrc":
    command     => "source /root/.bashrc",
    provider => shell,
    subscribe   => File["/root/.bashrc"], 
}
Run Code Online (Sandbox Code Playgroud)

  • `provider` => 'shell'` 看起来是迄今为止最好的答案。但请记住,“sh”不是“bash”,因此我们应该使用“.”而不是“source”。 (3认同)