如何在Chef中循环浏览bash例程? [英] How can I loop through bash routine in Chef?

查看:78
本文介绍了如何在Chef中循环浏览bash例程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Chef中有一个bash脚本,该脚本通过NTP协议从运行NTP服务器的3个实例中获取时间。当前的代码是

I have a bash script in Chef that fetches the time through NTP protocol from 3 instances running NTP server. The code at present is

if not node.run_list.roles.include?("ntp_server")
  bash "ntpdate" do
    code <<-EOH
    /usr/sbin/ntpdate -s 10.204.255.15 10.204.251.41 10.204.251.21
    EOH
  end
end

这一直很好。但是,我应该自动执行任务,例如替换其中一个实例,不需要手动干预即可更新上面代码中的IP。

This has been working just fine. However, I am supposed to automate the task such as if one of the instances is replaced, there is no manual intervention required to update the IP in the code above.

到为此,我已经能够成功获取运行 ntp_server 角色的实例。

To achieve that, I have been successfully able to fetch the instances running the ntp_server role.

ntp_servers = search(:node, 'role:ntp_server')

这样做之后,我无法将这些IP添加到代码中上面显示的Chef的bash子例程中。

Having done that, I am unable to add those IP's to the bash subroutine in Chef shown above in the code.

有人可以让我知道我应该如何实现吗?

Can someone let me know how am I supposed to achieve that?

推荐答案


  1. 您不应使用bash块并在每次厨师运行时调用ntpdate。 ntpd应该确保时钟保持同步,Chef为此已食谱

  2. 您可以将IP地址移动到节点并使用join in code。

  1. You shouldn't use bash block and call ntpdate with each chef run. ntpd should take care of clock being in sync, Chef has cookbook for this.
  2. You could move IP addresses to the node and use join in code.

...
code "/usr/sbin/ntpdate -s #{node["ntp_ipaddresses"].join(" ")}"
...


  • 请使用ntp食谱。

  • Please, use ntp cookbook.

    这篇关于如何在Chef中循环浏览bash例程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