Chef-如何在脚本中使用属性列表 [英] Chef - how to use a list of attributes in a script

查看:64
本文介绍了Chef-如何在脚本中使用属性列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Chef的新手,我想执行一个脚本以在系统中添加用户. 我在厨师那里有一本名为usersinto和属性的食谱:

I'm newbie in Chef and I want to execute a script to add users in my system. I have a cookbook in chef called usersinto and attributes:

node.default["usersinto"]["users"] = [ " user1 user2 user3 .... userN " ]

,并在配方中通过以下方式调用:

and is called in recipe by:

bash "launch-add" do
    code <<-EOH
        sh /home/user/addusers.sh "#{node["usersinto"]["users"]}"
    EOH
end

我会尝试很多事情,如果我在属性[[]中使用,脚本会捕获"["作为参数$ 1,如果我不使用"[]",脚本只会捕获第一个用户. 我该怎么办??

I'll try a lot of things, if I use in attributes "[", the script catches "[" as argument $1, if I don't use "[ ]" the script only catches the first user. How could I do this??

先谢谢您了:)

推荐答案

似乎您的脚本接受一个参数.我无法确定该参数是单个用户还是单个用户字符串.如果仅接受一个用户,则需要将node["usersinto"]["users"]属性设置为数组(无论如何,它看起来都应该是数组).

It seems your script accepts one argument. What I can't tell if that argument is a single user in or a single string of users. If it only accepts one user, you'd need to make your node["usersinto"]["users"] attribute an array (this looks like it should be an array in any case).

node.default["usersinto"]["users"] = %w{user1 user2 user3} 

一旦有了数组,就对其进行迭代,为每个用户执行bash脚本.

Once you have your array, iterate over it executing the bash script for each user.

node["usersinto"]["users"].each do |user|
    bash "launch-add" do 
       code <<-EOH 
         sh /home/user/addusers.sh #{user}
       EOH 
    end
end

如果您的脚本接受多个参数(每个参数是一个不同的用户)

In the case that your script accepts multiple arguments (each argument being a different user)

bash "launch-add" do 
   code <<-EOH 
     sh /home/user/addusers.sh #{node['usersinto']['users'].join(' ')}
   EOH 
end

这篇关于Chef-如何在脚本中使用属性列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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