Advertisement

Responsive Advertisement

Introduction to ansible

 Ansible Ad-hoc commands:

These are one-liner command to perform a single task on the target host. These commands are present in /usr/bin/ansible

Tasks like pinging all the hosts to check if they are running, copying a file, rebooting servers, installing a package can be easily done through Ansible Ad-hoc Commands. Here is a list of fundamental Ansible Ad-hoc commands which you must know.

Synatx: $ ansible [pattern] -m [module] -a "[module options]"

Eg 1: $ansible <hostname> -m ping 

In the above example, <hostname> can be individual hostname or group name or all inside the inventory file and -m is stands for module and -a stands for attribute.

Eg 2: $ansible <hostname> -m yum -a "name=tree state=present"

Above ad-hoc command installs tree package in remote machines.

References:

Ref1

Ref2

 Ansible Playbooks:

Ansible Sample Playbooks structure with basic example:

apache.yml

---

hosts: "linux"

vars:

    packagename: "httpd"

tasks:

- name: "Install Apache web server"

  yum

    name: "{{ packagename }}"

    state: "latest"

Things to remember:

  • YAML files always start with "---"
  • Indentations should be followed carefully even space miss leads to syntax error.
  • Perform syntax check before executing yaml script using below syntax:

            $ansible-playbook <yaml_filename> --syntax-check

Playbooks mainly consists of 4 important parts:

1.hosts:

This is mandatory and it should match the name in inventory file as it species in which remote server does the tasks mentioned in this script needs to perform.

Linux in above example is hostname, it should be  mentioned in inventory file.

2.vars:

This is optional.we can specify variables here, and those can be used any where in the playbook using {{ variable_name }}.

packagename in example is variable

3. tasks:

This is mandatory and we need to specify actual tasks here with any name and modules as per ansible documentation.

Installing apache server task in above example under tasks.

4.handlers:

This is optional and this is like a task, it only executes when any task notifies it. we will more about handlers later.

Inventory file:

This is very important file, as it stores all ip address or domain names of all servers which ansible master wants to manage. 
The default location of this file is /etc/ansible/hosts and if you want to change you can do so by editing ansible.conf.
The inventory file can be in yaml or ini 
If you use inventory file in other than default location, then you need to specify while executing ansible ad-hoc command and ansibleplaybook like below:

Adhoc commands:
$ansible -i <inventory_filename> <host/group name> -m <module> -a <attribiutes>
Eg:$ansible -i inventory production -m yum -a "name=tree state=present"

Ansible Playbook:
$ansible-playbook -i <inventory_filename> <host/group_name> <yaml_file>
Eg: $ansible-playbook -i hosts dbservers sample.yml

References:

Ansible Tags:

Ansible tags are used to run only one or some specific tasks from a large playbook instead of running the whole playbook. We use “tags:” attribute to use Ansible tags. 

For example, we have 4 tasks in a playbook, the first one is to add epel repo & 2nd task is to install the package, 3rd task is to set the configuration and the final is to check the status of the installed package like installed service is running and it is enabled, etc. however, we want to check only the status of the packages on some hosts, we use Ansible tags in this situation to run that specific tasks rather than running the all tasks in the playbook. It helps us to keep all the related tasks in one playbook and save the execution time as it provides the capability to run the specific task only.

Example:tags_demo.yml
---
- name: Install nginx
  hosts: {{ any_host_name }}
  become: true

  tasks:
  - name: Add epel-release repo
    yum:
      name: https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
      state: present
    tags:
    - epelrepo

  - name: Install nginx
    yum:
      name: nginx
      state: present
    tags:
    - install

  - name: Insert Index Page
    template:
      src: index.html
      dest: /usr/share/nginx/html/index.html
    tags:
    - configchange

  - name: Start NGiNX
    service:
      name: nginx
      state: started
    tags:
    - servicestatus

If we want to check status of nginix server on any remote host, we don't need to run entire play book. Just run the 4th tag with name "servicestatus" as below:
$ansible-playbook  tags_demo.yml -t servicestatus
                  or 
$ansible-playbook tags_demo.yml --tags servicestatus
                or
$ansible-playbook tags_demo.yml --tags "servicestatus"
                or
$ansible-playbook tags_demo.yml --tags="epelrepo"

Similarly we want to run all tasks in a play book except one or 2, then we can use --skip-tags inplace of --tags
$ansible-playbook tags_demo.yml --skip-tags="epelrepo"

References:

Notifier and Handlers:

Refer this for theory

Example:
---
- name: Verify apache installation
  hosts: slave2
  remote_user: root
  tasks:
  - name: Install httpd
    yum:
      name: httpd
      state: latest

  - name: Change welcome page
    copy:
      src: index.html
      dest: /var/www/html/index.html
    notify:
    - Restart apache

  - name: Ensure apache is running
    service:
      name: httpd
      state: started

  handlers:
    - name: Restart apache
      service:
        name: httpd
        state: restarted

