Chef - 如何在脚本中使用属性列表 [英] Chef - how to use a list of attributes in a script
问题描述
我是 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 " ]
在配方中被调用:
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屋!