Information Technology Reference
In-Depth Information
driver :
box : learningchef / centos65
box_url : learningchef / centos65
suites :
- name : default
run_list :
- recipe [ users : :default ]
attributes :
Let's write a recipe that queries the list of users in our users data bag and creates a local user
for each item. You can use the search() method to perform the data bag query, just like you
did for nodes in Chapter 12 . Plus, you can make use of the Chef user resource to create a
user based on the information contained in the data bag.
Enter in the code for recipes/default.rb that matches Example 13-4 .
Example 13-4. chefdk/chef-playground/cookbooks/users/recipes/default.rb
#
# Cookbook Name:: users
# Recipe:: default
#
# Copyright (C) 2014
#
#
#
search ( "users" , "*:*" ) . each ddo | user_data |
user user_data [ "id" ] ddo
comment user_data [ "comment" ]
uid user_data [ "uid" ]
gid user_data [ "gid" ]
home user_data [ "home" ]
shell user_data [ "shell" ]
end
end
end
end
We're using the each do construct similar to the code we wrote in Chapter 12 , this time iter-
ating through each item of the data bag and returning the data bag contents in user_data .
user_data is a hash containing the key-value pairs in the data bag item.
The user statement within the search() block is a Chef resource. The user resource creates
a local user on the node. It takes the following attributes:
Search WWH ::




Custom Search