Mastering Ansible Playbook Features: Part-1 — Hands-on Experience

Athira KK
9 min readNov 2, 2023

--

Hey Techies…👋

In this blog, we’re going to discuss about Mastering Ansible Playbook Features: Part-1.

🚀 Elevating Automation with Ansible Playbooks 🚀

Automation has emerged as the key to productivity and efficiency in the fast-paced field of information technology. Ansible Playbook, a potent technology that is transforming how we manage and orchestrate large systems, is also at the core of automation.

Playbooks serve as the defining structure for your workflow, executing tasks in the exact order they are written. These playbooks are formatted in YAML, describing and executing tasks through Ansible. Additionally, you have the flexibility to launch tasks both synchronously and asynchronously using playbooks.

Playbook is a fundamental and powerful component of Ansible, and its importance lies in several key aspects:

  • Automation: Playbooks are at the core of Ansible’s automation capabilities. They allow you to define and describe a series of automation tasks and workflows in a human-readable format. This automation can cover a wide range of activities, from system configuration and software installation to application deployment and orchestration.
  • Repeatability: Playbooks ensure that automation tasks are consistently applied across different environments and target hosts. This repeatability is critical in maintaining a consistent and predictable infrastructure, reducing human error, and enabling efficient scaling.
  • Modularity: Playbooks can be broken down into reusable roles and tasks. This modularity simplifies the management of automation code, making it more maintainable and adaptable for various use cases.
  • Scalability: Playbooks can be used to manage a single host or multiple hosts across different environments. This scalability is valuable when dealing with large-scale infrastructures or multi-environment deployments.
  • Error Handling: Playbooks allow you to define error handling and recovery strategies. You can specify how Ansible should react to different scenarios, ensuring that tasks are executed as expected even in the presence of errors.

Creating an Ansible playbook at a beginner level is a great way to start automating tasks in your infrastructure. Here’s a step-by-step guide to help you get started with a basic playbook:

🎯Prerequisites — 3 Virtual Machines

1. server - 1 CPU - 1GB RAM (Python 2.7) - Ansible Server
2. node1–1 CPU - 1GB RAM ( python 2.6 and above) - Ansible Client 1
3. node2–1 CPU - 1GB RAM ( python 2.6 and above) - Ansible Client 2

From ansible server login as an ansible user as per previous blog.

Please go through the previous blog in case you missed it —

https://medium.com/cloudnloud/setting-up-lab-environment-for-ansible-skillbuilder-series-7d41638815ed

From ansible user execute below command:

ansible all -m ping

the above ping command should return with a ping / pong green colour.

Step 1 : ansible-doc
Step 2: .yaml or .yml files
Step 3 : How to construct playbook
Step 4: How to ensure syntax issues in playbook before you execute playbooks
Step 5: How to see detailed output while playbook getting executed

1. Login to the ansible server:

Once you logged in to the server, switch to the dev directory and check that all attached hosts are visible and all are giving green “ping”: “pong” every time.

Step 1 : Importance of ansible-doc

🎯ansible-doc is a command-line utility within Ansible, enabling users to conveniently access and search Ansible’s extensive documentation right from the terminal. It offers insights into module functionality, parameter descriptions, and practical examples, making it a valuable tool for gaining in-depth knowledge about Ansible’s components and how to use them proficiently.

(I haven’t included the full output of this command since it’s quite lengthy).

ansible-doc -l

This command is used to list all the available Ansible modules along with a brief description of each module.

ansible-doc -l | wc -l

This command is used to determine the total number of available Ansible modules, helping users get an overview of the breadth of functionality that Ansible provides.

ansible-doc -l | grep package

This command is helpful when you want to find Ansible modules related to package management tasks, such as installing, updating, or removing software packages on remote systems.

ansible-doc yum

🎯The ansible-doc yum command will give you information about the module’s usage, parameters, examples, and other relevant details for working with YUM package management in Ansible playbooks and tasks.

(You will get a long output for the above command with module’s usage, parameters, examples, and other relevant details).

Here I’m taking a small part of the ‘Examples’ section to give a brief idea about creation of playbooks.

This section will provide you with detailed information and usage examples for the ‘yum’ module in Ansible, helping you understand how to work with YUM package management in your automation tasks.

Every module , there will be five to six examples like install the latest version , to know the packages installed ,remove specific packages and so on. ansible-doc will give you all modules . take one module and goto example section and practice on it to make more familiar with the playbook creation.

Step 2: .yaml or .yml files

🎯YAML — The abbreviation “YAML” stands for “YAML Ain’t Markup Language.” YAML is a human-readable data serialization format often used for configuration files and data exchange between languages with different data structures. The phrase “Ain’t Markup Language” emphasizes that YAML is not a markup language like HTML or XML, but rather a simple and readable way to represent structured data. YAML uses indentation and key-value pairs to define data structures, making it easy for both humans and machines to work with.

🎯YML — “Yet Another Markup Language’’ is a playful acronym that highlights the simplicity and readability of YAML (YAML Ain’t Markup Language), a data serialization format. The use of “Yet Another’’ humorously suggests that YAML is another language for structuring data, despite its name not indicating it as a markup language like HTML or XML.

