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
}