This is in continuation of the articles of the series “Learning Ansible.
In the previous article
Getting started with Ansible – Part 4, we learned about setting up local ansible config. We also wrote our first playbook and got to know about the setup module and fact variables. So, in this article, we will take it forward and will be covering the below topics,
- Printing fact variables using a debug module.
- Different levels of verbosity.
- Conditions in Playbook.
- Deploying sample file on different web servers using playbook.
Let’s get started.
Printing Fact Variables using debug module
We have created a sample playbook named print_fact.yml and here, we are printing the value of the OS family. We have seen that there are lots of built-in fact variables that we get by default when setup module executes.
Here, we are using the debug module which will help us to print fact variable. Ansible uses Jinga2 templating and thus we have enclosed ansible_os_family in {{}}.
Below is the code for our playbook.
- ---
- - hosts: websrv01
- tasks:
- - name: Printing Fact Variables
- debug:
- msg: "The value of OS Family is {{ansible_os_family}}"
Let’s run this playbook with the below command.
ansible-playbook print_fact.yml
OUTPUT
We can see from the output that the OS family has been printed successfully and for our web server 01, the OS family is RedHat as it is a CentOS-based web server.
Note
If you know that you are not going to use the values of fact variables inside the plays in your playbook, then it is recommended to disable the gather_Facts task which is a part of setup module and runs by default. This can be done by setting gather_facts: False.
Now, let’s see if we are still going to get the value of ansible_os_family variable. The updated playbook will be like.
- ---
- - hosts: websrv01
- gather_facts: False
- tasks:
- - name: Printing Fact Variables
- debug:
- msg: "The value of OS Family is {{ansible_os_family}}"
It clearly says that the variable is undefined as we have disabled the task. Let’s enable it again and see the values of OS family for all web servers.
Updated playbook code
- ---
- - hosts: all
- tasks:
- - name: Printing Fact Variables
- debug:
- msg: "The value of OS Family is {{ansible_os_family}}"
OUTPUT
And, we can see that Web Server 03 has Debian family as it is an Ubuntu server.
Now, we have seen the debug module, and this can be a real help if we want to know and print some information about the target machines and many times, we need detailed information on the output in case of errors. So, to get even more detailed output, we have different levels of verbosity which will help us to get to the exact error easily.
Levels of verbosity
Sometimes, even looking at the output doesn’t make us understand what exactly the issue is. So, there are 4 levels of verbosity and if we want to know more in the output, then we can add those levels during the execution of the playbook.
Level1
ansible-playbook -v web_db.yml
Level2
ansible-playbook -vv web_db.yml
Level3
ansible-playbook -vvv web_db.yml
Level4
ansible-playbook -vvvv web_db.yml
You can run all these commands to see the output. The number of occurrence of -v denotes the levels of verbosity.
Conditions in Playbook
In our previous article
Getting started with Ansible – Part 4, we got an error when we tried to install package httpd on web server 03 and we mentioned that we will resolve the error in the next article. So, let’s see what happened which caused installation failure on web server 03.
Httpd package could not be installed on web server 03 as web server 03 has an Ubuntu OS and this OS doesn’t have httpd instead it has apache2 to handle HTTP requests and yum is also not available there, so we will use apt to install the package in Ubuntu.
So, to do this, we can apply conditions in the playbook. Below is the code for the playbook which depending on the OS family decides which package to install using yum or apt.
- ---
- - hosts: WebServersGroup
- become: yes
- tasks:
- - name: Install httpd service
- yum:
- name: httpd
- state: present
- when: ansible_os_family == "RedHat"
-
- - name: Install apache2 service
- apt:
- name: apache2
- state: present
- when: ansible_os_family == "Debian"
-
- - name: Start and Enable httpd service
- service:
- name: httpd
- state: started
- enabled: yes
- when: ansible_os_family == "RedHat"
-
- - name: Start and Enable apache2 service
- service:
- name: apache2
- state: started
- enabled: yes
- when: ansible_os_family == "Debian"
OUTPUT
So, we can see now that issue has been resolved and for TASK [Install Httpd service] ansible is skipping web server 03 and for TASK [Install apache2 service] ansible is skipping web server 01 and 02.
That’s how we can do lots of interesting things with conditions in the playbook. For example, depending on free ram size with fact variables we can skip or perform some tasks, etc.
As we have learned conditions in the playbook, now let’s deploy some sample files on all web servers 01,02 and 03 and this time, I hope you will understand what is being written in the playbook.
Deploying sample file on different web servers using a playbook
So, to deploy files on web servers we are going to use copy and service modules of ansible.
- Copy module will copy the files from source to destination.
- The service module will help us in starting and enabling the service.
But before that let’s create a sample index.html file in the files directory with the below commands.
- mkdir files
- vim files/index.html
Below is our playbook.
- ---
- - hosts: WebServersGroup
- become: yes
- tasks:
- - name: Install httpd service
- yum:
- name: httpd
- state: present
- when: ansible_os_family == "RedHat"
- - name: Install apache2 service
- apt:
- name: apache2
- state: present
- when: ansible_os_family == "Debian"
- - name: Start and Enable httpd service
- service:
- name: httpd
- state: started
- enabled: yes
- when: ansible_os_family == "RedHat"
- - name: Start and Enable apache2 service
- service:
- name: apache2
- state: started
- enabled: yes
- when: ansible_os_family == "Debian"
- - name: Deploy index file
- copy:
- src: files/index.html
- dest: /var/www/html/index.html
- owner: root
- group: root
- mode: '0644'
- backup: yes
- - name: Restart httpd service on CentOS
- service:
- name: httpd
- state: restarted
- enabled: yes
- when: ansible_os_family == "RedHat"
- - name: Restart apache2 service on Ubuntu
- service:
- name: apache2
- state: restarted
- enabled: yes
- when: ansible_os_family == "Debian"
We can see that all of our tasks have been completed successfully. Now, let’s open one of the web servers using the public IP in AWS and paste it on the browser and see if it works.
Clearly, this content is coming from the same file which we just deployed. Great, 😊 we are done.
Summary
So, we have learned about the debug module for printing fact variables and also saw conditions in the playbook and deployed sample files with different tasks in plays in playbooks. We have lots of other interesting things coming up so keep learning.
You can also check out some of my previous articles of the series “Learning Ansible” here,
I hope you find this article helpful. Stay tuned for more … Cheers!!