How to manage server resource using chef-client – basics

How to manage server resource using chef-client – basics

Purpose – Chef Basics – Managing server resource directly using chef-client

====================================================


Assuming Chef DK is already installed on your machine


[user@host-1 chefspace]$ chef-client –version

Chef: 12.12.15


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

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


Scenario – Make sure to configure your server to use a specifed /etc/hosts file as per the recipe


[root@host-1 ~]# cat /etc/hosts

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

::1 localhost localhost.localdomain localhost6 localhost6.localdomain6


Step1: Create a chefspace directory for working with chef recipes


[user@host-1 chefspace]$ mkdir chefspace;cd chefspace

[user@host-1 chefspace]$ pwd

/home/user/chefspace

[user@host-1 chefspace]$ hostname

host-1

[user@host-1 chefspace]$ vi host-1_hostfile.rb

[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


Step2: Run the chef-client tool to locally apply the recipe ‘host-1_hostfile.rb’ to the system


[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]$

[user@host-1 chefspace]$

[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


Step3: Applying the recipe again to the system


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



Step4: Now manually update the /etc/hosts file and apply the recipe to restore the state of the file


Lets 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]$

[user@host-1 chefspace]$

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