In above example, handler "Restart Service" will run if and only if 2nd task i.e  "Change welcome page" is changed, since we defined notify under it.

Ansible Variables:

There are more than 21 ways to define and use variables in ansible:
The most common ways are below in precedence order low to high:
1.In default directory of roles.
2.Group vars 
3.Host vars
4.In vars directory of roles
5.Vars in a separate file  
6.Extra vars 

order of precedence for vars:

  • role defaults [1]
  • inventory file or script group vars [2]
  • inventory group_vars/all [3]
  • playbook group_vars/all [3]
  • inventory group_vars/* [3]
  • playbook group_vars/* [3]
  • inventory file or script host vars [2]
  • inventory host_vars/*
  • playbook host_vars/*
  • host facts / cached set_facts [4]
  • inventory host_vars/* [3]
  • playbook host_vars/* [3]
  • host facts
  • play vars
  • play vars_prompt
  • play vars_files
  • role vars (defined in role/vars/main.yml)
  • block vars (only for tasks in block)
  • task vars (only for the task)
  • include_vars
  • set_facts / registered vars
  • role (and include_role) params
  • include params
  • extra vars (always win precedence)
1.Group vars:
If you want to use a set of variables for more than 1 host, then in inventory file group all hosts and use same name file as shown in below example.
Eg:Create a group in invenory file
----------------------------------------------
[groupA]
host1 ansible_ssh_host=192.168.116.139
host2 ansible_ssh_host=192.168.116.137
-----------------------------------------------

Then create group_vars/groupA/main.yml file and create variables here like below:
ansible_user=administrator
ansible_password=bmtw8199S@
ansible_connection=winrm
ansible_winrm_server_cert_validation=ignore

You don't need to specify this file location in playbook, ansible will automatically pick it.

2.Host vars:
These have higher precedence than group vars.
The directory structure was host_vars/host1/main.yml
This is used if you want to create variables specific to host.
If you define same varaibles in group vars and host vars, it will override group vars variable values since it has more precedence.

Note:If you want to define variable names for host level and group level, you can do so in host file it seld, but it will not look good when size of project increases.
For group level vars:
---------------------------------------------------
[groupA]
host1 ansible_ssh_host=192.168.116.139
host2 ansible_ssh_host=192.168.116.137
[groupA:vars]
ansible_user=administrator
ansible_password=bmtw8199S@
ansible_connection=winrm
ansible_winrm_server_cert_validation=ignore
------------------------------------------------------
For host level vars:
--------------------------------------------------------------------
host1 ansible_ssh_host=192.168.116.139 ansible_user=Admin
---------------------------------------------------------------------

3.Inside default and vars folder in roles:
The variables defined inside default folder can be changed easily as they has low precedence and you can also define variables inside vars folder in roles.

4.Vars in a separate file  
you can declare vars in a seperate file and they can be used in a playbook, these should be defined in playbook before play, in which play you are about to use those.

Ed:I have created vars files in below location /etc/ansible/vars/vars_filename.yml(absolute path) and relative path is vars/vars_filename.yml

Eg:
----------------------------------------
---
- hosts: {{ any name }}
  vars_files:
      - vars/vars_filename.yml
 tasks: your task here

-----------------------------------------
5.Extra vars:
These variables have higher priority and are passed in command line when running play books as below:
Eg: Lets say your playbook has a variable named "dummy" if you want to give it a value "final", then you can pass using extra vars it override all previous values as it has most precedence.

$ansible-playbook <yaml file name> --extra-var dummy=final
                                   or
$ansible-playbook <yaml file name> -e dummy=final
 
For passing multiple values:
$ansible-playbook <yaml file name> --extra-var "dummy=final second_dummy=final2"

Ansible Vault:

If you want to use any sensitive information like passwords in playbook, then encrypt file using ansile vault so that when you open the file without password it will show encrypted data:

1.To encrypt file which is already created in ansible:
$ansible-vault encrypt <file name>
It will prompt for password enter it and if any one opens this file without password then dummy encrypted test will be displayed.

2.To  create a file  with encryption:
$ansible-vault create <file name>
It will prompt for password enter it and if any one opens this file without password then dummy encrypted test will be displayed.

3.To decrypt file:
$ansible-vault decrypt <file name>
Enter the password which you used while encrypting.

4.To edit or open file with out decrypting
$ansible-vault  edit <file name>
$ansible-vault  view <file name>

5.To decrypt file in runtime while executing playbook
You can provide enter password or store password in a file and specify file location.
$ansible-playbook <filenamme.yml> --ask-vault-pass
$ansible-playbook <filename.yml> --vault-password-file=/location/of/file

Click here for more

Ansible Roles:

Click here























Post a Comment

0 Comments