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[\"use
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
The user information is specified in data bags, rather than attributes.
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)
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"
#
# Cookbook Name:: demo
# Recipe:: default
#
# Copyright (c) 2016 The Authors, All Rights Reserved.
users_manage "admins"
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
{
"id": "user1",
"ssh_keys": [
"ssh-rsa I AM A DUMMY KEY 1"
],
"groups": [
"admins"
],
"uid": 2001
}
{
"id": "user2",
"ssh_keys": [
"ssh-rsa I AM A DUMMY KEY 2"
],
"groups": [
"admins"
],
"uid": 2002
}
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
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}
.