Overview of YAML and Ad-hoc commands in Ansible

Last updated on Nov 11 2021
Nitin Bajabalkar

Table of Contents

Overview of YAML and Ad-hoc commands in Ansible

Ansible – YAML Basics

Ansible uses YAML syntax for expressing Ansible playbooks. This blog provides an overview of YAML. Ansible uses YAML because it is very easy for humans to understand, read and write when compared to other data formats like XML and JSON.

Every YAML file optionally starts with “—” and ends with “…”.

Understanding YAML

In this section, we will learn the different ways in which the YAML data is represented.

key-value pair

YAML uses simple key-value pair to represent the data. The dictionary is represented in key: value pair.

Note − There should be space between : and value.

Example: A student record

--- #Optional YAML start syntax
james:
name: james john
rollNo: 34
div: B
sex: male
… #Optional YAML end syntax
Abbreviation
You can also use abbreviation to represent dictionaries.
Example
James: {name: james john, rollNo: 34, div: B, sex: male}

Representing List

We can also represent List in YAML. Every element(member) of list should be written in a new line with same indentation starting with “- “ (- and space).

Example

countries:

– America

– China

– Canada

– Iceland

Abbreviation

You can also use abbreviation to represent lists.

Example

Countries: [‘America’, ‘China’, ‘Canada’, ‘Iceland’]

List inside Dictionaries

We can use list inside dictionaries, i.e., value of key is list.

Example

james:

name: james john

rollNo: 34

div: B

sex: male

likes:

– maths

– physics

– english

List of Dictionaries

We can also make list of dictionaries.

Example

– james:

name: james john

rollNo: 34

div: B

sex: male

likes:

– maths

– physics

– english

 

– robert:

name: robert richardson

rollNo: 53

div: B

sex: male

likes:

– biology

– chemistry

YAML uses “|” to include newlines while showing multiple lines and “>” to suppress newlines while showing multiple lines. Due to this we can read and edit large lines. In both the cases intendentation will be ignored.

We can also represent Boolean (True/false) values in YAML. where boolean values can be case insensitive.

Example

– james:

name: james john

rollNo: 34

div: B

sex: male

likes:

– maths

– physics

– english

 

result:

maths: 87

chemistry: 45

biology: 56

physics: 70

english: 80

 

passed: TRUE

 

messageIncludeNewLines: |

Congratulation!!

You passed with 79%

 

messageExcludeNewLines: >

Congratulation!!

You passed with 79%

Some common words related to Ansible.

Service/Server − A process on the machine that provides the service.

Machine − A physical server, vm(virtual machine) or a container.

Target machine − A machine we are about to configure with Ansible.

Task − An action(run this, delete that) etc managed by Ansible.

Playbook − The yml file where Ansible commands are written and yml is executed on a machine.

 Ansible – Ad hoc Commands

Ad hoc commands are commands which can be run individually to perform quick functions. These commands need not be performed later.

For example, you have to reboot all your company servers. For this, you will run the Adhoc commands from ‘/usr/bin/ansible’.

These ad-hoc commands are not used for configuration management and deployment, because these commands are of one time usage.

ansible-playbook is used for configuration management and deployment.

Parallelism and Shell Commands

Reboot your company server in 12 parallel forks at time. For this, we need to set up SSHagent for connection.

$ ssh-agent bash
$ ssh-add ~/.ssh/id_rsa
To run reboot for all your company servers in a group, 'abc', in 12 parallel forks −
$ Ansible abc -a "/sbin/reboot" -f 12

By default, Ansible will run the above Ad-hoc commands form current user account. If you want to change this behavior, you will have to pass the username in Ad-hoc commands as follows −

$ Ansible abc -a "/sbin/reboot" -f 12 -u username

File Transfer

You can use the Ad-hoc commands for doing SCP (Secure Copy Protocol) lots of files in parallel on multiple machines.

Transferring file to many servers/machines
$ Ansible abc -m copy -a "src = /etc/yum.conf dest = /tmp/yum.conf"
Creating new directory
$ Ansible abc -m file -a "dest = /path/user1/new mode = 777 owner = user1 group = user1 state = directory"
Deleting whole directory and files
$ Ansible abc -m file -a "dest = /path/user1/new state = absent"

Managing Packages

The Ad-hoc commands are available for yum and apt. Following are some Ad-hoc commands using yum.

The following command checks if yum package is installed or not, but does not update it.
$ Ansible abc -m yum -a "name = demo-tomcat-1 state = present"
The following command check the package is not installed.
$ Ansible abc -m yum -a "name = demo-tomcat-1 state = absent"
The following command checks the latest version of package is installed.
$ Ansible abc -m yum -a "name = demo-tomcat-1 state = latest"

Gathering Facts

Facts can be used for implementing conditional statements in playbook. You can find adhoc information of all your facts through the following Ad-hoc command −

$ Ansible all -m setup

So, this brings us to the end of blog. This Tecklearn ‘Overview of YAML and Ad-hoc commands in Ansible’ blog helps you with commonly asked questions if you are looking out for a job in DevOps. If you wish to learn Ansible and build a career in DevOps domain, then check out our interactive, Configuration Management using Ansible Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/configuration-management-using-ansible/

Configuration Management using Ansible Training

About the Course

Tecklearn has specially designed this Configuration Management using Ansible Training Course to advance your skills for a successful career in this domain. The course will cover different components of Ansible and how they are used in software development operations.  You will get an in-depth knowledge of these concepts and will be able to work on related demos. Upon completion of this online training, you will hold a solid understanding and hands-on experience with Ansible.

Why Should you take Configuration Management using Ansible Training?

  • The average Ansible salary in USA is $136,500 per year or $70 per hour. Entry level positions start at $100,000 per year while most experienced workers make up to $187,500 per year.
  • Wells Fargo, Capital One, Apple, CISCO & many other MNC’s worldwide use Ansible across industries.
  • According to Grand View Research, the DevOps market size is estimated to be worth $12.85 billion by 2025. DevOps professionals are highly paid and in-demand throughout industries including retail, eCommerce, finance, and technology.

What you will Learn in this Course?

Introduction to DevOps

  • What is Software Development
  • Software Development Life Cycle
  • Why DevOps?
  • What is DevOps?
  • DevOps Lifecycle
  • DevOps Tools
  • Benefits of DevOps
  • How DevOps is related to Agile Delivery
  • DevOps Implementation

Configuration Management using Ansible

  • What is Ansible?
  • Ansible vs Puppet
  • Ansible Architecture
  • Ansible Installation
  • Setting up Master Slave using Ansible
  • Configuring Ansible Roles
  • Write Playbooks
  • Applying configuration using Ansible
  • Hands on

Got a question for us? Please mention it in the comments section and we will get back to you.

0 responses on "Overview of YAML and Ad-hoc commands in Ansible"

Leave a Message

Your email address will not be published. Required fields are marked *