Managing server resource directly using chef-client

Managing server resource directly using chef-client

chef_manage_server_resources_chefclient

Here in this article we will try to make sure to configure the server to use a specified /etc/hosts file as per the recipe.

Test Environment

Fedora
Chef DK

What is Chef Resource

Chef resource describes one part of the system such as file or a package.

What is Chef Recipe

Chef recipe is a file that groups related resources and it acts like a policy to configure the system.

Procedure

Step1: Validate the Chef Client version

[user@host-1 chefspace]$ chef-client --version
Chef: 12.12.15

Also verify the current hosts file on the server as shown below.

[root@host-1 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

Step2: Create a Chef Recipe

Here is the chef recipe we would like to apply to the server to ensure that the hosts file is updated as per the required state.

[user@host-1 chefspace]$ cat host-1_hostfile.rb
file '/etc/hosts' do
content '127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.1 host-1'
end

Step3: Run the chef-client tool to locally apply the recipe

Now, we will try to run our recipe in local mode to apply the hosts file update locally.

[user@host-1 chefspace]$ chef-client --local-mode host-1_hostfile.rb
[2017-06-02T00:12:16-07:00] WARN: No config file found or specified on command line, using command line options.
[2017-06-02T00:12:16-07:00] WARN: No cookbooks directory found at or above current directory. Assuming /home/user/chefspace.
Starting Chef Client, version 12.12.15
resolving cookbooks for run list: []
Synchronizing Cookbooks:
Installing Cookbook Gems:
Compiling Cookbooks...
[2017-06-02T00:12:31-07:00] WARN: Node host-1.user.example has an empty run list.
Converging 1 resources
Recipe: @recipe_files::/home/user/chefspace/host-1_hostfile.rb
* file[/etc/hosts] action create[2017-06-02T00:12:31-07:00] WARN: Could not set uid = 0 on /tmp/.chef-hosts20170602-8873-1itkika, file modes not preserved
[2017-06-02T00:12:31-07:00] WARN: Could not set gid = 0 on /tmp/.chef-hosts20170602-8873-1itkika, file modes not preserved
================================================================================
Error executing action `create` on resource 'file[/etc/hosts]'
================================================================================
Errno::EACCES

NOTE: The above action failed to update the /etc/hosts file as it requires root access to update the file.

Now Running the same command with sudo to provide root priviliages.

[user@host-1 chefspace]$ sudo chef-client --local-mode host-1_hostfile.rb
[2017-06-02T00:21:55-07:00] WARN: No config file found or specified on command line, using command line options.
[2017-06-02T00:21:55-07:00] WARN: No cookbooks directory found at or above current directory. Assuming /home/user/chefspace.
Starting Chef Client, version 12.12.15
resolving cookbooks for run list: []
Synchronizing Cookbooks:
Installing Cookbook Gems:
Compiling Cookbooks...
[2017-06-02T00:21:59-07:00] WARN: Node host-1 has an empty run list.
Converging 1 resources
Recipe: @recipe_files::/home/user/chefspace/host-1_hostfile.rb
* file[/etc/hosts] action create
- update content in file /etc/hosts from 3bd7c4 to fd61bc
--- /etc/hosts 2017-06-02 00:20:43.767630790 -0700
+++ /etc/.chef-hosts20170602-9965-1qpxiwc 2017-06-02 00:21:59.043555799 -0700
@@ -1,4 +1,3 @@
-
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.1 host-1
- restore selinux security context

Running handlers:
Running handlers complete
Chef Client finished, 1/1 resources updated in 03 seconds

[user@host-1 chefspace]$ cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.1 host-1

The recipe ‘host-1_hostfile.rb’ has been applied and the /etc/hosts file has been updated as per the required state mentioned in the recipe.

Step4: Applying the recipe again to the system

In this step we will try to apply the same recipe again to see what response we get from the execution.

[user@host-1 chefspace]$ sudo chef-client --local-mode host-1_hostfile.rb
[2017-06-02T00:27:43-07:00] WARN: No config file found or specified on command line, using command line options.
[2017-06-02T00:27:43-07:00] WARN: No cookbooks directory found at or above current directory. Assuming /home/user/chefspace.
Starting Chef Client, version 12.12.15
resolving cookbooks for run list: []
Synchronizing Cookbooks:
Installing Cookbook Gems:
Compiling Cookbooks...
[2017-06-02T00:27:46-07:00] WARN: Node host-1 has an empty run list.
Converging 1 resources
Recipe: @recipe_files::/home/user/chefspace/host-1_hostfile.rb
* file[/etc/hosts] action create (up to date)
Running handlers:
Running handlers complete
Chef Client finished, 0/1 resources updated in 03 seconds

As you can see the state of the file is intact and hence no action has been taken.

Step5: Restore the state of the file

Let’s say somebody updated the /etc/hosts file for some testing purpose and forgot to restore the file to its initial state.

[user@host-1 chefspace]$ cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.1 host-1
192.168.0.100 testserver.example.com

Now apply the recipe and restore the state of the /etc/hosts file,

[user@host-1 chefspace]$ sudo chef-client --local-mode host-1_hostfile.rb
[2017-06-02T00:32:04-07:00] WARN: No config file found or specified on command line, using command line options.
[2017-06-02T00:32:04-07:00] WARN: No cookbooks directory found at or above current directory. Assuming /home/user/chefspace.
Starting Chef Client, version 12.12.15
resolving cookbooks for run list: []
Synchronizing Cookbooks:
Installing Cookbook Gems:
Compiling Cookbooks...
[2017-06-02T00:32:07-07:00] WARN: Node host-1 has an empty run list.
Converging 1 resources
Recipe: @recipe_files::/home/user/chefspace/host-1_hostfile.rb
* file[/etc/hosts] action create
- update content in file /etc/hosts from 75a244 to fd61bc
--- /etc/hosts 2017-06-02 00:29:55.540172876 -0700
+++ /etc/.chef-hosts20170602-10735-1fg7rr 2017-06-02 00:32:07.860349837 -0700
@@ -1,5 +1,4 @@
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.1 host-1
-192.168.0.100 testserver.example.com
- restore selinux security context
Running handlers:
Running handlers complete
Chef Client finished, 1/1 resources updated in 03 seconds
[user@host-1 chefspace]$ cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.1 host-1

As you can see the file state has been, restore as defined in the recipe.

This is just a simple example of how and why we want to use chef to configure our systems and maintain the state of the system. The recipe could be run periodically or as part of continuous automation system such as chef Automate.

Hope you enjoyed reading this article. Thank you.