Cookbook Recipe : 10
Creating users & group using databag , undefault recipe.
[root@chef-workstation chef-repo]# ls -lrt
total 4
-rw-r--r--. 1 root root 2284 May 5 20:30 README.md
drwxr-xr-x. 2 root root 24 May 6 09:21 roles
drwxr-xr-x. 10 root root 140 May 9 17:40 cookbooks
[root@chef-workstation chef-repo]#
[root@chef-workstation chef-repo]# mkdir data_bags
[root@chef-workstation chef-repo]# cd data_bags/
[root@chef-workstation data_bags]# mkdir users groups
[root@chef-workstation chef-repo]# cd ../..
[root@chef-workstation chef-repo]# knife data bag create users
Created data_bag[users]
[root@chef-workstation chef-repo]#
[root@chef-workstation chef-repo]# knife data bag create groups
Created data_bag[groups]
[root@chef-workstation chef-repo]#
[root@chef-workstation chef-repo]# cat data_bags/users/sai.json
{
"id":"sai",
"comment":"Test-user",
"uid":2002,
"gid":0,
"home":"/home/sai",
"shell":"/bin/bash"
}
[root@chef-workstation chef-repo]# cat data_bags/users/advaith.json
{
"id":"advaith",
"comment":"group-user",
"uid":2003,
"gid":0,
"home":"/home/advaith",
"shell":"/bin/bash"
}
[root@chef-workstation chef-repo]# cat data_bags/groups/group1.json
{
"id":"group1",
"gid":2005,
"members":["sai","advaith"]
}
[root@chef-workstation chef-repo]#
[root@chef-workstation chef-repo]# knife data bag from file users sai.json advaith.json
Updated data_bag_item[users::sai]
Updated data_bag_item[users::advaith]
[root@chef-workstation chef-repo]#
[root@chef-workstation chef-repo]# knife data bag from file groups group1.json
Updated data_bag_item[groups::group1]
[root@chef-workstation chef-repo]#
[root@chef-workstation cookbooks]# chef generate cookbook users_group
Generating cookbook users_group
- Ensuring correct cookbook content
- Committing cookbook files to git
Your cookbook is ready. Type `cd users_group` to enter it.
There are several commands you can run to get started locally developing and testing your cookbook.
Why not start by writing an InSpec test? Tests for the default recipe are stored at:
test/integration/default/default_test.rb
If you'd prefer to dive right in, the default recipe can be found at:
recipes/default.rb
[root@chef-workstation cookbooks]# cd users_group/recipes/
[root@chef-workstation recipes]# cat default.rb
#
# Cookbook:: users_group
# Recipe:: default
#
# Copyright:: 2022, The Authors, All Rights Reserved.
search("users","*:*").each do |user_data|
user user_data["id"] do
comment user_data["comment"]
uid user_data["uid"]
gid user_data["gid"]
home user_data["home"]
shell user_data["shell"]
end
end
Non-Default recipe:
[root@chef-workstation recipes]# cat group.rb
search("groups","*:*").each do |group_data|
group group_data["id"] do
gid group_data["gid"]
members group_data["members"]
end
end
[root@chef-workstation recipes]#
[root@chef-workstation recipes]# knife node run_list add Node-1 "recipe[users_group]"
Node-1:
run_list:
recipe[users]
recipe[users_group]
[root@chef-workstation recipes]#
Note: group non-default recipe is note added with the above command
[root@chef-workstation recipes]# knife cookbook upload users_group
Uploading users_group [0.1.0]
Uploaded 1 cookbook.
[root@chef-workstation recipes]#
Node1:
In this above result only user are created but not group, because it is not added to run list with the above command
To add non-default recipes , please use the below 2 methods:
Method1:
[root@chef-workstation recipes]# knife node run_list add Node-1 "recipe[users_group]","recipe[users_group::group]"
Node-1:
run_list:
recipe[users_group]
recipe[users_group::group]
[root@chef-workstation recipes]# knife node show Node-1
Node Name: Node-1
Environment: _default
FQDN: node1
IP: 192.168.2.133
Run List: recipe[users_group], recipe[users_group::group]
Roles:
Recipes: users_group, users_group::default
Platform: centos 7.9.2009
Tags:
[root@chef-workstation recipes]# knife cookbook upload users_group
Uploading users_group [0.1.0]
Uploaded 1 cookbook.
[root@chef-workstation recipes]#
Method2:
[root@chef-workstation recipes]# cat default.rb
#
# Cookbook:: users_group
# Recipe:: default
#
# Copyright:: 2022, The Authors, All Rights Reserved.
search("users","*:*").each do |user_data|
user user_data["id"] do
comment user_data["comment"]
uid user_data["uid"]
gid user_data["gid"]
home user_data["home"]
shell user_data["shell"]
end
end
include_recipe "users_group::group"
Node1:
Now group is also created
----------------------------End of Example10---------------------------------------
Comments
Post a Comment