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
Abbreviation- To represent dictionaries, you can also use abbreviations.
Example:
● 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).
Abbreviation- You can also use the abbreviation to represent lists.
Example:
● List inside Dictionaries
A list can also be used within dictionaries, i.e., a value a key is a list..
Example:
● List of Dictionaries
We can also make a list of dictionaries.
Example:
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:
● 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.
To run reboots for all your company servers in a group, 'abc', in 12 parallel forks −
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: −
- 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
- Creating a new directory
- Deleting whole directory and files
● 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.
The following command checks the package is not installed.
The following command ensures that the most recent version of the package is installed.
● 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 −
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