Open In App

How Can YAML Be Utilized In Ansible Playbooks For Automation And Orchestration ?

The backbone of Ansible is used to define the configuration to be done at the target nodes. It manages the complex configuration using a playbook that is written in YAML. Therefore, understanding its flexibility is the key to using it to manage target nodes efficiently.

Overview of YAML

One minute of reading through the following GFG article



What is the difference between YAML and JSON? link

Features of YAML

Features are also an advantage of YAML



Disadvantages of YAML

Applications of YAML

More Detailed Explanation About The YAML

How YAML is used?

We can optionally used optionally begin with — and end with … . Three things to take note of Lists, Dictionaries and Variables. Taking care of indentation is a must, which is as follows: If you want to nest the data under a main one, remember to start with a new line and a tab space for.

Step 1: All members of the list begin with – and are at same at same indentation level

---
#GFG articles by yeskaydeecodes9
- Ansible Configuration And Inventory Files
- How can YAML be utilized in Ansible playbooks for automation and orchestration?
...

Step 2: And a dictionary is represented in simple key: value form.

---
author:
name: yeskaydeecodes9
interests: Devops
likes: networks
...

Step 3: And the both can be merged as to form list of dictionaries.

- author:
name: yeskaydeecodes9
interests: Devops
likes: networks
- writer:
name: novelsbyauthor
interests: SciFi
likes:
- flora
- fauna

Step 4: Variables are specified using {{}}

---
- name: Install and Configure Web Server
hosts: web_servers
become: true

vars:
web_server_package: apache2
web_server_port: 80

tasks:
- name: Install Web Server Package
yum:
name: "{{ web_server_package }}"
state: present

- name: Configure Web Server Port
template:
src: templates/httpd.conf.j2
dest: /etc/httpd/conf/httpd.conf
notify: restart_web_server

handlers:
- name: restart_web_server
service:
name: apache2
state: restarted

Here two variables are defined web_server_package and web_server_port.

Why restart?

Whenever the server restarts there is a chance that the service has not started or to restart it after the configuration has been changed, therefore as a best practice we restart it.

Code and Structure

Structure Of The Folder

my_ansible_project/

├── hosts [inventory file]

└── install_apache.yml [playbook]

Code

---
- hosts: all
become: true
tasks:
- name: Installing apache2 package and php
apt:
name:
- apache2
- libapache2-mod-php
state: latest
update_cache: yes
when: ansible_distribution == "Ubuntu"

List of IP’s classfied as Web_servers ( for more info scroll to bottom to find link)

ansible-playbook install_apache.yml -u client -i inventory --ask-become-pass ;

Note: Here client is the target node , whose IP is defined in inventory file.

Output

Notice the change

Now we can see no changes are made, as it was already done

What to do next?

Setup the Ansible on Virtual Machines on Virtual Box, which is explained in detail in the article mentioned. After understanding Ansible Playbook, you will see that YAML is used not only in Playbook but also in Inventory Files and Configuration Files. For more details refer to this GFG article

Conclusion

Playbooks are written in YAML, and are foundation to use Ansible effectively. Though with many advantages, it has some limitations as discussed.It enables users to automate and orchestrate infrastructure and application deployments with ease.

YAML be utilized in Ansible playbooks for Automation and Orchestration – FAQ’s

Does Ansbile Playbook support only YAML?

No, it may be a primary format but JSON are supported with some limitations. Therefore YAML is preferred

Is Indentation required in YAML?

YAML syntax is relatively forgiving compared to other data serialization formats, but proper indentation and formatting are crucial for ensuring readability and correctness. In YAML, indentation is used to denote the structure and hierarchy of data.

Can YAML be used only with Ansible?

Nope, there are further applications as you further deep down into the wold of DevOps you would see that it would be used from managing CI/CD pipelines to other automations.


Article Tags :