-1

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 :)

Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
  • Two things to it: 1. To access variable inside the bash code, its should be without quotes. e.g. #{node["usersinto"]["users"]}. Also its good practice to use single quotes, if you are not doing String interpolation. 2. Until you share the script, its difficult to guess on errors. – Mrigesh Priyadarshi Aug 18 '16 at 03:43

3 Answers3

0

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}.

coderanger
  • 52,400
  • 4
  • 52
  • 75
0

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
justMiles
  • 553
  • 3
  • 11
0

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.

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
}
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185