Hey Techies…👋
In this blog, we’re going to discuss about Mastering Ansible Playbook Features: Part-2.
Error handling in Ansible playbooks is crucial for ensuring the reliability and robustness of your automation tasks. Ansible provides several mechanisms for handling errors and exceptions during playbook execution.
✨✨Writing a playbook with more than one play and handling errors is an essential part of Ansible automation. Here’s an example playbook that includes multiple plays and demonstrates how to handle errors:
The ansible-playbook — help command is used to display the help and usage information for the ansible-playbook command. When you run this command, Ansible will provide a list of available options, flags, and a brief description of how to use the ansible-playbook command effectively. This is a helpful reference when you need to understand the various command-line options and syntax for running Ansible playbooks.
In the example provided, run the ‘ansible-doc service’ command to learn about the syntax and documentation for the Ansible module related to the ‘service’ management. This command allows you to access detailed information and documentation on how to use the ‘service’ module in Ansible, including its parameters, options, and usage.
I’ve given the name for below playbook as httpd.yaml
The provided Ansible playbook is used to install the latest version of the Apache HTTP Server (httpd) and then restart the web service on all hosts defined in your Ansible inventory. Let’s break down the playbook:
vim httpd.yaml
Add the below entries into the file:
---
- name: install httpd
hosts: all
tasks:
- name: install httpd
yum:
name: httpd
state: latest
- name: restart web service
service:
name: httpd
state: started
- Under tasks, there are two tasks:
- Play 1 — “Install httpd” task uses the yum module to ensure that the latest version of the “httpd” package (Apache) is installed.
- Play 2 — “Restart web service” task uses the service module to restart the “httpd” service, ensuring that the web server is running with the latest changes.
When you run this playbook, it will check if the ‘httpd’ package is installed, and if it’s not, it will use ‘yum’ to install the latest version. It will also ensure that the ‘httpd’ service is restarted, applying any configuration changes that may have occurred during the installation. This playbook is suitable for managing Apache HTTP Server installations on your target hosts.
ansible-playbook httpd.yaml - syntax-check
ansible-playbook httpd.yaml -C
Run the below command and before entering login to the node1 and node2 server and run:
node1 → tailf /var/log/messages — is used to continuously display the contents of the /var/log/messages log file in a terminal.
node2 → watch -n 1 yum list httpd — is used to continuously monitor the available versions of the “httpd” package using the yum package manager with a 1-second interval.
ansible-playbook httpd.yaml — when we run this command we will get error like below:
To remediate this error : run
cat /etc/ansible/ansible.cfg
You’ll found lines below and we’re going to edit it:
Uncomment the four lines above by removing the hash symbols (#), and insert the following lines into the ‘ansible.cfg’ file as shown below:
[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
This configuration is using the [privilege_escalation] section to specify privilege escalation settings.
- become=True: This setting indicates that privilege escalation is enabled. Ansible will attempt to escalate privileges when executing tasks.
- become_method=sudo: It specifies the method Ansible should use for privilege escalation. In this case, it’s using the ‘sudo’ method, which is a common way to run commands with superuser (root) privileges.
- become_user=root: It specifies the user account to which Ansible should escalate privileges. In this case, it’s set to ‘root,’ which is the superuser.
- become_ask_pass=False: This setting determines whether Ansible should prompt for the password when escalating privileges. Here, it’s set to ‘False,’ indicating that Ansible should not ask for a password during privilege escalation.
This configuration is typically used in Ansible playbooks to specify how tasks should run with elevated privileges, allowing you to perform administrative tasks on target hosts.
Now run the command ‘ansible-playbook httpd.yaml’ and check the differences by logging into node1 and node2 and run the corresponding commands stated above:
We can observe that the error has been resolved, and the ‘httpd’ service has been successfully installed and initiated.
✨✨When copying and pasting playbooks from the internet, it’s important to ensure that the indentation is correct to avoid issues. Here we’re discussing an effective and simple method to avoid such issues: You can achieve this by using the :set paste command. Here’s how to do it:
Open your file in Vim:
vim test.yaml
Enter command mode by pressing Esc.
Enable paste mode by typing:
:set paste
Now, paste your content into Vim using your preferred method.
By using :set paste, Vim will treat the pasted text as-is without automatically adjusting the indentation.im your_file.yaml
---
- name: testing
hosts: all
tasks:
- name: Install the latest version of Apache
yum:
name: httpd
state: latest
- name: Install Apache >= 2.4
yum:
name: httpd>=2.4
state: present
- name: Install a list of packages (suitable replacement for 2.11 loop deprecation warning)
yum:
name:
- nginx
- postgresql
- postgresql-server
state: present
There are 3 plays in above playbook :
Under tasks, there are three plays:
- “Install the latest version of Apache”: This task uses the yum module to ensure that the latest version of the “httpd” package (Apache) is installed.
- “Install Apache >= 2.4”: This task uses the yum module to ensure that a version of “httpd” greater than or equal to 2.4 is installed.
- “Install a list of packages”: This task uses the yum module to install a list of packages, including ‘nginx,’ ‘postgresql,’ and ‘postgresql-server.’ This demonstrates how to install multiple packages using a list.
If you want to know the differences by running above playbook, you can login to the target nodes and run below commands which we discussed already:
node1 → tailf /var/log/messages
node2 → watch -n 1 yum list httpd
💡Stay tuned… This topic will be continued
💹Next day — Mastering Ansible Playbook Features: Part-3
⭐⭐⭐ Enjoy your learning….!!! ⭐⭐⭐