What is Ansible - YAML Basics
What is Ansible
Ansible is an open-source software tool that provides simple and powerful automation to support cross-platform computing. It is primarily intended for IT professionals who use it for application deployment, workstation and server updates, cloud deployment, configuration management, and almost anything that system administrators do weekly or daily.
Ansible is easy to deploy because it does not rely on agent software and has no additional security infrastructure.
Ansible is at the forefront of automation, systems management, and DevOps, but it’s also a valuable tool for developers to use in their day-to-day operations.
Ansible playbook
An Ansible playbook is a blueprint of automation tasks—which are complex IT actions executed with limited or no human involvement.
Ansible playbooks are executed on a set, group, or classification of hosts that make up an Ansible inventory.
Understanding YAML basics
YAML stands for Yet Another Markup Language.
Ansible uses YAML syntax to express Ansible playbooks. Because compared to other data formats such as XML and JSON, it is very easy for people to understand, read, and write.
Below are the different ways in which YAML data is expressed
Key-value pair
YAML uses key-value pair to represent data. A key-value pair (KVP) is a set of two related data items:
- Key: A unique identifier for a data item
- Value: The identifiable data or pointer to the location of the data.
Each YAML file optionally starts with "---" and ends with "...".
Example
Let’s look at the custom record in YAML below:
--- #Optional YAML start syntaxAngela:name: angela marktx_ref: poolu-tx-6720bbtyttuage: 25sex: female… #Optional YAML end syntax
Note: There should be space between the colon(:) and the value.
The above example can also be expressed in dictionary form as follows:
Angela: {name: angela mark ,tx_ref: poolu-tx-6720bbtyttu, age: 24, sex: female}
Representing a list in YAML
We can also represent Lists in YAML. Each list element must be written on a new line with the same indentation starting with "-" and a space.
Example
Let’s look at the list of football teams in YAML below:
---teams:- Psg- Chelsea- Arsenal- Juventus…
The above example can also be expressed in the form below:
Countries: [‘Psg’, ‘Chelsea’, ‘Arsenal’, ‘Juventus’]
List in dictionary
We can use the list in the dictionary, that is, key-value is a list.
Example
Let’s look at the code below:
---Angela:name: angela marktx_ref: poolu-tx-6720bbtyttuage: 25sex: femalelikes:- fruits- pasta- sausages…
The above example can also be expressed in the form below:
{Angela: {name: angela mark ,tx_ref: poolu-tx-6720bbtyttu,age: 25, sex: female, likes:['fruits', 'pasta', 'sausages']}}
List of dictionaries
We can also create a list of dictionaries.
Example
Let’s look at the code below:
---- Angela:name: angela marktx_ref: poolu-tx-6720bbtyttuage: 25sex: femalelikes:- fruits- pasta- sausages- Thomas:name: thomas mullertx_ref: taifr-tx-8695jjtyjfnage: 30sex: malelikes:- soccer- music- dancing…
In conclusion, to include newline while displaying multiple lines, YAML uses "|", and also ">" to subdue new lines. Because of this, we can read and edit large lines. In both cases, the indent will be ignored.
jobs:build:runs-on: ubuntu-lateststeps:- uses: actions/checkout@v2- name: Set up Python 3.9.5uses: actions/setup-python@v2with:python-version: 3.7- name: Install dependenciesrun: |python -m pip install --upgrade pippip install -r requirements.txt- name: Coverage reportrun: |pip3 install coveragecoverage run mysite_rebrand/manage.py testcoverage reportrun: >pip3 install coveragecoverage run mysite_rebrand/manage.py testcoverage report
Boolean values can also be represented in YAML as true/false and should be case-sensitive.