Hey Learners! Welcome back. In the previous challenge, we understood the concept of Ad-hoc commands which comes in handy when you want to perform a quick task, practice some Ad-hoc commands and manage our nodes with Ansible. Moving forward, we'll learn about the concept of Ansible Playbooks, which is suitable for orchestrating complex, multistep tasks. Let's start...
Ansible Playbooks
Ansible Playbooks are the files where Ansible code is written in YAML format which contain the steps which we want to execute on a particular node managed by Ansible.
Playbooks are the building blocks for all the use cases of Ansible.
Playbook Structure:-
Each playbook is an aggregation of one or more plays in it. Playbooks are structured using Plays.
The function of a Play is to map a set of instructions defined against a particular host.
YAML is a strictly typed language so extra care needs to be taken while writing the YAML files. A YAML starts with ---
(3 hyphens).
Task 1-
1- Write an Ansible Playbook to create a file on different servers.
Let us start by writing a Playbook for creating a file on managed nodes
Create a file_create.yml file with the below content. Use the vim file_create.yml
command.
---
- name: Create a empty file
hosts: nodes
tasks:
- name: Creating a "demo.txt" file
file:
path: /home/ubuntu/demo.txt
state: touch
...
The above is the sample playbook to create an empty file called "demo.txt" on managed nodes.
Different YAML tags:-
name - Specifies the name of the Ansible Playbook
hosts - specifies the lists of hosts or host groups against which we want to run the task. it is mandatory.
tasks - All playbooks should contain tasks or lists to be executed. Tasks are a list of actions one needs to perform on managed nodes. A tasks field contains the name of the task which is not mandatory but should prove useful in debugging the playbook. It should contain the module that should be executed, and the arguments that are required for the module you want to execute.
In this case, we define "Creating a "demo.txt" file" as a task
name
and use afile
module withpath
andstate
as an argument.
To execute this playbook use the ansible-playbook command as shown below
ansible-playbook file.yml
After successful execution of the playbook ensure that the file is created over managed hosts using the Ansible Ad-hoc command or you can check by accessing nodes over SSH directly.
2- Write an Ansible Playbook to create a new user.
Create a file called user_create.yml with the below content. Use the vim user_create.yml
command
---
- name: Create a new user
hosts: nodes
become: yes
tasks:
- name: Adding new user
user:
name: ansible
state: present
Execute the playbook and ensure the changes with the Ad-hoc command. Note use become
flag as a user required sudo privileges to create users.
3- Write an Ansible playbook to install docker on a group of servers
Create a "docker_install.yml" file with the content shown below using the vim docker_install.yml
command.
---
- name: Install docker
hosts: nodes
become: yes
tasks:
- name: Installing docker
apt:
name: docker.io
state: present
Here we use the become flag as yes which is used to activate privilege escalation.
If you have any error like " no matching packages found" then first update nodes with the apt update
command.
Execute the playbook and check whether docker is installed or not using the Ad-hoc command.
Task 2- Writing the Ansible Playbooks with the best practices
Ansible is a powerful open-source automation tool that allows us to automate IT infrastructure management. Ansible playbooks are the configuration files that describe the state of your systems. Writing them with best practices can make your automation more robust, reliable, and scalable. Let's discuss the some of best practices for writing Ansible Playbooks.
Use a clear structure:- Make sure your playbook is organized and easy to read. Use comments and section headers to explain what each part of the playbook does. Avoid nesting too many tasks or plays inside each other, as it can make the playbook difficult to read and maintain.
Use variables:- Variables can make your playbook more flexible and reusable. They allow you to store values that can be used across multiple tasks and playbooks. You can define variables in the playbook itself or separate files, such as group_vars or host_vars.
Use Loops:- To repeat any task or part of code multiple times use a loop in the Ansible playbook instead of writing code for each task.
Use conditions:- Conditions allow us to execute a task only if a specific condition is met. This can be useful when you need to run a task only on a subnet of hosts or when you need to run a task only if a specific variable is defined.
---
- name: Example for Variable
hosts: all
vars:
names:
- a
- b
tasks:
- name: creating "{{ item }}" file
file:
name: "{{ item }}"
state: present
loop: "{{ name }}"
- name: printing msg
debug:
msg: "b is my fav file"
when: names == "b"
...
Use Roles:- Roles are a way to organize your tasks, variables, and files' reusable components. They can be used across multiple playbooks and provide a way to share functionality between different teams. roles can also make our playbook more modular and easier to maintain.
Use Tags:- Tags allow us to selectively run specific tasks or groups of tasks within a playbook. This can be useful when testing or debugging your playbook or when you need to run a subnet of tasks. tags can be added to individual tasks or entire plays.
By following these best practices, you can write efficient, modular, and maintainable Ansible playbooks that can be easily adapted to different environments and situations.
Thank you so much for taking the time to read till the end! Hope you found this blog informative.
Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.
Find me on:- Hashnode LinkedIn Github
Happy Learning!