Understanding YAML: Key-Value Pair

Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 5

$ sudo apt-get update

$ sudo apt-get install software-properties-common


$ sudo apt-add-repository ppa:ansible/ansible $ sudo apt-get update
$ sudo apt-get install ansible

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.

YAML Syntax

--- #Optional YAML start syntax


james:
name: james john
rollNo: 34
div: B
sex: male
… #Optional YAML end syntax

We can also use like this


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%

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

Example:

Sample Playbook for apache installation


---
- hosts: apache

sudo: yes

tasks:

- name: install apache2

apt: name=apache2 update_cache=yes state=latest

- name: enabled mod_rewrite

apache2_module: name=rewrite state=present


notify:

- restart apache2

handlers:

- name: restart apache2

service: name=apache2 state=restarted

Examples:
---
- hosts: 127.0.0.1
connection: local
tasks:
- name: ensure nginx is at the latest version
apt: name=nginx state=present
- name: start nginx
service:
name: nginx
state: stopped
...

You might also like