Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ansible
open source
communitycreator

What is Ansible - YAML Basics

henry anorue

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 syntax 
Angela: 
   name: angela mark 
   tx_ref: poolu-tx-6720bbtyttu 
   age: 25
   sex: female
… #Optional YAML end syntax 
Customer record in YAML

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 
…
List of Football Teams

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 mark 
    tx_ref: poolu-tx-6720bbtyttu 
    age: 25
    sex: female 
    likes: 
        - fruits
        - pasta
        - sausages 
… 
List in dictionary

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 mark 
    tx_ref: poolu-tx-6720bbtyttu 
    age: 25
    sex: female 
    likes: 
        - fruits
        - pasta
        - sausages 

- Thomas: 
    name: thomas muller 
    tx_ref: taifr-tx-8695jjtyjfn 
    age: 30
    sex: male 
    likes: 
        - soccer
        - music
        - dancing 
… 
List of Dictionaries

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-latest
    steps:
    - uses: actions/checkout@v2
    - name: Set up Python 3.9.5
      uses: actions/setup-python@v2
      with:
        python-version: 3.7
    - name: Install dependencies
      run:  |  
        python -m pip install --upgrade pip
        pip install -r requirements.txt
    - name: Coverage report
      run:  |
        pip3 install coverage
        coverage run mysite_rebrand/manage.py test
        coverage report

      run:  >
        pip3 install coverage
        coverage run mysite_rebrand/manage.py test
        coverage report
The use of "|" and ">" in YAML file

Boolean values can also be represented in YAML as true/false and should be case-sensitive.

RELATED TAGS

ansible
open source
communitycreator
RELATED COURSES

View all Courses

Keep Exploring