IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    chef使用shell的output做为Template的变量

    深夜的蚊子发表于 2016-12-12 08:08:25
    love 0

    本文主要介绍如何在chef中使用shell的output结果动态创建一个变量,从而在template中使用该变量

    这里主要用到了chef里的ruby_block资源

    看一个例子

    #tricky way to load this Chef::Mixin::ShellOut utilities
    Chef::Resource::RubyBlock.send(:include, Chef::Mixin::ShellOut)
    ruby_block "get output" do
        block do
            command = '/tmp/test.sh'
            command_out = shell_out(command)
            node.default['return_val'] = command_out.stdout
        end
        action :create
    end
    
    template '/tmp/new.sh' do
       source 'test.erb'
       owner 'root'
       group 'root'
       mode '0644'
       variables( lazy {{:config_var => node['return_val']}})
    end

    上面的代码执行结果如下

    * ruby_block[get output] action run
      - execute the ruby block reload client config
    * template[/tmp/new.sh] action create
      - create new file /tmp/new.sh
      - update content in file /tmp/new.sh from none to 2c9c1f
      --- /tmp/new.sh  2016-08-05 13:20:21.678786687 +0000
      +++ /tmp/.chef-new.sh20160805-12338-81wvgw   2016-08-05 13:20:21.678786687 +0000
      @@ -1 +1,2 @@
      +mrigesh
      - change mode from '' to '0644'
      - change owner from '' to 'root'
      - change group from '' to 'root'
      - restore selinux security context

     

    © 2016, 深夜的蚊子. 版权所有. 如转载,请注明:转载自 蚊子空间[http://www.wenzizone.cn]



沪ICP备19023445号-2号
友情链接