Understanding YAML: Key-Value Pair
Understanding YAML: Key-Value Pair
Understanding YAML: Key-Value Pair
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.
YAML Syntax
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
Example
Countries: [‘America’, ‘China’, ‘Canada’, ‘Iceland’]
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%
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.
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"
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:
sudo: yes
tasks:
- restart apache2
handlers:
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
...