5 Ansible Modules/Keywords that you must know
By: Yogendra Singh Rathore
Tags: Ansible Become, Ansible Modules, Ansible, Ansible Tutorial, Ansible Handlers

These 5 modules/keywords of ansible will make your configuration life easy, so let’s get started.

1. Timestamp

As the name suggests, Timestamp is a keyword which is used to provide the current timestamp of the system i.e. yy-MM-dd HH:mm:ss.

Where to use?

To print out the time for logging purposes. To name a directory. Store the value into a variable and use it as per your needs.

Example(To name a directory using timestamp variable):

In the above example we have used timestamp variable to name a directory that is in the opt/ folder, so the name of directory would be: ROOT_2020–12–23_13_33.tgz

2. lineinfile and regexp

The “lineinfile” keyword is used to find and make some changes at a particular line in the file. This becomes very helpful when you want to edit some configuration changes in a file without opening the file.

Example(To change the bind address in mysqld.conf file)

The “lineinfile” keyword is used along with the regexp and line keywords. The regexp keyword helps in finding the line where you want to make changes. The line keyword is used to update the changes in that line.

3. handlers and notify

Handlers are life savers in ansible. They are same as Functions in any programming language.

“Handlers are write once use multiple times”

Handlers are used along with the “notify” keyword. In the “lineinfile Example”, you must have noticed the “notify” keyword to restart mysql.

Example:

Continuing with the above example, so after making the changes bind-address using “lineinfile”, we need to restart the mysql service in order to make changes take effect.

So, we make use of handlers which will restart the mysql service. Here, the name of our handler is “Restart mysql”. So, in our playbook whenever we want to restart the mysql service we need not write the entire code again and again, we can now simply call this handler using the notify keyword.

4. become and become_method

The become keyword is one of the most powerful keyword in the ansible. It is used to set the privilege of the user. So, let’s continue with the above example, when using “lineinfile” command, you are making some changes in the file that is specified in the “path” command. So, in order to make some changes in that file you need the “write permission” of that file.

Thus, to access the file and make changes in it you need to become a user that can write in that file. This is done with the help of become keyword.

Example:

Here you will see that become is “yes” and become_method is “sudo”, this means that the ansible user will become a sudo user, thus allowing to make changes in that file.

So, now when you execute this file, you will get an error: “sudo: a password is required

”. Thus, to avoid this error, execute the ansible playbook with the following command:

ansible-playbook Filename.yml — ask-become-pass

5. register, debug and when

The register keyword is the most helpful keyword in ansible. It is used to print the output on the output screen. Not just that, you can store the output into a variable and then this output variable can be used for conditional statements and looping.

Example:

In the above playbook, you can see that, we are creating a new ec2 key and then copying the private key to a specific location. Here, we have made use of register, debug and when keywords.

register: The variable ec2_key_info is a register variable that store the output of the ec2_key module.

debug: The debug module displays the output that we have specified in this case, “ec2_key_info” on the output screen.

when: The when keyword is used to apply a condition in the playbook task. In our case, we want to perform the copy the ec2 private key only when a new ec2 key is created. Thus we use “ec2_key_info.changed”, so if changed is true, it will be recorded in our register variable i.e. “ec2_key_info”. So the copy operation will only be performed when a new key is created.

Click Here, to try out some ansible examples.