Ansible Playbooks: How to Create and Manage Playbooks. In this guide, we introduce Ansible playbooks, its major features then show you how to create and use Ansible playbooks.
With IT environments becoming more and more complex, system administrators and developers are searching for new and better technologies for quick results. An automation tool like Ansible makes complex tasks simpler. The powerful open-source automation tool comprises various features that eliminate repetitive tasks and time consumption. As a result, DevOps teams are able to focus on other areas that require more time and attention.
Every Ansible configuration starts with a playbook, which is a crucial aspect of Ansible. In simple terms, an Ansible playbook is a structured collection of scripts used for outlining the steps necessary to manage a system configuration.
One or more plays, each of which is made up of modules, make up Ansible playbooks. Ansible plays are flexible because of modules, which are PowerShell or Python that address specific areas of the target managed servers. The open source community that utilizes and promotes Ansible as it offers a wide variety of modules. The modules are basically for user administration and program installation.
System administrators and DevOps teams may automate different elements of server management and application deployment with Ansible Playbooks, making it a highly useful tool in the market. Here are a few key features of Ansible Playbook:
1. Offers Variable Support – Playbooks facilitate the usage of variables, enabling you to specify data dynamically and increase the flexibility and reusability of your configurations.
2. Simple Syntax – You may specify the intended state of your systems with Ansible Playbooks thanks to their YAML syntax. Playbooks are simple to read and comprehend because you indicate what needs to be done instead of writing code that explains how to achieve it.
3. Versatile – Playbooks may be used for many different activities, ranging from basic configuration management to intricate application deployments and orchestration, making Ansible a flexible tool for different automation requirements.
4. Scalable – In terms of scalability, Ansible performs better than other IT automation technologies. Ansible can work on a bigger scale than other IT Automation solutions, and it easily handles a larger infrastructure. It even has the ability to manage node fluctuations at ease, unlike other automation technologies.
5. Integration support – Ansible Playbooks’ functionality and connectivity with current workflows can be expanded by integrating with a variety of external tools and services, including cloud providers, monitoring tools, and various version control systems.
6. Agentless Architecture – Another key feature of Ansible is it requires no agents or daemon installations on remote hosts. To connect to and control the target systems, it employs SSH, which eases setup and lowers security concerns.
7. Community Support – Ansible may have a small support community, but it holds yearly events and conducts speedy troubleshooting of online resources. In fact, the enterprise version features a team that offers qualified assistance.
We are at the main part of Ansible Playbooks: How to Create and Manage Playbooks. Let’s try.
Ansible provides package management modules such as, apt, dnf, pacman and zypper for all major Linux distributions. Use them to install and remove software packages to your target servers.
Let’s create a playbook file to install the Nginx package on the target server.
Save and close the file then run the playbook to restart Nginx and MariaDB services.
ansible-playbook playbook.yml -i inventory
You will see the following screen.
Copy Files Using Playbook
Ansible provides a copy module that allows you to copy files from the Ansible controller machine to remote machines. It also allows you to assigned the owner and group ownership to the specific files.
Let’s create a playbook file to copy a file.txt from the control node to the remote node.
nano playbook.yml
Add the following lines:
- name: Ansible copy module example
hosts: web-server
tasks:
- name: Copy files from control node to remote node
ansible.builtin.copy:
src: /home/vyom/Documents/file.txt
dest: /tmp/
Save and close the file then execute the playbook to perform the copy operation.
ansible-playbook playbook.yml -i inventory
You will see the following screen.
You can use the copy module with the remote_src option to copy files within the remote node. Let’s create a playbook file to create a backup copy of nginx.conf configuration file.
nano playbook.yml
Add the following lines:
- name: Ansible copy module example
hosts: web-server
tasks:
- name: Copy files within the remote node
ansible.builtin.copy:
src: /etc/nginx/nginx.conf
dest: /etc/nginx/nginx.conf.bak
remote_src: yes
Save and close the file then execute the playbook to perform the copy operation.
You can use the lineinfile Ansible module to modify, replace or add a single line to a file. It is specifically used to configure remote servers.
Let’s create a playbook file to modify the SSH service configuration. We change the line “PermitRootLogin yes” to “PermitRootLogin no” and change the line “PubkeyAuthentication yes” to “PubkeyAuthentication no”.
Ansible Playbooks: How to Create and Manage Playbooks: Conclusion
Ansible is becoming a popular configuration management tool used by many DevOps engineers and IT professionals who require efficient and effective results. It provides a number of features that can assist with some of your incredibly complex IT procedures. Playbooks can be executed faster by using Ansible’s parallel task execution capabilities across many hosts. In fact, it can even be used to simplify and streamline the configuration infrastructure management process.
I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.