[Ansible] How to branch playbook processing [Configuration management]

Hello! I'm Shimeji from the System Solutions Department.
It's been two months since I transferred to the SRE team.
There's still a lot I don't understand, but I'm grateful for the environment where I can learn new things every day!
Learning new things is fun!

Today we'll be talking about the configuration management tool Ansible.
There are times when you want to branch the processing of a playbook.

For example, suppose you run the following Playbook process.
The contents are the usual one, which renames the original configuration file and then installs a new configuration file.

- name: Rename Config File shell: mv /etc/hoge/main.cf /etc/hoge/main.cf_backup - name: Deploy Config File template: src: "{{ inventory_dir }}/roles/hoge/template/conf.j2" dest: "/etc/hoge/main.cf" owner: root group: root mode: 644

The above process will "rename the existing configuration file" and "install a new configuration file" without any problems.
However, if there is already a file called "main.cf_backup", you may want to skip the process...
In such a case, you can use Registered Variables and a when statement.

Registered Variables allow you to store the results of a task in a variable.
By branching the process based on the result, you can skip the process.
To branch the process, use the when statement.

If you want to skip a file that already exists

The following is the processing of the Playbook taking the above into consideration

- name: Rename Config File Confirm stat: path: /etc/hoge/main.cf_backup register: result - name: Rename Config File shell: mv /etc/hoge/main.cf /etc/hoge/main.cf_backup when: not result.stat.exists - name: Deploy Config File template: src: "{{ inventory_dir }}/roles/hoge/template/conf.j2" dest: "/etc/hoge/main.cf" owner: root group: root mode: 644

See the process called Rename Config File Confirm

  stat: path: /etc/hoge/main.cf_backup register: result

We use the stat module to check if main.cf_backup exists.
If it does, we store True in the variable result, otherwise we store False.

And there is a when statement in the Rename Config File process

when: not result.stat.exists

Only perform the Rename Config File operation if result is not True

Let's confirm that the process is not executed when the conditions are actually met.
Let's execute the process on the following server where main.cf_backup exists:
192.168.33.72 [CentOS72]

ansible-playbook -i hosts operation.yml --ask-pass
PLAY [apply common configuration to all nodes] ********************************* TASK [Gathering Facts] ***************************************************************** ok: [192.168.33.72] TASK [hoge : Rename Config File Confirm] ************************************************ ok: [192.168.33.72] TASK [hoge : Rename Config File] ************************************************** skipping: [192.168.33.72] TASK [hoge : Deploy Config File] ********************************************************************* ok: [192.168.33.72] PLAY RECAP **************************************************************** 192.168.33.72 : ok=3 changed=0 unreachable=0 failed=0

Yes, the process is skipped

Other branching operations

The when statement can also perform various other conditional branching.
Below is a Playbook that installs Apache only if the OS is CentOS7.

  - name: CentOS7 Install Apache yum: name: httpd state: present when: - ansible_facts['distribution'] == "CentOS" - ansible_facts['distribution_major_version'] == "7"

Conditions can be written in list format.
In this case, the process will be executed only if all conditions are met.
(In the above example, the OS is CentOS and the version is 7.)

Let's actually execute the process on the following two servers:
192.168.33.67 [CentOS67]
192.168.33.72 [CentOS72]

ansible-playbook -i hosts operation.yml --ask-pass
PLAY [apply common configuration to all nodes] ********************************* TASK [Gathering Facts] ***************************************************************** ok: [192.168.33.65] ok: [192.168.33.72] TASK [test : CentOS7-install-apache] ************************************************* skipping: [192.168.33.65] changed: [192.168.33.72] PLAY RECAP ********************************************************************* 192.168.33.65 : ok=1 changed=0 unreachable=0 failed=0 192.168.33.72 : ok=2 changed=1 unreachable=0 failed=0

Only 192.168.33.72 is being processed.
Success!

At the end

This was a brief introduction, but
there are many other ways to branch conditions with the when statement.
If you want to know more, please read the official documentation
!

If you found this article helpful , please give it a like!
1
Loading...
1 vote, average: 1.00 / 11
20,241
X facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Shimeji mushrooms

CERTIFICATE:
- TOEIC 835
- LPIC304
- AWS Solution Architect Associate
- AWS Solution Architect Professional
- GCP Professional Cloud Architect
- IPA SC (not registered)

Kagome, Kagome,
the old man behind me, that's it.

It's my uncle. (2018)