Chef - how to use a list of attributes in a script

佐手、 提交于 2019-11-28 14:04:00

问题


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

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??

Thanks in advance :)


回答1:


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} 

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



回答2:


The way you have it, the value of node["usersinto"]["users"] is an array with one element (which is a string). This probably isn't what you wanted, either make it a single string or an array of multiple strings. In either case, as written you would want #{node["usersinto"]["users"].first}.




回答3:


If you're new to chef is then you should look into using some of the community cookbooks to solve common issues like user management. For example the "users" cookbook is ideal for managing user accounts

  • https://supermarket.chef.io/cookbooks/users

The user information is specified in data bags, rather than attributes.

Example

A sample cookbook can be generated using the chef command

chef generate cookbook demo

Creates a cookbook called "demo" which can be expanded as follows:

├── Berksfile
├── Berksfile.lock
├── .kitchen.yml       <-- Test kitchen file
├── metadata.rb
├── README.md
├── recipes
│   └── default.rb     <-- Recipe to be tested
└── test
    └── integration
        ├── data_bags            <-- Test data
        │   └── users
        │       ├── user1.json
        │       └── user2.json
        ├── default
        │   └── serverspec
        │       └── default_spec.rb   <-- Test
        └── helpers
            └── serverspec
                └── spec_helper.rb

Test kitchen is a tool that is bundled with chefdk and can be used to test the cookbook logic

$ kitchen verify default-ubuntu-1404
-----> Starting Kitchen (v1.4.2)
..
..
..
       User "user1"
         should exist
         should belong to group "admins"
         should have uid 2001
         should have authorized key "ssh-rsa I AM A DUMMY KEY 1"

       User "user2"
         should exist
         should belong to group "admins"
         should have uid 2002
         should have authorized key "ssh-rsa I AM A DUMMY KEY 2"

       Finished in 0.12919 seconds (files took 0.31869 seconds to load)
       8 examples, 0 failures

       Finished verifying <default-ubuntu-1404> (0m8.42s).
-----> Kitchen is finished. (0m9.14s)

metadata.rb

The "users" cookbook is added as a dependency

name 'demo'
maintainer 'Mark O''Connor'
maintainer_email 'me@demo.com'
license 'all_rights'
description 'Installs/Configures demo'
long_description 'Installs/Configures demo'
version '0.1.0'

depends "users"

recipes/default.rb

#
# Cookbook Name:: demo
# Recipe:: default
#
# Copyright (c) 2016 The Authors, All Rights Reserved.

users_manage "admins"

test/integration/default/serverspec/default_spec.rb

require 'spec_helper'

describe user('user1') do
  it { should exist }
  it { should belong_to_group 'admins' }
  it { should have_uid 2001 }
  it { should have_authorized_key 'ssh-rsa I AM A DUMMY KEY 1' }
end

describe user('user2') do
  it { should exist }
  it { should belong_to_group 'admins' }
  it { should have_uid 2002 }
  it { should have_authorized_key 'ssh-rsa I AM A DUMMY KEY 2' }
end

test/integration/data_bags/users/user1.json

{
  "id": "user1",
  "ssh_keys": [
    "ssh-rsa I AM A DUMMY KEY 1"
  ],
  "groups": [
    "admins"
  ],
  "uid": 2001
}

test/integration/data_bags/users/user2.json

{
  "id": "user2",
  "ssh_keys": [
    "ssh-rsa I AM A DUMMY KEY 2"
  ],
  "groups": [
    "admins"
  ],
  "uid": 2002
}


来源:https://stackoverflow.com/questions/39008803/chef-how-to-use-a-list-of-attributes-in-a-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!