Bash 如何将Puppet exec配置为作为onlyif的一部分执行另一个命令

Bash 如何将Puppet exec配置为作为onlyif的一部分执行另一个命令,bash,puppet,centos7,hyperic,Bash,Puppet,Centos7,Hyperic,我正在编写一个Puppet模块来部署Hyperic,最后一部分是在服务未运行时执行hq-agent.sh start。为了确定服务是否正在运行,我可以执行hq-agent.sh status,其中包含文本“hq-agent正在运行” Puppet文档声明onlyf可以工作,但是如果正在运行,则需要返回退出代码1,如果未运行,则返回0;执行转换的适用bash(?)命令是什么 伪代码=>if('hq-agent.sh status'包含“running”)返回1;否则返回0 这听起来很像应该通过服务

我正在编写一个Puppet模块来部署Hyperic,最后一部分是在服务未运行时执行hq-agent.sh start。为了确定服务是否正在运行,我可以执行hq-agent.sh status,其中包含文本“hq-agent正在运行”

Puppet文档声明onlyf可以工作,但是如果正在运行,则需要返回退出代码1,如果未运行,则返回0;执行转换的适用bash(?)命令是什么


伪代码=>if('hq-agent.sh status'包含“running”)返回1;否则返回0

这听起来很像应该通过
服务
资源而不是
Exec
来建模的东西。这样做并不需要通过系统的常规服务控制子系统(initscripts、systemd等)来管理服务,尽管我当然会建议您进行安排,即使您必须自己编写适当的脚本或配置文件。但是,在本例中,
hq agent.sh
脚本的接口听起来与传统的initscript类似,如果不是完全相同的话。如果是这样,那么将其设置为普通的系统服务可能会非常容易。如果你做到了这一点,那么管理它就很容易了

service { 'hq-agent':
  ensure => 'running',
  enable => true,
}
但是,如果您只想使用临时脚本来管理服务,Puppet可以支持这一点。具体而言,具有
start
restart
status
stop
属性,您可以使用这些属性指定用于管理服务的任意命令。比如说,

service { 'hq-agent':
  ensure     => 'running',
  provider   => 'service',
  hasstatus  => false,
  hasrestart => false,
  status     => 'hq-agent.sh status',
  start      => 'hq-agent.sh start',
  stop       => 'hq-agent.sh stop',
  path       => '/path/to/hyperic/bin',
  # no 'enable' attribute specified
}
  status => 'hq-agent.sh status | grep -q running'
该特定示例根据
hq agent.sh
脚本与标准SysV initscript的表面相似性,对其退出代码进行了一些假设。具体来说,它假设它们符合。如果事实上它们没有,那么您需要测试脚本的输出,而不是它的退出代码,那么典型的方法是将输出导入
grep
。比如说,

service { 'hq-agent':
  ensure     => 'running',
  provider   => 'service',
  hasstatus  => false,
  hasrestart => false,
  status     => 'hq-agent.sh status',
  start      => 'hq-agent.sh start',
  stop       => 'hq-agent.sh stop',
  path       => '/path/to/hyperic/bin',
  # no 'enable' attribute specified
}
  status => 'hq-agent.sh status | grep -q running'

但是,请注意,您可能需要测试脚本的标准错误,而不是它的标准输出。

这听起来非常像应该通过
服务
资源,而不是
执行
来建模的东西。这样做并不需要通过系统的常规服务控制子系统(initscripts、systemd等)来管理服务,尽管我当然会建议您进行安排,即使您必须自己编写适当的脚本或配置文件。但是,在本例中,
hq agent.sh
脚本的接口听起来与传统的initscript类似,如果不是完全相同的话。如果是这样,那么将其设置为普通的系统服务可能会非常容易。如果你做到了这一点,那么管理它就很容易了

service { 'hq-agent':
  ensure => 'running',
  enable => true,
}
但是,如果您只想使用临时脚本来管理服务,Puppet可以支持这一点。具体而言,具有
start
restart
status
stop
属性,您可以使用这些属性指定用于管理服务的任意命令。比如说,

service { 'hq-agent':
  ensure     => 'running',
  provider   => 'service',
  hasstatus  => false,
  hasrestart => false,
  status     => 'hq-agent.sh status',
  start      => 'hq-agent.sh start',
  stop       => 'hq-agent.sh stop',
  path       => '/path/to/hyperic/bin',
  # no 'enable' attribute specified
}
  status => 'hq-agent.sh status | grep -q running'
该特定示例根据
hq agent.sh
脚本与标准SysV initscript的表面相似性,对其退出代码进行了一些假设。具体来说,它假设它们符合。如果事实上它们没有,那么您需要测试脚本的输出,而不是它的退出代码,那么典型的方法是将输出导入
grep
。比如说,

service { 'hq-agent':
  ensure     => 'running',
  provider   => 'service',
  hasstatus  => false,
  hasrestart => false,
  status     => 'hq-agent.sh status',
  start      => 'hq-agent.sh start',
  stop       => 'hq-agent.sh stop',
  path       => '/path/to/hyperic/bin',
  # no 'enable' attribute specified
}
  status => 'hq-agent.sh status | grep -q running'

但是,请注意,您可能需要测试脚本的标准错误,而不是它的标准输出。

太棒了,谢谢您-我会尝试一下,然后再报告。这看起来确实像是一个模式,应该是有效的。太棒了,谢谢你-我会尝试一下,然后再报告。这看起来确实是一种应该奏效的模式。