USD ($)
$
United States Dollar
India Rupee

Understanding Ansible YAML Basics

Created by Deepak Sharma in Articles 23 Jun 2024
Share
«How to Configure a Banner in Cisco ...

Introduction to Ansible - YAML Basics

Are you aware of Ansible? In this blog we will dive into the fascinating world of Ansible and discover how it employs the user-friendly YAML syntax to craft its playbooks. This post is enough to get basic level of understanding about Ansible and YAML. I'll attempt to explore in more detail why Ansible choose it over file types like XML and JSON in this post. YAML, with its simplicity and human-readability, becomes your trusty sidekick in the world of automation. Do you know each YAML file starts with “---” and ends with “...”.

Understanding YAML

In this section, we'll look at the various ways YAML data can be represented.

● key-value pair

To express data, YAML employs basic key-value pairs. The dictionary is represented as a pair of keys and values. Please keep in mind that there should be a space between: and value.

Example: A student record

banner image

Abbreviation- To represent dictionaries, you can also use abbreviations.

Example:

banner image

● Representing List

We can also represent List in YAML. Every list element (member) should be typed on a new line with the same indentation, beginning with "- " (- and space).

banner image

Abbreviation- You can also use the abbreviation to represent lists.

Example:

banner image

● List inside Dictionaries

A list can also be used within dictionaries, i.e., a value a key is a list..

Example:

banner image

● List of Dictionaries

We can also make a list of dictionaries.

Example:

banner image

YAML employs the symbol "|" to include newlines when displaying multiple lines and the symbol ">" to suppress newlines when displaying multiple lines. As a result, we can read and edit long lines. The indentation will be ignored in both circumstances. In YAML, we may also encode Boolean (True/False) values, which can be case insensitive.

Example:

banner image

● Some common words related to Ansible.

Service/Server − A machine process that offers the service.

Machine − A physical server, virtual computer (VM), or container.

Target machine − A machine that will be configured using Ansible.

Task − An Ansible-managed operation (for example, run this, delete that).

Playbook − The YML file that contains Ansible commands and is run on a system.

Ansible.cfg – ansible configuration file 

Inventory File – a file that contains all the remote ansible nodes.

● Ansible - Ad hoc Commands

Ad hoc commands are commands that can be run on their own to conduct brief tasks. These commands do not need to be executed afterwards. For example, you must restart all of your company's servers. You will use the Adhoc commands from '/usr/bin/ansible' for this.

Because these ad-hoc instructions are only used once, they are not used for configuration management or deployment. Ansible-playbook is used to manage and deliver configuration.

- Shell Commands and Parallelism

Restart your enterprise server in 12 parallel forks. For this, we must configure an SSH agent for connection.

banner image

To run reboots for all your company servers in a group, 'abc', in 12 parallel forks −

banner image

Ansible will run the aforementioned Ad-hoc commands from the current user account by default. If you want to change this behaviour, use the following Ad-hoc instructions to pass the username: −

banner image

- File Transfer

Ad-hoc commands can be used to SCP (Secure Copy Protocol) numerous files in parallel across several workstations. File transfer to many servers/machines

banner image

- Creating a new directory

banner image

- Deleting whole directory and files

banner image

● Managing Packages

Ad-hoc commands for yum and apt are available. The following are some yum ad-hoc commands. The following command determines whether a yum package is installed but does not update it.

banner image

The following command checks the package is not installed.

banner image

The following command ensures that the most recent version of the package is installed.

banner image

● Gathering Facts

Facts can be utilized in playbooks to build conditional statements. You can discover ad hoc information on all of your facts by using the links below. Command on the fly −

banner image


REST-Based APIs: Definition & Examples»
Deepak Sharma

He is a senior solution network architect and currently working with one of the largest financial company. He has an impressive academic and training background. He has completed his B.Tech and MBA, which makes him both technically and managerial proficient. He has also completed more than 450 online and offline training courses, both in India and ...

More... | Author`s Bog | Book a Meeting

Related Articles

#Explore latest news and articles

How to Learn Network Automation: Starter 23 Jul 2024

How to Learn Network Automation: Starter

Effective ways on how to learn network automation using Python for network automation engineers and start a journey to master this essential skill.
What is Machine Learning in AI? Explored 6 Jan 2024

What is Machine Learning in AI? Explored

Basics of what is Machine Learning in AI means. Understand the concepts, and its relation to artificial learning.
Network Automation is Essential for Engineers 22 Jun 2024

Network Automation is Essential for Engineers

Discover the crucial role of network automation for network engineers. Learn why automation is essential for efficiency and competition in the networking field.

Comments (0)

Share

Share this post with others

Contact learning advisor

Captcha image