Step 3 : Constructing playbook

Let’s construct our first playbook named ‘example.yaml’.

Follow the below steps:

Login to ansible server and switch as ansible user , then

[ansible@server ~]$ cd dev
[ansible@server dev]$ vim example.yaml

Add below entries in example.yaml file:

---
- name: Demo installing package
hosts: all
tasks:
- name: Install the latest version of Apache
yum:
name: httpd
state: latest

The provided Ansible playbook is a basic example of how to install the latest version of the Apache web server (httpd) on all hosts defined in your Ansible inventory. Let’s break down the playbook:

  • ‘ — — — ‘at the beginning is the YAML document separator.
  • name is a description of the playbook.
  • hosts: all specifies that this playbook will be executed on all hosts defined in your Ansible inventory.
  • Under tasks, there’s a task named “Install the latest version of httpd.”
  • The yum module is used to interact with the YUM package manager.
  • name: httpd specifies the name of the package you want to install, in this case, ‘httpd’ (Apache).
  • state: latest instructs Ansible to ensure that the latest version of the package is installed.

When you run this playbook, it will check if the ‘httpd’ package is installed on all target hosts, and if it’s not, it will use ‘yum’ to install the latest version of Apache. This playbook is a simple example of how Ansible can be used for package management and can be extended to perform more complex automation tasks.

Step 4: How to ensure syntax issues in playbook before you execute playbooks:

🎯ansible-playbook —help — This 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.

ansible-playbook --help

(I haven’t included the full output of this command since it’s quite lengthy).

Syntax: ansible-playbook <playbook.yaml> - syntax-check
ansible-playbook example.yaml - syntax-check

🎯ansible-playbook example.yaml — — syntax-check indicates that the syntax of the Ansible playbook ‘example.yaml’ is correct. The playbook has been successfully validated for syntax errors, and the message “playbook: example.yaml” informs you that the playbook file being checked is ‘example.yaml.’ This means that there are no syntax issues in your playbook, and it is ready to be executed without any syntax-related errors.

Now I’ll intentionally introduce an error to demonstrate how the output will change:

Take a look at the following output; it clearly indicates the error’s location. Let’s resolve it and then execute the playbook.

🎯Simulation — It typically refers to the process of running Ansible playbooks or tasks in a test or simulation environment to validate their behavior before applying them to a production environment.

Run Playbooks in Check Mode: Use the ansible-playbook command with the -C or — check option to perform a dry run of your playbooks. This checks for syntax errors and shows you what changes would be made without actually applying them.

Syntax: ansible-playbook <playbook. yml> -C
ansible-playbook example.yaml -C

The output of the command ansible-playbook example.yaml -C shows the results of executing an Ansible playbook in check mode (using the -C flag). Here’s what the output means:

  • The playbook named “example.yaml” is executed.
  • It starts with the “Gathering Facts” task, which collects information about the target hosts. In this case, it runs successfully on both “node1” and “node2” (as indicated by “ok”).
  • The next task is “Install the latest version of httpd.” It uses the yum module to ensure that the latest version of the “httpd” package (Apache) is installed. This task resulted in a “change” on both “node1” and “node2,” indicating that the state of the system was changed to ensure the latest version of “httpd” is installed.
  • The “PLAY RECAP” section summarizes the results for each host. In this case, both “node1” and “node2” had 2 tasks executed, with 1 change. There were no unreachable hosts or failed tasks.

This output confirms that the playbook ran successfully in check mode, and it would have made the necessary changes if it were run without the -C flag. It’s a good way to verify the intended changes before applying them.

To confirm this action, log in to both node1 and node2 and perform the following checks:

[root@node2 ansibleseries]# yum list httpd

Step 5: How to see detailed output while playbook getting executed

Syntax: -playbook <playbook. yml> -vvvv
ansible-playbook example.yaml -C -vvvv

🎯The command ansible-playbook example.yaml -C -vvvv runs an Ansible playbook in check mode (-C) with very high verbosity (-vvvv). Here’s what the additional verbosity levels provide:

  • -v stands for verbose, and when used multiple times (as in -vvvv), it increases the level of verbosity.

When running with very high verbosity, you’ll get detailed information about each step of the playbook execution, which can be useful for debugging and understanding the exact actions taken by Ansible.

The output will show extensive information, including the configuration, tasks executed, host details, and more. This can be helpful for diagnosing any issues, understanding the execution flow, and monitoring the changes Ansible would make if the playbook were run without the -C flag.

(I haven’t included the full output of this command since it’s quite lengthy).

💡Stay tuned… This topic will be continued

💹Next day — Mastering Ansible Playbook Features: Part-2

⭐⭐⭐ Enjoy your learning….!!! ⭐⭐⭐

--

--

Athira KK
Athira KK

Written by Athira KK

AWS DevOps Engineer | Calico Big Cats Ambassador | WomenTech Global Ambassador | LinkedIn Top Cloud Computing Voice

No responses yet