Thursday, 6 May 2021

Yaml for Ansible - Part 1

 YAML:

stands for Yet Another Mark up language.

  • is a data serialization language designed to be readable and writable by humans
  • it is commonly used for configuration files but it is used in many apps where data being stored and transmitted over the network.
  • We can communicate with another language too by using Yaml including OS and or services/apps running on OS.
  • Yaml is case sensitive scripting language
  • In Yaml , it doen't allow Tabs for indentation , you have to use spaces instead
Keys:
Key is used to store any value and that can be changed depending on condition in any other language.
For ex: x: 10

Sequence data collection:

this is same as array in another language , like list of same data type values. We can represent as below

sequence representation

name:
- value1
- value2

Here name is array name, value1, value2 are items of array.
you can also represent as below

name: [value1,value2]

Map Data collection

It is same definition as in Java

Map representation

family:
 krishh: 20
 ram: 10

you can also add sequence data inside Maps as below

family:
krishh: 20
sachin: 45
 - fullname: Ramesh Sachin Tendulkar
 - age: 50
 - score: 2000
rohit: 40
Dhawan: 46

sequences are ordered collections and Maps are unordered collections

Ansible Play book

Playbook is used to send commands to remote servers / execute commands in remote servers in a scripted way using ansible commands.

To understand playbooks we need to know the below.
  • Task
    • Install tomcat in remote server
      • yum install tomcat -y
  • Play
    • collection of tasks is called Play 
      • Install java
      • Install tomcat
      • create file in home directory
  • playbook
    • collection of Plays
      • it a combination of Play and Tasks.
      • Here we can write plays for one server or multiple servers.
  • Playbooks
    • More than one play book means more than one yml file you are writing for your requirement.

How to write Ansible playbook

  1. start with   --- (3 - ) at the start of the yml file , it means that you are writing yml file.
  2. Target section list (hosts,users etc)
  3. variables list (optional)
  4. Tasklist
    1. list all the modules that you run in the order
These are the steps we need to write in yaml or yml file and we call it this for one play.

Sample.yml


Yaml file contains sequences and each sequence is called one play.

If you are writing playbook for Ansible , hosts is the default key , where you are providing servers group name.


How to write yaml sccript 


 
first.yml : write this yml file in ansible server
---
  - hosts: ansible-servers
    vars:
      mycontent: "this file is created using var content"
    tasks:
     - copy:
        dest: /tmp/var_file.txt
        content: "{{ mycontent }}"


execute this command in ansible server:
ansible-playbook first.yml

Now open ansible host and check for file in the location : /tmp/var_file.txt.

It will be created as below

else you can execute below command to check without manually opening the file in client
ansible ansible-servers -m command -a "cat /tmp/var_file.txt ".

So that you can see the below content for the same.


Reading vars from command line using vars_prompt

Write yml file as below to test this.

read.yml
---
 - name: this is play1
   hosts: localhost
   vars_prompt:
    name: var1
    prompt: enter any value ?

and execute yml file : ansible-playbook read.yml

Note: the value which you are entering will be assigned to the variable which is in yml file.

Print variable value using command line

For this use debug module in playbook, debug module has one argument as msg.

read.yml
---
 - name: this is play1
   hosts: localhost
   vars_prompt:
    name: var1
    prompt: enter any value ?
   tasks:
- name: "this is to print the variable value"
   debug:
   msg: "this is the value you entered {{ var1 }} "


output as below


suppose if you are not assigning or entering any value , default value it set as "Hello world".

Using loops

Lets see an example without loops , how we need to create 3 siilar directories by using command and name in yml

---
  hosts: localhost
    tasks:
    - name: this will create dir1
      command: mkdir /tmp/dir1
    - name: this will create dir2
      command: mkdir /tmp/dir2
    - name: this will create dir3
      command: mkdir /tmp/dir3

once you execute above yml file, it will create 3 dirs under tmp folder,.

instead of executing same command multiple times, we can use loops concept here for easy purpose.

---
 - hosts: ansible-servers
   tasks:
   - name: this is to create dirs
     command: mkdir /tmp/"{{ item }}"
     with_items:
     - new_dir1
     - new_dir2
     - new_dir3

output in /tmp of ansible client server after executing above playbook


Now , lets see how we can pass variable while running playbook and set those varaible values inside if your plabook

ansible-playbook read.yml -e "var1= hi var2=bye "

---
- name: this is a play
    hosts: localhost
- tasks:
- debug:
    msg: "the value of var1= {{ var1 } } and var2={{ var2 }} "

When you execute above script , it will assign values what you entered.

Conditional statements : 




Use when keyword to give a condition to execute.


when above file executed , when_file.txt will be created , if you make when: false , it won't be created.       

Yaml modules: deligate_to and register

register:
module is used to store the response to a variable after completion of each task.
if you want to store the response of each task after completion , we can use register at the end of the task.

this module is very helpful in conditional statements.


register.yml

---
 - name: This is a play
   hosts: ansible-servers
   tasks:
   - command: touch /tmp/register_module.txt
     register: response_out
   - debug:
      msg: "{{response_out}}"

output :

module : delegate_to

This module will help us in executing task only servers/hosts which are given as parameter to delegate_to parameter .

for ex:

---
 - name: This is a play
   hosts: ansible-servers
   tasks:
   - command: touch /tmp/register_module.txt
     delegate_to: localhost 

here though you pass hosts: ansible-servers , the file won't create here instead of that it will create in localhost or the server which you given an argument to delegate_to.

Ansible Playbook to install vim and wget on client machines.

---
 - hosts: ansible-servers
   become: yes
   tasks:
    - yum:
       name: wget
       state: present
    - yum:
       name: vim
       state: present


Ansible automation is designed to fulfil the below requirements
  • provisioning
  • configuration
  • application deployment
  • orchestration

1 comment:

  1. Can you please explain about Roles in Ansible. Thanks in advance.

    ReplyDelete