![我可以使用 Puppet 运行 shell 内置命令吗?](https://linux22.com/image/556311/%E6%88%91%E5%8F%AF%E4%BB%A5%E4%BD%BF%E7%94%A8%20Puppet%20%E8%BF%90%E8%A1%8C%20shell%20%E5%86%85%E7%BD%AE%E5%91%BD%E4%BB%A4%E5%90%97%EF%BC%9F.png)
我希望每次更改其内容时~/.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"],
}
但如您所知,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
有没有什么解决方法?
答案1
source
在 Puppet 中重新创建一个新命令是没有意义的.bashrc
,因为它将在子 shell 中运行,并且更改不会传播到您当前的 shell(我假设,这是您要尝试执行的操作)。您无法执行(我认为)您想执行的操作。
答案2
您还可以经常在命令前加上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"],
}
答案3
从技术上讲,您可以使用:
exec { "root_bashrc":
command => "bash -c 'source /root/.bashrc'",
subscribe => File["/root/.bashrc"],
refreshonly => true,
}
然而,@womble 已经指出,那样获取 .bashrc 是没有意义的;它只会影响在该命令中运行的 bash shell,而不会影响任何当前正在运行的 bash shell。
您可能可以设置PROMPT_COMMAND="source /root/.bashrc"
为每次在任何当前正在运行的交互式 shell 中显示提示时重新运行 .bashrc,但这似乎有点耗费资源。我从未尝试过,但我认为它会起作用。