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

  • YAML stands for Yet Another Markup Language.
  • Just like the JSON format, YAML is also a readable format.
  • YAML provides a clean and human-readable way to represent complex data structures, making it ideal for defining tasks, variables, and configurations in the Ansible playbook. Extension is .yml or .yaml.
  • It’s commonly used for configuration files and data exchange.

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

  • Human-Readable: YAML’s syntax is designed to be easily readable by humans, making it good for configuration files and playbooks.
  • Simplicity: It has a minimalistic syntax, and YAML is straightforward to write and understand, even for those without programming experience.
  • Expressiveness: It is simple, yet it describes complex structures with simple methods.
  • Hierarchical Structure: YAML uses a hierarchical structure, making it suitable for representing complex data structures such as dictionaries and lists, which are commonly used in Ansible playbooks.

Disadvantages of YAML

  • Statelessness: It is not able to store data, and therefore sometimes it can be difficult to orchestrate when different versions are encountered.
  • Limited Expressiveness: It may not support all features of programming languages and therefore has limitations.
  • Whitespace: Extra or absence of it , can cause errors which are sometimes difficult to spot. Indentation plays a vital role.

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

  • Playbook => install_apache.yml
---
- 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"
  • hosts:
    • all: Specifies that the playbook should be applied to all hosts defined in the inventory. The all keyword refers to all hosts in the inventory.
  • apt: Ansible module used to manage packages. It is used here to install Apache2 and PHP packages.
  • name: Specifies the name of the packages to be installed. Multiple package names can be provided as a list.
  • update_cache:
    • yes: This parameter tells Ansible to update the package cache before installing or upgrading packages. This ensures that Ansible has the latest information about available packages.
  • Inventory ⇒ hosts

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

  • Command
ansible-playbook install_apache.yml -u client -i inventory --ask-become-pass ;
  • -u is for target node we are logging in
  • -i is for inventory files which is hosts
  • —ask-become-pass is for prompting users to put password for the target node

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

Output

  • Output when the playbook was run for first time

Notice the change

  • Output when the playbook is run again

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.



Contact Us