How to Work With Ansible?

Ansible

Table of Contents

Share and Enjoy !

Ansible
Ansible

The Ansible tutorials will cover the following topics.

  • What is Ansible?
  • How to install Ansible?
  • The Ansible architecture
  • Configuration of Ansible
  • How to work with Playbook in Ansible?
  • How to work with Inventory Files in Ansible?
  • How to work with modules in Ansible?
  • How to develop test and release a playbook?
  • How to take Ansible to Production?
  • How to handle Errors in Ansible?
  • How to Rollback in Ansible?
  • How to work with Reporting in Ansible?
  • How to create a custom module in Ansible?
  • How to create provisioning for Docker?
  • How to deploy Ansible in large organization?
  • What is Ansible tower?

What is Ansible?

Ansible is a very simple IT automation engine that automate provisioning ,configuration, application deployment, intra service orchestration whose aim is to provide enlarge productivity. It is written in Python and can be installed as Linux machine only.

Ads code goes here

Ansible is the infrastructure automation tool. It can set up several built agents in continuous integration(CI) system. Using ansible building or rebuilding infrastructure is easy, fast and error free. All setup can be done by just one click. Ansible provides a fuel towards devops journey.The primary aim of configuration management is to provide  machines to a denied state as fast as possible.

Other infrastructure automation tools are-

  • Chef
  • Puppet
  • Satt
  • etc

Infrastructure testing tools are: –

  • Serverspce
  • Test kitchen
  • etc.

Provisioning tools are: –

    • Docker
    • Lxca
    • Vagrant
    • etc

Deployment tools are:-

  • Thoughtworks Go
  • Atlassion bamboo
  • Jenkins
  • etc

Orchestration tools are

  • Mcollective
  • Satt
  • Serf
  • Chef

Monitoring tools are:-

  • Nagi OS
  • Ganglia
  • Zenoss
  • Graphite
  • Sensu
  • Riemann

Logging tools are:-

  • Logstash-kibana
  • Sumologic
  • Psyslog

Ansible needs to be installed on a single machine which will manage your whole infrastructure.For remaining clients machine either we need their hostname or IP address.

Client server-Ansible
Client server-Ansible

Push and pull based mechanism

There are two mechanisms available.Pull based mechanism and push based mechanism.

Pull based

In pull based mechanism,client will contact the server and check if any config applicable for the machine.If any, server will provide the client with the configuration or other details.

Puppet and chef follow pull based mechanism.Agent on the server periodically checks for the configuration information from central server.

Pull based
Pull based
Push based

In push based mechanism, server will push the applicable config/or other details to client if applicable without the agent.

push based mechanism
push based mechanism

Ansible follows push based mechanism.Central server pushes the configuration information on target client.

Normal/ pull based
ServerClient
Responsible for providing the serviceAccess the server
Need some utility to access the server
Ansible/ push based
Ansible server config changes in serverNo need to install some utility to access the server
The Architecture of Ansible
Agent Based SystemAgentless  system
These kind of system needs an external agent along with the dependencies.No agent is required only a proper SSH daemon setup is required.
Agent based systems need to invoke the agent in order to run and pull the latest configuration .Either the agent runs as service or cron job.These system pushes the configuration remotely without the agent.
Parallel agents execution may slow down the server. Serverless execution can make the process faster.Parallel execution is faster as long as parallel execution count is lesser than SSH connection allowed count.
Agents installation and permissions are headache.Only SSH based remote connection is required.

Puppet needs agent on puppet server .Agent program needs some space and CPU prioritization.

Ansible by default runs on push mode but using ansible –pull, ansible provides an agent that can work as pull mode.To make default SSH connections faster, we can always enable control persist and pipeline mode.

Tools like chef, puppet are agent based and they by default work on pull mode. Using serverless chef and master less puppet we can scale up large machines.

Ansible architecture

Ansible architecture
Ansible architecture

Ansible tasks defined in the playbooks can execute sequentially by default. However, we can insert conditions and loops so that selective execution can happen.Ansible provides set of API s to run a script. Mostly when we create our own task set we call these APIs with the help of custom wrappers.All playbooks are written in YAML language. They are simple and declarative.Ansible can execute N threads in parallel.

Features of Ansible

  • Open source
  • Written on python so easy to read and extend(Built on top of python)
  • Easy installation and configuration
  • Highly scalable
  • Agent less client connection
  • SSM for secure connection
  • Simple English Language confirmation setup

Who uses Ansible

If we want to push one software or patch or want to install some packages. Manual installation is very tedious and time consuming.They are error prone too. To simplify the process we can use Ansible.These activities can be done very easily by ansible script/command/playbook.

Below are the guys who uses Ansible

  • System administrator
  • Data engineers
  • Devops Professionals
  • Developers
  • Testers
  • Database administrator
  • Network administrator

How to install Ansible?

Ansible software is available in EPEL repositories.Ansible does not come with normal unix or linux distribution.So we need to activate epel.In order to install Ansible, yum will try to contact internet and download the required software and other dependency files.

Prerequisites of Ansible

  • SSH client(Open SSH)
  • Python(PyYaml,jinja2)
  • Paramiko
  • Vagrant
  • Serverspce
  • PIP
  • Git
  • httplib2

Ansible system requirements for clients(node)

  • 2 GB RAM
  • 20 GB Hard disk
  • SSH(OpenSSH)
  • os-RHEL/CENTOS/UBUNTU/ORACLE LINUX/HAC/BSD/Solaris/Windows OS

Ansible system requirements for ansible server

  • LINUX(RHEL/CENTOS/ ORACLE LINUX)operating system.
  • 4GB RAM
  • 40 GB Hard disk space
  • SSH(open SSH)
  • Enabled EPEL repository for centos(6.8 or 7.2)
  • Internet Connection

Installing Ansible

If you have an existing infrastructure but need a server version of Ansible,we can install Ansible with “pip”.Pip tool managesthe packages of python along with library.The advantage of Pip is that all ansible releases are pushed automatically.Hence no more manual updation is required.

After Git installation
PATH=/home/vagrant/ansible/bin:/usr/local/bin:/ bin:/usr/bin:/usr/loal/sbin:/usr/sbin:/sbin:/home/vagrant/bin.
PYTHONPATH=/home/vagrant/ansible/lib
MANPATH=/home/vagrant/ansible/docs/man

Once we have installed ‘Easy_install’we can further install the remaining packages.

>sudo easy_install pip   // install pip.package

Then we need to install paramiko,PyYAML,jinja2 and httplib2

>Sudo pip install paramiko PyYAML,jinja2 and httplib2

This command will install all the other packages.

By default, ansible checks out development branch. Just in case we want some other branch we need to provide the following command-

>git branch  -a
//like
>git checkout release1.7.3

Example Once the checkout is finished and ansible switches to our denoted branch, we can cross verify the version using the following command

>ansible--version
Install Ansible from a RPM file-
>sudo rpm –uvh~/rpmbuild/ansible-*.noarch.rpm
Install via Apt

Ansible is available in ubuntu under personal package Archive(PPA)

>sudo apt-get install apt-add-repository
>sudo apt-add-repository ppa:rquillo/ansible
>sudo apt-get update
>sudo apt-get install.ansible
Installing via brew(Homebrew)
>brew update
>brew install ansible
Installing via pip(pythons package manager)
>sudo easy_install pip
>sudo pip install ansible

For installing multiple other package it is always better to install ‘easy_install’package.
Since Ansible can be installed in Linux flavour.Below are the modules to install and get the packages—

For Fedora,Red Hat enterprise Linux,CentOS and other compatible Linux distribution,all head to use the following command—-

> yum install ansible

Yum is a package management .It resolves enterprise dependency for installing epel.

>yum install –y epel –release
>Yum install –y ansible //This will install ansible.

For Ubuntu,Debian and other compatible Linux distribution,We need to use the following command——-

>apt --get install ansible

If using pip the following command need to be used——

>pip install ansible

Pip tool will resolve the other dependencies, Download the required packages and install.

If you want to install from source code then you need to use the following command

>git clone git://github. com/ansible/ansible.git 
>cd ansible/ 
>sudo make install/source ./hacking/env_setup

Ansible is originally put in github.All releases are posted to git regularly.
How to check if Ansible is installed properly?

>ansible --version

This command will provide you the version of Ansible If Ansible is installed successfully.

alternatively,

>rpm-qa|grep ansible
>ansible -version.el<version>.noarch

This shows that ansible is installed correctly.

To get help in ansible
>ansible --help

Usage:

ansible <host_pattern>[options]

Options:

-a MODULE_ARGS,--args= MODULE_ARGS

Module arguments

-i INVENTORY,--inventory_file=INVENTORY

Provide which inventory host file to pick (default=/etc/ansible/hosts)

-m MODULE_NAME,--module_name= MODULE_NAME

Name of the module to run(default=command)

How to check details of ansible package?

>rpm-ql ansible-version.el<version>.noarch|more
/etc/ansible->main file
/etc/ansible/hosts->Clients ip and details
/etc/ansible/roles->all roles are defined
/user/bin/ansible->ansible commands
/user/bin/ansible-console->console command
/user/bin/galaxy-predefined templates
/user/bin/ansible-playbook->configure playbook
/user/bin/ansible-pull->enable pull mode
/user/bin/ansible-vault-> Ansible vault
[Python library]
-----
-----
[ansible modules]
-----
-----
[extras]
-----
-----

Now we need to create user who can access Ansible . By default, everything will be created as a root user.
How to check who is logged  in?

>whoami

this command will tell who is the user currently logged in.

How to create a new user for ansible?

>useradd ansuser/usedadd-d/home/ansadm-m 
>password ansuser
>new password
>Retype password     [ You need to done steps in all clients]
Password-x-1 ansuser(create a non expiry password)
The Ansible architecture

Once we have created an User for Ansible, we need to switch to new user.

How to switch to other user?

>su -ansuser

su-switch user is the command to switch user from one account to another account.

How to generate public and private key?

>su -ansuser
>ssh-keygen
>Enter passphase-No data (extra security)
//This will generate public and private key.
//We need to copy the public key to all the clients.
>ssh-copy-id-i id-rsa.pub [email protected]

Alternatively:

>su-ansuser
>ssh-keygen-t rsa
>cat rsa.pub

Copy the key.In client we have to follow the same step

>mkdir .ssh
>chmod700 .ssh
>chown ansuser:ansuser.ssh
>cd.ssh/
>vi authorized-keys

paste the key.

>chown ansuser:ansuser.authorized-keys
>chmod 600 authorized-keys

Can we avoid password while logging in to clients?

In case of SSH password less login we can avoid providing password while logging in to clients.In server we generate public and private key and send the public key to client.The client’s response is decrypted using the private key.

This is how the password less login happens between each of the client machine.

Components of Ansible

Program

  • ansible
  • ansible-doc
  • ansible-playbook
  • ansible-pull

Module

  • Perform configuration and system management.Like Copy,service,user,group,etc.

Ansible Architecture: Agentless configuration

Ansible follows a client server architecture where server is called the ansible control server. The server has to be a Linux system.The control server is written on python. The server machine is called controller machine and server is command line based.

Since ansible is config management automation tool, it neither does store huge files or server nor it places lesser files or clients.

Ansible server client architecture

Ansible server client architecture

Ansible clients are otherwise known as nodes(manage host or manage node).Clients can be on linux,Unix,(BSD,Solaris,HP-UX,MAC)or windows system. Client does not have any agent to receive the data from server.Clients gets connected over SSH with the command.

>SSH [email protected]

Server consists of inventory file, Host information (Client machine’s host and ip address). We can alter content by adding details inventory parameters in the server.

How to work with Playbook in Ansible?

A playbook is a collection of different tasks.(Even though via command line we can perform one task at a time.).Manual way of performing tasks are time consuming and tedious.They are error prone too. In case if we have to manage hundreds of machines,playbook comes handy.Playbook in ansible is written in YAML Language and ansible uses a standard YAML parser.Hence a playbook is a collection of tasks and placed in a .yaml file.

Playbook governs the task that needs to be executed so Playbook is the list of tasks (called plays). Plays are human readable YAML files. The output of YAML plays will be a JSON format.

READ  How to Work With Ansible Variables?

Structure of a playbook

The playbook is a simple text file .The playbook should start with 3 hyphens(—)and optionally ends with 3 dots(…).

Playbook consists of –

  • Lists of remote hosts
  • User variables
  • Tasks
  • Handlers

Playbook tasks should for KISS principle (keep it simple stupid).It will be simple, easy to read and easy to maintain.

An ansible playbook is having one or more plays.Each play will have 4 sections.

  • Section-1-target section[Target section on says the host,mode,how the execution will happen and other SSH related settings.]
  • Section-2-variable section[Variable section talks about the variables that we will use while running.]
  • Section-3-task section[Task section is the actual tasks that needs to be performed.]
  • Section-4-(Optional)Handler section[Handler section talks about a section or set of code that needs to be run if a change happens.]

Each play consists in a list .as per YAML list each line must start with a dash(-).

Target section consists of the followings

  • host-Where the play will be fired.
  • user-The username that connects to the machine.
  • become- state of the current user
  • become_method- the method used as become
  • Sudo-set yes if ansible uses sudo to become root on client machine.
  • Sudo_user-The user via which ansible will connect the client machine and become using sudo.
  • Connection-This will tell ansible what transport to use to connect to the client machine.(ssh or paramiko)
  • gather-facts-Automatically run the setup module on remote machine and gather the facts.

Variable section

Variables make a playbook more versatile and allow not being hard coded.Variables and data are easily modified.Variables defined in playbook however can be overridden by global variable or inventory variables.Variables defined in variable section are called vars.

Like

vars:
packages:httpd

Variables can also be loaded from external YAML files by providing ansible a set of list of variables to load.To load external yaml variable file we need to use vars – files directive.

var_files:
/conf/india.yml
/conf/china.yml
/conf/bangladesh.yml

Now each yaml file will look like the below-

india.yml
- - -
Web:'webserver.techtravelhub.com'
TZ:'Kolkata/India'

We can further make the session more interactive (not suitable for automation through)

vars-prompt:
-name:'https-paraphrases'
 prompt:'key passphrase'
 private:yes

Here the pass phrase becomes private and not printed to the screen.

To use the variable we can use three below written formats.

  1. {{variableName}}
  2. ${variableName}
  3. $ variableName

Ansible supports complex variable structures like dictionaries in playbook.

Like 
{{variableName.key}}
Example-{{ansible_etho.ip4.address}}

Scope of the variables

Variables that are defined in variable section applicable to that play only.Variables are not shared among plays.

Task section

Task section consists of actual tasks that the play needs to perform.

 tasks:
  -name:install httpd
   yum:
     name:httpd
     state:installed

This can be written as

  tasks:
  -name:install httpd
   action :yum name =httpd state installed

For tasks names are not required .They are optional property but for better documentation, it is good to have name of each task.Name of tasks also become handy when we try to incorporate handlers, Names will show in console that depicts what task is being done.

Note:

Ansible does not provide a fully featured dependency system.however ansible guarantees that all our changes will be executed in order they are written in playbook.If any module depends on another module so we need to write the code in playbook and place before our target module.

Handlers

The handlers are similar to task structure and support the same format for calling modules.However the only difference is that the tasks execute by default but handlers are executed once they are called .Even if the handlers are called multiple times ,unless there is a change handlers run only once.

The most frequent use of handlers is to restart the machine after a development or restart the daemons after they are upgraded and configured.

- - -
  -hosts:db
    tasks:
      -name:upgrade the DHCP
        action:yum name=dhcp state=latest
        notify:restart dhcp
      -name:configure file copy
        copy:
        src:dhcp/dhcpd.conf
        dest:/etc/dhcp/dhcpd.conf
        notify:restart dhcp
      -name:start dhcp
        action:servicename=dhcpd state=started enabled=yes
     handlers:
      -name:restart dhcp
       action:service name= dhcp state=restarted

We can include multiple handlers in a playbook .They may be attached to different tasks.

How to work with Ansible modules?

Refer Ansible modules to get more in depth information about Ansible modules.Ansible modules takes argument as key value pair (key=value)to perform a task in the remote machine.The response comes in JSON format.

Ansible modules do not do any changes if they are not required.This makes ansible very fast in execution It will simply say skipped about the command,otherwise it will say success.

How to get help about a module?

Ansible provides a command called ansible-doc which is the help file about the module we want to inspect.

To get a list of all modules along with a short description of each module.

>ansible-doc-l

To see help file for a particular module

>ansible-doc modulename/file

Setup Module:

One of the important module of Ansible is setup module.Setup module connects to the clients and gathers data about the system and then returns those values.
To run any ansible module,we need to pass two/three parameters-

  • Host Pattern -> Host pattern to match the machine.(client)
  • Module name -> Which module we want to use.

Host pattern supports group name, a machine name,a glob,a tilde(~) with regular expression. We can select all or * to select all machines.

>ansible<hostname>-u root -k -m setup
>ansible<hostname>-m setup
FieldExampleDetails
ansible_architecturex86_64The client machine’s architecture
ansible_distributionREHLThe client machine’s Linux or Unix distribution.
ansible_distribution_version8.0The client machine’s Linux or Unix distribution version.
ansible_domaintechtravelhub.comThe domain name part of server’s hostname.
ansible_fqdnwebserver.techtravelhub.comfully qualified domain name of the client machine.
ansible_interfaces[“lo”,”etho”]A list of all the interfaces including loopback interfaces of the client machine.
ansible_kernal2.9.32_842.el6.X86_64The kernel version of the client machine.
ansible_memtotal_mb10000The total memory in MB available in client machine.
ansible_processor_count2The client machines CPU available.
ansible_virtualization_roleguestif the client machine is a guest or a host machine.
ansible_virtualization_typekvmThe client machines virtualization setup.

If you have installed tractor or Ohai installed on the client machine,the servers Python automatically fetch these information.

Command module

Command module allows us to write and execute any arbitrary command on the managed machine.

The arbitrary command can be

  • Pre Provided installer.
  • Self written custom script.
  • rebooting of machine.

Command module does not run the command within shell, so redirection is not possible (Like pipe,expand shell,background command).

How to work with modules in Ansible?

Ansible modules

>ansible-doc-l|more
>ansible-doc-l|wc-l =>1378
>ansible-doc-s file (attribute)

These commands are used to list the modules and check its functionalities.

Playbook Modules

The command line usage of module is different than playbook module as many facts may come from another modules.Apart from these few modules just do not work in ansible command line as the required access may not be obtained.

The popular modules for playbook are-

  • The template module
  • The set-fact module
  • The pause module
  • The wait-for module
  • The assemble module
  • The add_host module
  • The group_by module

The template module

The template module allows us to design an outline of a configuration file.The actual values can be inserted later.The template is predefined set of contents where we just need to put data as per requirements and situations.Whenever,there is a need for dynamic change in the content,we use template in ansible.Templates are written in Jinja2 format.

>ansible-doc-s template

will show the details of template.Destination and source are two required parameter for ansible.Jinja2 format template can hold conditions,for loops,and other macro.

The template creation-
>vi index.j2(jinja2 file)
<html>
<head>
<body>
The host is {{ansible_hostname}}
The page is copied at {{ansible_date_time.time}}
//These two variables are coming from facts
Please contact system admin{{name}}for any issue.
//variable coming from playbook .
</body>
</html>

Lets create a playbook

- - -
  -hosts:all
    user:ansadmin
    become:true
    become_method:sudo
   vars:
    name:"techtravelhub"
   tasks:
    -name:copy the jinja2 template on the client machine
   -template:
     src:"/etc/ansible/index.j2"
     dest:"temp/index.html"
…

Note-Since the content is dynamic inture,copy module does not work. Copy module only works on static content.So, in this condition we need to use template module.

Loop in jinja2 file
Options{
listen_on port 80{
127.0.0.1
{% for ip in ansible_all_ipv4_addresses%}
{{ip}}
{%endfor%}
};
Comments in Jinja2 file:

Anything in between {# and #} is ignored by jinjaz processor.Comments are not required but best practice to include what we are trying to do.Jinja2 supports if and end if.To enable if loop,we need to put if tag as true.Otherwise anything between {% if %} and { % endif %} is ignored.

{#variables for zone config#}
{% if 'authorative names' in group_names%}
{%set zone_type='test'%}
{%set zone_dir='testdir'%}
{%else%}
{%set zone_type='actual'%}
{%set zone_dir='actualdir'%}
{%endif%}

The set_fact module:

The set_fact module allows us to build our facts on an ansible play.These facts can be used inside templates or variables in the playbook.Facts may come from setup module.

This helps to avoid complex logic inside template.

Example of set_fact module
- - -
   -name:configure DB server
    hosts:db
    tasks:
     -name:install mysql
       Yum:
        name:"mysql_server"
        state:"installed"
      -name:Calculating InnoDB buffer pool size
        set_fact:innodb-buffer-pool-size-mb={{ansible_memtotal_mb/3}}

This will calculate the memory and ensures 1/3 memory is empty.

pause module

The pause module acts as a synchronization point and halts the execution of a playbook for a certain period of time.This pause or wait can be configured.This pause module is very effective while using with playbook but not much of useful when run via command prompt.

Pause module  is useful when we want users to provide input like confirmation.It is also useful when manual intervention is required.It is like through automation we installed something and manually check if everything is working fine.Playbook will wait for an ‘enter’ key from user as a confirmation to proceed.

Example of pause module
- - -
  -hosts:web
    tasks:
     -name:wait for user input
      pause:
        prompt="Enter to continue and ctrl+c to quit"
      -name:timed wait
        pause:
            seconds:60
…

This module can wait for an indefinite time which may delay the execution.

The wait-for module

The wait-for module helps to poll a particular/predefined TCP port.It will halt until that port accepts a remote connection.The polling operation is done with the remote client machines.This module is mostly applicable to the processes or service that takes long time to start or for processes that we want to run in the backend.For such services we need to use wait-for module.

- - -
   -hosts:web
     tasks:
       -name:Install tomcat
          yum:
            name:tomcat 7.1
            state:"installed"
       -name:start tomcat
          service:
            name:tomcat 7.1
            state:"started"
      -name:wait to start tomcat
          wait_for:
            port:"9999"
            state:"started"
…

The Assemble module:

This module helps us to assemble several files that are combined on client machine.This is applicable when config file does not allow includes,globbing.This is also helpful for the authorized_key file.

- - -
   hosts:all
     tasks:
       -name:Build the authorized_key file
         assemble:
           src:"/opt/sshkeys"
           dest:"/root/.ssh/authorized_keys"
           owner:"root"
           group:"root"
           mode:"0700"
…

The add_host module:

This module allows us to dynamically add new client machines into a play.Add_host module adds our host to a group dynamically by creating a non existent group.The module takes hostname and groupname as an argument primarily.But it can accept other arguments like ansible_ssh_user,ansible_ssh_port etc.

The group_by module:

This module creates groups depending upon the facts available about the machine.The group_by module accepts one argument called key.(Key is the group name where the client machine will be added).

- - -
    -name:create operating system group
      hosts:all
      tasks:
       -group_by:
          Key:"os_{{ansible_distribution}}"

The debug module

Please refer the ansible playbook debug section to get the details.

Let us assume a task given as-
task-

  • Subtask1:install a package called
  • httpd using yum module.
  • Subtask2: Start the httpd service using service module.

The playbook will look like –

- - -
-hosts:dbserver.techtravelhub.com or
-hosts:db or
-hosts:127.10.20.25
User:ansadmin
become:true
become_method:sudo
tasks:
     -name:Installing httpd package
       yum:
         name:"httpd"
         state:"present" or "installed"
     -name:starting httpd service
       service:
         name:"httpd"
         state:"started"
...

Now before we run we need to check if httpd is installed or if the httpd service is up or not(we need to check this information in client or in the target machine ).

>rpm-q httpd =>(if software is installed)
>systemctl status httpd =>(Is the service up?)

If they are not installed or service is up,it is time to run the playbook.

How to run playbook in server?

In order to run playbook in server ,first we need to check if we have written playbook correctly.To check the syntax we have the playbook by the following command.

>ansible-playbook playbookname.yml --syntax-check

If there is no syntax error you can execute the playbook by the following command

>ansible-playbook playbookName.yml

In case we can see if the playbook is executed correctly.

Note:-The tasks can have the following status

  • Ok
  • Changed
  • Unreachable
  • Failed

Note:

Now no. of ok+no. of changed+no. of unreachable+no. of failed=Total number of tasks.We can redirect these output to another file.

How to use variable in inventory file and playbook?

Let us create one more playbook named myplaybook.yml

---
  -hosts:web
   user:ansibleadmin
   become:true
   become_method:sudo
   vars:
     {all variables goes here}
     Package_name="httpd"
     Space="500"
    tasks:     
       -name:Install the package {{package_name}}
        yum:
           name: "{{package_name}}"
           state:"installed"
        -name:Make the service {{package_name}} up and running
        service:
           name:"{{package-name}}"
           state:"started"
...

Ansible Playbook to work with debug module

Lets create a playbook named as debug play.

>v1 debugplay.yml
- - -
  -hosts:web
   user:ansibleadmin
   become:true
   become_method:sudo
   tasks:
     -name:To display the time zone
    debug:
      msg:the time zone is {{ ansible-date-time.tz}}
...

How to store results in Ansible?

Like all other modules, debug module provides output. However, debug module helps to extract the output which can be used later, this makes the Playbook more versatile and flexible.

The storing of results can be applied to—-

  1. Getting a list of files in a remote directory and downloading them all with fetch.
  2. Running tasks when a previous task changes before the handlers run.
  3. Getting the contents of the remote host SSH key and building a known hosts file.

How to debug a playbook?

Ansible provides two ways to debug a Playbook. They are as follows

  1. A verbose mode
  2. A debug module
  3. The check mode
The verbose mode

Verbose is the basic method to debug a playbook. It just prints out all the values that were returned by each modules after it executes. It is most useful while using register keyword.

Syntax for verbose mode
>Ansible –playbook --verbose playbook name.yml
Debug Module:

Debug module is the little enhanced than verbos but very simple way to debug a Playbook. The debug module text two optional arguments—-

  • msg
  • tail

msg sets the message that will be printed if a module fails, if set as yes, it indicates a failure to ansible. If a module fails, It stop the processing the Playbook for the client machine.

---
  -name: show the debug
    hosts:all
    user:root
     vars:
       hostcount:5
     tasks:
       -name: print interfaces
         debug:
              msg:{{item}}"
              with_ items: ansible_interfaces
...

Debug option will help us to understand on what is happening with the Playbook.To enable debug option we can run command with verbose(-v) switch.

level of varbose-

-V-->Provides default output.
-VV-->Provides an extra information than default.
-VVV-->Provides more information than -VV including SSH commands.
The check mode and diff mode

ansible also supports a check mode a diff mode.To enable check mode we need to use –check command and to enable diff mode we need to use –diff command. However, please remember check mode may not provide correct result always.

READ  How To Watch Sporting Action Live Using Assured 5 Kodi Add Ons

The diff mode shows the changes that are made by the template module as it works best on text files.The diff mode can be attached to check mode.

How to segregate playbook into different small plays?

Ansible supports to separate our playbook into different files and include them from some other locations. Ansible also supports roles to include multiple files that perform a similar functions. Ansible supports several other methods for increasing the speed at which ansible configures our machine.

Techniques for supporting larger projects

As and when we create complex infrastructure, our playbook bound to become huge in size. A large playbook is very difficult to read and maintain. Debugging and fault finding becomes tedious job.To avoid these issues, ansible supports divide and rule principal by providing includes.

Includes

Includes split the playbook into diff sections containing related plays. These small or granular level plays can be assembling into main playbook.

There are four types of includes available in ansible-

  • Variable Includes-They put out variables in external YAML files.
  • Playbook Includes-They put plays from another files in a single play.
  • Task Includes-They put tasks in another file, later includes them in main playbook.
  • Handler includes-All handlers can be put in a single file and later can be called from main playbook.

Task includes

Task includes are useful when we have lots of common tasks that need to be repeated on client machines. We can put all these tasks into a separate YAML file and include them from main task.

Task includes inherit the facts from the play they are included from. However, we can create and use our own variables.

We can include conditions on each tasks. Conditions will separately be added to each included task. The tasks are still included.

An example of task includes in ansible playbook

-setupuser.yml

---
   -name:create user account
     user:
       name:"{{user}}"
       state:present
   -name:create user SSH directory
      file:
       path:"/home/{{user}}/.ssh"
       owner:"{{user}}"
       group:"{{user}}"
       mode:"0600"
       state:"directory"
   -name:copy public key
     copy:
      src:"keys/{{user}}.pub"
      dest:"/home/{{user}}/.ssh/authorized_keys"
      mode:"0600"
      owner:"{{user}}"
      group:"{{user}}"
...

Actual playbook

- - -
  -host:all
   user:ansadmin
    tasks:
      -include:setupuser.yml user={{item}}
        with_item: 
          -abc
          -xyz
          -mno
...
Handler includes

Similar to tasks includes, we can place all handlers into a different yaml file an and later calls them into playbook.

-restart.yml-

---
  name :Restarting the service
   service:
    name:"{{pname}}"
    state:restarted
...

The Handler includes file contents several common tasks that we want to handle after the task has been done. They are reusable in nature. Handler can further trigger other handlers. Thus we can set up a series of cascading handlers one after another.

---
   tasks:
    name: install httpd
     yum:
      name:"httpd"
      state:"installed"
     notify: restart service
.
.
.
.
.
    handlers:
     -include :restart.yml
...

Playbook includes

Play Book includes allows us to include whole plays. These plays can be embedded as fully self contained files.

---
  -include "myplay1.yml"
  -include "myplay2.yml"
  -include "myplay3.yml"
.
.
.
.
...

Filter operation on Playbook

>ansible- i hosts playbooks/anyplaybook.yml-- list tasks

This command provides the tasks.

>ansible- i hosts playbooks /myplaybook.yml-- start -at-task='install httpd package'(package)

This command will start executing the task we specify.

>ansible-playbook-i hosts playbooks /my playbook.yml--step

This command will prompt user to execute task in the form of y/n.

Ansible pull mode

To improve playbooks scalability to a larger extent for large project ansible supports a pull mode. In this mode ansible does not connect over SSH rather it runs only on the machine it is working. It downloads the already configured configuration file from git repository.

We need to use ansible pull in the below written scenarios
  • If the node is a member of auto scaling server farm and not available when configuring them.
  • We have a large machine base to configure with equally higher numbers of forks. They make fake huge time to get configured.
  • All our machines need to update their configuration automatically when repository in updated.
  • We are working on a machine where we do not have network access.
The disadvantages of pull mode
  • We need credentials to access other machines, gather variables and a copy of files.
  • We need to manually co-ordinate to run the playbooks across a server farm.
  • Servers may be behind Strict firewalls that may not allow SSH connection(incoming)

Pull mode does not require any major changes in the playbook rather, we need to setup a cron job that will run the ansible-pull mode configuration on every predefined minutes.

Playbook example of pull mode

---
  -name: setup cron
    cron:
     name:"ansible-pull"
     user: ansadmin
     minute:"/10"
     state:"present"
     job:"ansible-pull-u" http://git.int.example.com.com/gitrepos/ansiblepull.git-D/opt/ansiblepull
         {{inventory_ hostname_short}}.yml
...

How to test connectivity between server and clients?

Ansible provides a module called ping that helps us to test the connectivity.

>ansible appserver1.techtravelhub.com—u root—m ping

O/P-

appserver1.techtravelhub.com | success >>{
"changed" : false ,
"ping":"pong"
}

In case if you have setup SSH key you don’t need to use –k argument.If you have set up ansible user then you do not need to specify the user(root) also.

How to setup username globally?

In order to set up username globally we need to configure ansible.cfg.Open the same file in vi editor and the file will look like below—-

Now navigate to remote—user under default section .We can also change the default part where ansible connects using SSH.To add username in the inventory file we need to add the below line—

ansible—ssh—user

like

[Web] 
webserver1.techtravelhub.com ansible—ssh—user = ansadmin
webserver2.techtravelhub.com ansible—ssh—user = testuser

Provide user a sudo privileges

To provide an user a sudo privileges, we need to use -–become command.

Open sudoers. in vi editor

>vi /etc/ sudoers

Now goto the line

Root ALL = (ALL) ALL

Provide your user name like

ansadmin | ALL =(ALL) NO PASSWD:ALL

Now we need to change this line (add this line) in all client machines in order to provide this user (ansadmin) sudo privilege in every machine.

>ansible all – m user-a  "name=ansadmin state=present” -- become

You can use password with sudo or you can use passwordless sudo.

If you want to use the password ,you need to use-k argument or set the ask –sudo-pass value to true .You can use – – sudo command in command line as well.

Adhoc Commands

Adhoc commands are useful to provide some generic commands.Like copy a file ,ping a machine.Assume that we have the below inventory file

[Web]
webserver1.techtravelhub.com
[db] 
dbserver1.techtravelhub.com

We want to copy a file from to web server.
The ad hoc command syntax –

>ansible<[group Name]/machine name/ip>-m<module-name>-a<attributes>
>ansible-all /web/10.0.0.1 -m file/copy/user/yum/ping

Like

>ansible all-m ping =>pings all machines
>ansible all-m shell-a "hostname" =>Gives hostname
>ansible all-m shell-a "uname" =>Gives username
>ansible all-m shell-a "etc/os_release"=> Gives OS name
>ansible all-m user-a "name=techtravelhub state=present"

This command checks all machines where name = techtravelhub is present.

Variables in Ansible

Concept of variables in Ansible is defined in the variable section.

Ansible facts

Ansible facts are the info of the node or client machine.Like

  • host_name
  • ipaddress(ipv 4,ipv 6…)
  • karnel version
  • network devices
  • hard disk etc

Ansible facts are the read only information and cannot be changed from ansible server.

>ansible web –m setup =>(provides us the setup info)

The info will give us lots of other information which we may not be interested.So we can filter the information that we require.

>ansible web –m setup -a "filter=ansible-architecture”

Provides you all client related OS details

>ansible web –m setup -a "filter=ansible-distribution"
>ansible web -m setup -a "filter=ansible-date-time"

To print these information ,we can use debug module and use “msg” command to get these.

How to work with Inventory files in Ansible?

Inventory files plays a great roles in Ansible configuration.

Configuring Inventory File

Basic editing of inventory file (just like an .ini file ) needs to be done manually using any text editor (I personally like Vi editor). However you can also download and install inventory plugins. By default the default inventory file is called hosts and is placed at /etc/ansible. The default file resembles to .ini file.

Some basic default values of inventory(.cfg)file

#inventory                   =/etc/ansible/hosts
#library                     =/usr/share/my_modules
#module_utils                =/usr/share/my_modules_utils
#remote_tmp                  =~/.ansible/tmp
#local_tmp                   = ~/.ansible/tmp
#forks                       =5
#poll_interval               =15
#sudo-user                   =root
#ask-sudo-pass               =True
#ask-pass                    =True
#transport                   =smart
#remote_port                 =22
#module_lang                 =c
#module_set_locale           =False

How to work with Inventory Files in Ansible?

Inventory file contains the lists of nodes(client machine details).Inventory files of Ansible has two entries

  1. Static inventory –where ip does not change
  2. Dynamic inventory-where ip may change(cloud platform)
Static Inventory

Default ansible inventory is located at /etc/Ansible/Hosts.Depending on the inventory modules, inventory file may be in any format.(Best practice to use an .ini format)

Ansible inventory parameters

There are two variables that controls how ansible will interact will hosts.

  • ansible_host-This variable is used to specify the FQDN or ip address of the server.
web ansible_host=webserver1.techtravelhub.com
db ansible_host= dbserver1.techtravelhub.com
  • ansible_connection-This variable is used to specify how the ansible will connect the target servers.
webserver1.techtravelhub.com ansible_connection=ssh

How to change default inventory?

To change default inventory we need to open hosts in a vi editor. In the content section everything starts with a # is commented.We can edit the file in insert mode.

Like

10.2.5.25
abc.myhost.com

How to list out host names?

>ansible all --list--hosts

This command will lists out the hosts and ip address from host file.

How to create customized host file without the default host file?

To create a customized host file without touching the default host file, we need to create a .ini file( extension is not mandatory but best practice).

>vi myInventory.ini
190. 125. 9. 5
190. 125. 9. 10
190. 125. 9. 15

this is how we reinsert new hosts or ips. Once we entered the details, we need to provide the below command to apply this new host file

>ansible all-- list --hosts -i myInventory.ini

Note:

In the above command if you do not give -i switch and the inventory file name the command will fetch details from the default inventory

>ansible all-- list --hosts

If by mistake if we put ip address and host name, will ansible try to push both the time?

No, it will try to do the same but as it is already done previously and no changes happened in between, it will only say ok.

How to create logical grouping in inventory file?

Groups:

Group names are enclosed with a square bracket([ ]) and everything written below to it before next group starts or an end of inventory file characters encountered (…) is part of the above group.

A machine can be in many groups .However Ansible will run the module on the entire group at once.

In inventory file create the groups by providing the group name in square bracket.like [group name]

1.[ web] 

webServer1.techtravelhub.com
webServer2.techtravelhub.com
. 
.
.
.
webServerN.techtravelhub.com
2.[db] 
dbServer1.techtravelhub.com
dbServer2.techtravelhub.com
.
.
.
.
dbServerN.techtravelhub.com
3.[app] 
appServer1.techtravelhub.com
appServer2.techtravelhub.com
.
.
.
. 
appServerN.techtravelhub.com

Instead of hostname you can give ip address or ip and hostname combined.

How to create child group inside the Inventory file?

Ansible supports groups and subgroup concept in inventory file.

Like—

[India]
S1.techtravelhub.com
S2.techtravelhub.com
[Africa]
A1.techtravelhub.com
A2.techtravelhub.com
[China]
C1.techtravelhub.com
C2.techtravelhub.com
[asia:children]
India
China
>ansible india --list-hosts =>o/p-2
>ansible asia --list-hosts =>o/p-4

In subgroup we can use IP addresses as well(along with host names).Ansible supports shorter name instead of long host names.

[Servers]
web ansible –host= S1.techtravelhub.com
>ansible web -- list-host => o/p-1

Apart from these ansible supports to mention connection information.

[Servers]
Server2.techtravelhub.com ansible-connection='ssh'
Server1.techtravelhub.com ansible-connection='coinrun'

Incase you have not mentioned the connection information,the default is SSH.

Along with these ansible supporters ansible-user ,ansible-ssh-pass,ansible-port with the host information.

Like

[Servers]
Server1.techtravelhub.com ansible-user='admin'
ansible-ssh-pass="[email protected]"
ansible-port='8080'

How to filter inventory file?

> ansible web--list-hosts //fitters all hosts name present in web group.
> ansible db--list-hosts //fitters all hosts name present in db group.
> ansible app--list-hosts //fitters all hosts name present in app group .
> ansible all--list-hosts //fitters all hosts name present in the inventory file.

How to insert two groups in filter command?

>ansible web && db -–list-host(need to confirm)

Regular Expression in Ansible Filter

Ansible supports regular expression – pattern matching in inventory file. Say we have the following servers under web groups

[web]
webserver1.techtravelhub.com
webserver2.techtravelhub.com
.
.
.
.
webserver10.techtravelhub.com

The same can be written as —–

webserver[1:10].techtravelhub.com
>ansible web--list-host

Command will bring all ten web servers.

 

How to Work with Ansible Loop and conditions?

Loops and Conditions are powerful tool to customize the Ansible tasks.This section of the post talks about Ansible loops,ansible loop with_subelements,ansible loops examples,ansible loops tutorial,ansible loops with_items,ansible loop vs with_items,ansible loops variables,ansible advanced loops,ansible loops explained,ansible loops in playbook.

 Ansible loops

Loops are used to perform repetitive tasks.Ansible allows us to perform repetitive tasks over several times with different inputs the repetition can be done using looping.

Ansible supports three looping structure.

  • Simple loop
  • Lists of Hashes
  • Nested loops
Simple loop

Simple loop can be done using with_items key and iterate over a set of values provided to the list of items. Here ‘item’ becomes the variable. The list of items are fixed or static.

Let us assume,we need to install three different packages in all client machines.

The package are-

  • vsftpd
  • httpd
  • postfix

So,in general we will create one playbook where all these 3 tasks will be defined.Lets us create a playbook named noloopplay.yml

>vi noloopplay.yml

The structure will be:

- - -
   -hosts:db
    user:ansadmin
    become:true
    become_methods:sudo
    tasks:
      -name:Install package vsftpd
       yum:
         name:"vsftpd"
         state:"installed"
 
      -name:Install package httpd
       yum:
         name:"httpd"
         state:"installed"

      -name:Install package postfix
       yum:
         name:"postfix"
         state:"installed"
…

Now if we see the tasks carefully,we can observe only the package name is changing in the three tasks.so we can create a simple loop to perform these tasks.
To create a task with simple loop,we will create a playbook named withloopplay.yml file.

>vi withloopplay.yml

The structure will be

- - -
   -hosts:db
    user:ansadmin
    become:true
    become_methods:sudo
    tasks:
      -name:Installing package
        yum:
          name:{{item}}
          state:"installed"
          with _item:
             -httpd
             -postfix
             -vsftpd
...

One more example with simple loop and variable

- - -
   -hosts:db
    user:ansadmin
    become:true
    become_methods:sudo
    vars:
      packages:
         -httpd
         -postfix
         -vsftpd
    tasks:
      -name:Installing package
        yum:
          name:{{item}}
          state:"installed"
          with _item:"{{packages}}"
//we can even start the services
       -name:Starting services
         service: 
           name:{{item}} 
           state:"started"
           with _item:"{{packages}}"
...
List of Hashes

Add some groups (g1,g2,g3) and add some users (u1,u2,u3).

tasks:
   -name:adding groups
     group:
      name:"{{item}}"
      state:"present"
      with_item:
           -g1
           -g2
           -g3
    -name: adding users
      user:
        name:"{{item}}"
        state:"present"
         with_item:
           -u1
           -u2
           -u3
How to assign a specific user to a specific group?

In this case also, we need to add hashes techniques.

tasks:
    -name: adding users
      user:
        name:"{{item.uname}}"
        state:"present"
        groups:"{{item.gname}}"
         with_items:
           -{uname:"u1",gname:"g1"}
           -{uname:"u2",gname:"g2"}
           -{uname:"u3",gname:"g3"}

We can add multiple groups to an user-

-{uname:"u1",gname:"g1","g2","g3"}

Nested loop

Just like programming languages,ansible supports nested loop which has two components-

  • Outer loop
  • Inner loop

Assume a scenario where we want to provide a set of 4 users to 5 different DBS.In plain task list we need to write 20 different tasks.But using nested loop concept ,we can manage the same in one single task.

User4->outer loop

DB5->Inner loop

task:
   -name:To provide access to different user to different DB
  debug:
     msg:"{{item[0]}} is provided access to {{item[1]}}"
     with_nested:
       -['user1', 'user2', 'user3','user4']
       -['DB1', 'DB2', 'DB3', 'DB4', 'DB5']

Lookup plugin

Ansible also support lookup plugins. These plugins will help ansible to fetch data from external source.We can also use repeating module in the below situations-

  • Repeating a module many times with similar settings.
  • Iterating over all the values of a fact that is a list.
  • Used to create many files for future usage with assemble module to combine into one large file.
  • Used with_fileglob to copy a directory of files using the glob pattern matching.
READ  How To Watch Sporting Action Live Using Assured 5 Kodi Add Ons

Conditions in ansible -When Conditional

Ansible support logic building using when conditional.Conditions are achieved via when key. The value that we assign to when is a python expression. When clause enable us to process some of the items in the list depending on a condition.

Assume that we want to install “vsftpd” package only on Redhat Linux machine.We may have hundreds of clients attached to ansible server,furthermore we want to install httpd package in centOS.

Example of When condition in Ansible

tasks:
  -name:Installing vsttpd package
    Yum:
      name: "vsftpd"
      state:"installed"
      when ansible_distribution=="Redhat"//This is a case sensitive comparison
  -name=Installing vsftpd package
    Yum:
      name:"httpd"
      state:"installed"
      when ansible_distribution=="CentOS"

We can use variables in the when condition.

Example of When condition in Ansible

---
   -name:Install httpd via yum
     yum:
      name:"httpd"
      state:"installed"
       when:
        ansible_os_family=="RedHat"
    -name:Install httpd via apt
      apt:
       name:"httpd"
       state:"installed"
        when:
         ansible_os_family=="Debian"
…

When condition can be used with a combination of pause to allow users to provide input. In general, ansible stops the current task if it encounters some problem without even running any handlers.

-name:Take user input if something goes wrong
   pause:
    prompt:"Not a proper input for os"
    when:
     ansible_os_family:="RedHat"

Using when condition we can skip actions for the below written scenarios: –

  • Working around differences in operating system.
  • Check if custom written scripts have already been run.
  • Refusing to alter systems that have a particular file present.
  • Improving performance by avoiding an ansible module that we know won’t change anything but may take a while to do so.
  • Prompting a user and only then performing action that they request.

How to use external data lookups?

Ansible supports look up plugins that allow ansible to gather data from outside sources. Ansible has several data lookup plugins.The plugins are flexible so that we can write our own plugins for data lookup. Mainly the data lookups are written in python on the controlling machine.

Data lookups are executed in two different ways
  • Direct calls with “with_*” key.they can be used as a variable or they are used in a loop.

Like with_fileglob

  • Direct injection-In this way,we directly inject lookup plugin in the playbook.
---
   -name:download a file
     hosts:all
      tasks
        -name:download  file.
          get_url:
           dest:"/var/temp/myfile.tar.gz"
           url:"http://server/myfile.tar.gz"
          environment:
           http-proxy:{{lookup('env','http-proxy')}}”

The lookup plugins can be used in ansible variable section too. The way it works is that it runs every time to pick data from external file.

We use lookup plugins in the below conditions –

  1. Copying a whole directory of apache config to a conf.d style directory.
  2. Using environment variables to adjust what the playbook does .
  3. Getting configuration from DNS TXT records .
  4. Fetching the output of a command into a variable.

How to work with Handlers in ansible?

Handlers are important to customize and make playbook efficient.This section of the post talks about Ansible Handler,How to use Ansible Handlers?,ansible handlers notify,ansible handlers vs tasks,ansible handlers debug,ansible handlers global,ansible handlers example,ansible handler multiple tasks,ansible handler arguments,ansible add handler.

Handlers

Instead of executing several tasks,we want some tasks(like-restart the servers)to be executed only when some event or status change occur.

---
   -hosts:db
    user:ansadmin
    become:true
    become_methods:sudo
     tasks:
       -name:Install httpd package
         yum:
           name:"httpd"
           state:"installed"
        -name:starting the httpd service
         service:
           name:"httpd"
           state:"started"
        -name:Copying the index file
          copy:
            src:"/etc/ansible/index.html"
            dest:"/var/www/html/index.html"
         -name:Restarting the server
           service:
             name:"httpd"
             state:"restarted"
…

The above playlist always restarts the server irrespective of the fact that any change happens or not.
To avoid this we can use handlers.

handlers:
     -name:Restarting the server
        services:
          name:"httpd"
          state:"restarted"

In task we need to include –

Notify:
- Restarting the server

Note:

The handler name and the task level notify name should be same.Here notify name and task name should match and the comparison is case sensitive.

In this scenario,if a change in the task happens then handler will run.

How to execute or run operations parallely?

As per configuration file, ansible will only fork up to five times or five different machines at a single go. So if we have a huge number of machines to manage, we need to increase the fork value.apart from these,we need to launch ansible tasks asynchronously. These two factors make ansible to run in maximum forks. Now to run operations parallel, we need to use async and poll keywords. The async keywords allows ansible to run jobs parallel, and ansible will wait till it finishes. The poll command checks the status of the task if the given job is completed or not.

run operations parallel
-hosts:all
   tasks:
    -name:Install httpd
      Yum:
       name:"httpd"
       state:"installed"
    -name:Run update
      command: /user/bin/httpd
      async:"400"
      poll:"15"

while combined with command module with yum module, yum module acts differently. The command module runs across all machines whereas only yum module works on a batch of five machines, once finished yum takes up next five.

If the command module starts a daemon, then we can start it without further polling to check the status. We can use wait _for module to check for completion. If we set poll: 0 (zero) then ansible will not wait for the job to complete. Now on the other hand if the job takes huge time to complete the task, we can wait for undefined period by setting async to 0 (zero).

The ansible polling can be used if

  • We have a long running task that may reach timeout.
  • We need to run same operations on a huge set of client machines.
  • We have such operations for which we do not need to wait to complete.

In the below written scenarios, we cannot use async or polling-

  • If our job acquires a lock that prevent other thing from running.
  • If our job takes short time to run.

How to delegate task in ansible?

By default, ansible runs all required tasks “all at once” on the configured client machine. This feature is good when we have huge machine base that needs to be configured. In this case, each of the machine is responsible for communicating its status to the remote machines. Now if we need to perform an action on a different host than the one Ansible is working, we can use delegation mechanism.

The delegation of task is done via delegate_to key. The module will help running the task on delegated machine (not on all target machines).Delegation can be done in local machine (local host) or it can be done to any host that is in inventory.

The reasons for delegation are as follows-

  • Removing a host from a load balancer before deployment.
  • Changing DNS direct traffic away from a server where we are about to change.
  • Creating an iSCSI volume on a storage device.
  • Using an external server to check that access outside the network works.

Tags in Ansible

Ansible tags allow us to select a part or parts from a Playbook to run. The remaining section will be stopped.

Let’s create a Playbook called playtags.yml

---
  -hosts:db
   user:ansadmin
   become:true
   become_ method:sudo
   tasks:
    -name: installing httpd package
     yum: 
      name:"httpd"
      state:"installed"
      tags:
       -install
    -name:starting the service
      service:
       name:"httpd"
       state:"started"
       tags:
        -service
    -name:copying the index file
      copy:
       src:"/er/ansible/index.html"
       dest:"/var/www/html/index.html”
     tags:
       -copy
       -deploy
       -configure
...

In Ansible, we can assign multiple tags with a task

> ansible-playbook playtags.yml--tags copy
> ansible-playbook playtags.yml--tags service
> ansible-playbook playtags.yml--tags install

To list down the tags

> ansible-playbook playtags.yml--list-tags

Ansible Vault

Ansible Vault is helpful to keep something like Playbooks safe and secret. If we do not use vault, any user may login and read the content of our playbook.

The ansible vault is made off AES 256 cyber technology to encrypt a playbook with proper authentication and authorization. However, if anybody knows the authorization key(password) he can decrypt the encrypted playbook.

To encrypt and already existing playbook
> ansible-vault encrypt PlaybookName.yml
New vault password:// set the new password
Confirm password:// confirm the password

The data is encrypted completely. Now if we wish to see using cat or vi, we can see an encrypted version of the playbook. We cannot even change content of the playbook.

To view the Playbook
 >ansible-vault view PlaybookName.yml

It will ask for password.If the correct password is given, we can see the original content.To edit an already encrypted playbook

ansible-vault edit PlaybookName.yml
To decrypt an encrypted Playbook
>ansible-vault decrypt PlaybookName.yml
How to create a new Playbook in encrypted using vault?
> ansible-vault create NewPlaybookName.yml
New vault password:
Confirm vault password:
How to change vault password?
> ansible-vault rekey NewPlaybookName.yml
New vault password:
rekey vault password:
How to execute an encrypted Playbook?
> ansible-playbook NewPlaybookName.yml--ask-vault-pass
Vault password:

We can save the password in a file and refer  the file from command line.

> ansible-playbook playbookName.yml--vault-pass-file passwordFile

Ansible configuration

Ansible configuration file is similar to INI file format. The file ansible.cfg placement depends on installation.

  • If we have installed ansible via system manager or pip the ansible.cfg file will be automatically available under /etc/ ansible directory.
  • In case we have installed via GitHub, the ansible.cfg will be present on example directory we need to copy or clone it.

Ansible configuration

In order to get the configuration file Ansible will follow the below steps:-

  • It checks in environment variable for the entry of ANSIBLE_CONFIG and which file it is pointing to .
  • In case it does not find any entry there it will check for current directory (./ansible.cfg)
  • In case it does not find it in the current directory,It will search the file in users home directory (~/.ansible.cfg).
  • If all the above searches fail,it will search in the config file in (/etc/ansible/) directory.
  • In case if we have installed Ansible via yum or package manager the config file should get autocreated.

Most of the configuration parameters can be accessed via environment variable. The common way to construct a configuration parameter is

Ansible_(Parameter in upper case )

Elements of configuration file

Ansible has many configuration parameters namely-

  • hosfile–>Path to the inventory file .The inventory file consists of list of machines that ansible will try to connect.
hostfile=/etc/ansible/hosts
  • Library–> Actions that can be performed via ansible (some piece of code ) module, resides in library.
library=/use/share/ansible.
  • forks–> Number of parallel threads allowed to run.
forks=5
  • sudo_user–> The access level of default user to work with the ansible command.
sudo_user=root
  • remote_port–> In which port can be used for SSH connection.(default is 22).
remote_port=22
  • host_key_checking–> disable the SSh host key checking(by default it is true).
host_key_checking=False
  • Timeout–> The timeout value for SSH connection attempts.
timeout=60

log_ path–> Enable logging for ansible.(by default ansible does not log anything)

log_ path=/var/log/ansible.log

Roles in Ansible

Roles are also next level abstraction of a Playbook.As and when our project gets bigger we will get larger files base or a lot of files to work on. Roles comes handy that allows us to group files together in a defined format.

Roles also allows us to place the variables, files, tasks, templates, handlers in a folder for future use. The integration of these are really simple.

Ansible further allows us to define roles inside of a role (it resembles a tree).Roles are primarily services but they can also be Daemons, options, or characteristics.

Why roles are need in Ansible?

Playbook are split into bits and pieces and places in different directories.

Roles help us in-

  • Bringing father modularity
  • Assembling changes
  • Troubleshooting becomes easy

To manage roles, we need to perform the following tasks-

  1. Create a folder called roles with the Playbook.
  2. In roles folder create subfolders for each role we wish to create.
  3. In each role folder create files, handlers, meta, tasks, templates and vars folders. If we do not need to use them, we can create as per our need.
  4. In Playbook, add the roles by the keyword roles.
DirectoryDetails
tasksThe task folder contains main.yml file that includes list of tasks for that particular role.
filesThe files folder is the by default locations for files for the role that are used by the copy or script module.It contains all static content files.
templatesThe template folder contains the templates .Templates are written using jjinja2 format for roles.
handlersThe handlers folder again contains main.yml file that further contains all handlers for the role.
varsThe vars folder contains main.yml that further contains variables for the role.
metaThe meta folder contains a main.yml file containing settings for a role along with its dependencies.It is data about the role.The major information are author, name, description, company, license, main_ansible_version etc
defaultThe default folder contains the default variables to be sent to the roles.It contains a main.yml file that contains all default values that can override the initial values.
README.mdInstallation file.

Assume that we have 100 tasks in a single Playbook. Reading, maintaining, troubleshooting are tough for this large Playbook.

>cd/etc/ ansible will take us to roles folder.It is an empty folder initially.We can set up the roles in ansible.cfg file.In configuration file go to roles_path variable.You can see

roles_ path=/etc/ansible/roles: user/share/ansible/roles

Ansible looks for roles definition in these two directories.
To create roles folder structure we need to provide the below command

> ansible_ galaxy init myRoles --offline

Where galaxy module helps us to create the roles setup init helps in initialise.
myRoles is the name of the role.Offline-> if we do not give this command, ansible will to go internet and see in repository for a role settings.

Hands on on Roles

  • go to vars
>vi main.yml
pname:"httpd"
role1:"Linux architect"
  • go to files

Place the static files that we want to copy.

  • go to template

Create an index.html file

<html>
<head>
This is {{ansible_ hostname}}
</head>
<body>
<h1> this web page is hosted on {{ansible_ hostname}} </h1>
<h2> the copy time is {{ansible_ date_ time.time}}</h2>
<h3> contact the admin_ {{name1}} Role={{role1}}</h3>
// the role will come from vars file as vars overrides the default.
</body>
</html>
  • go to meta
>vi main.yml

Change author

author: techtravelhub
description: test
  • go to handler

edit main.yml

>vi main.yml
---
 -name: restarting the service
   service:
     name:"{{pname}}"
     state: restarted
...
  • let’s create tasks

To create modularity let’s create several tasks here.

To create install.yml

---
  -name: installing the{{pname}} package
    yum:
      name:"{{pname}}"
      state:"installed"
...

To create service.yml

---
  -name: starting the service
    service:
     name:"{{pname}}"
     state:"started"
...

To create configure.yml

---
  -name: copying the configuration file
    copy:
      src:"httpd.conf"
      dest:"\etc\httpd\config\httpd.conf"
    notify:
     -"restarting the service"
   -name: copying the index file
     template:
       src:"index.j2"
       dest:"/var/www/html/index.html"
     notify:
     -restarting the service
...

Now edit main.yml file under task folder

-include: Install.yml
-include: Service.yml
-include: configure.yml

We can use import_ task as an alternative to include.Now the final task to create a Playbook.

-webserver.yml-

---
  -hosts: web
   user:ansadmin
   become:true
   become_ method:sudo
  roles:
   -myRole
...

Now to run this Playbook we need to use the below command

> ansible_playbook webserver.yml

Note: http://galaxy.ansible.com is having a lot of predefined rules. We can download and configure them to further use.

Share and Enjoy !

88 thoughts on “How to Work With Ansible?”

  1. Hi all, here every one is sharing these knowledge, thus it’s nice to read this blog, and I used to visit this website all the time. oreiep.se/map33.php spiral i armen

  2. Today, I went to the beachfront with my kids. I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She put the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear. She never wants to go back! LoL I know this is totally off topic but I had to tell someone! breed.aperca.se/map4.php body shop tyskland

  3. Its not my first time to visit this web page, i am browsing this site dailly and obtain good facts from here all the time. panilul.se/map7.php kokoskakor med choklad

  4. If you desire to get a great deal from this post then you have to apply these methods to your won web site. wolga.aperca.se/map1.php vita kärleksmums recept

  5. It’s actually a nice and helpful piece of information. I am happy that you shared this helpful information with us. Please keep us informed like this. Thank you for sharing. arin.munhea.se/map1.php rГ¶da utslag i nacken

  6. Hmm it appears like your site ate my first comment (it was super long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well am an aspiring blog writer but I’m still new to everything. Do you have any suggestions for rookie blog writers? I’d definitely appreciate it. egluopu.se/map23.php bГ¤sta brun utan sol tabletter

  7. Heya i am for the first time here. I found this board and I find It really useful & it helped me out much. I hope to give something back and help others like you helped me. sgemr.munhea.se/map9.php mörk choklad ica

  8. Your mode of explaining all in this article is actually pleasant, every one be able to easily understand it, Thanks a lot. oreiep.se/map11.php byggprojektör bim lön

  9. Wonderful, what a webpage it is! This web site presents useful facts to us, keep it up. derse.aperca.se/map6.php gjuta plintar vilken betong

  10. Hi would you mind stating which blog platform you’re using? I’m planning to start my own blog soon but I’m having a hard time making a decision between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your layout seems different then most blogs and I’m looking for something completely unique. P.S Apologies for being off-topic but I had to ask! otertbe.se/map33.php hГ¤lsa alla frГҐn mig

  11. WOW just what I was looking for. Came here by searching for
    Excellent post. Keep posting such kind of information on your blog. Im really impressed by it.
    Hey there, You have performed an incredible job. I will certainly digg it and individually recommend to my friends. I’m confident they’ll be benefited from this web site. sasilu.se/map30.php pamela love sverige

  12. Hi there, i read your blog from time to time and i own a similar one and i was just curious if you get a lot of spam responses? If so how do you protect against it, any plugin or anything you can recommend? I get so much lately it’s driving me mad so any help is very much appreciated. prefi.munhea.se/map11.php tappar hГҐr brist

  13. Actually when someone doesn’t know after that its up to other people that they will help, so here it occurs. lanba.se/map15.php apoteket hjГ¤rtat sickla

  14. Hey! Someone in my Myspace group shared this site with us so I came to look it over. I’m definitely enjoying the information. I’m book-marking and will be tweeting this to my followers! Terrific blog and superb design. reipr.aperca.se/map6.php gГ¶ra i malmГ¶

  15. Hello it’s me, I am also visiting this website on a regular basis, this web site is truly pleasant and the users are really sharing nice thoughts.

  16. We stumbled over here by a different website and thought I may as well check things out. I like what I see so now i am following you. Look forward to looking at your web page repeatedly.

  17. What’s up, for all time i used to check web site posts here in the early hours in the dawn, for the reason that i like to find out more and more. azilopar.se/map16.php svamp i slidan behandling

  18. First of all I want to say excellent blog! I had a quick question in which I’d like to ask if you don’t mind. I was interested to find out how you center yourself and clear your head prior to writing. I have had a difficult time clearing my mind in getting my ideas out there. I truly do enjoy writing but it just seems like the first 10 to 15 minutes are wasted simply just trying to figure out how to begin. Any recommendations or hints? Thanks! tege.aperca.se/map5.php fГ¤rga hГҐret askblont

  19. With havin so much content do you ever run into any issues of plagorism or copyright infringement? My blog has a lot of unique content I’ve either created myself or outsourced but it seems a lot of it is popping it up all over the internet without my permission. Do you know any methods to help stop content from being ripped off? I’d truly appreciate it. cator.munhea.se/map4.php kejsarn av portugallien film

  20. It is appropriate time to make a few plans for the future and it’s time to be happy. I’ve learn this put up and if I could I desire to recommend you some fascinating issues or advice. Perhaps you could write subsequent articles relating to this article. I wish to learn even more things about it! panilul.se/map19.php bjГ¶rn borg sandaler herr

  21. It is appropriate time to make some plans for the longer term and it’s time to be happy. I’ve learn this put up and if I may I want to recommend you some fascinating issues or tips. Maybe you can write subsequent articles regarding this article. I wish to learn even more things approximately it! oratrg.se/map42.php skin always dry

  22. Hello there, just became aware of your blog through Google, and found that it is really informative. I am going to watch out for brussels. I’ll be grateful if you continue this in future. Many people will be benefited from your writing. Cheers! dehllap.se/map25.php vuxen mГҐlarbok panduro

  23. These are actually enormous ideas in regarding blogging. You have touched some good factors here. Any way keep up wrinting. tortm.aperca.se/map6.php läkemedel vid kärlkramp

  24. Nice post. I was checking constantly this weblog and I am inspired! Very useful information particularly the last section 🙂 I deal with such info much. I used to be seeking this certain info for a long time. Thanks and good luck.

  25. Wonderful work! That is the type of information that should be shared around the net. Disgrace on the search engines for now not positioning this put up upper! Come on over and visit my website .

  26. When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get several emails with the same comment. Is there any way you can remove people from that service? Cheers!

  27. Hello, just wanted to say, I enjoyed this article. It was inspiring. Keep on posting! oreiep.se/map24.php robert pettersson takida flickvän

  28. Hey there! I know this is kinda off topic nevertheless I’d figured I’d ask. Would you be interested in exchanging links or maybe guest authoring a blog post or vice-versa? My site addresses a lot of the same topics as yours and I believe we could greatly benefit from each other. If you’re interested feel free to send me an email. I look forward to hearing from you! Superb blog by the way!

  29. Oh my goodness! Amazing article dude! Thank you, However I am experiencing troubles with your RSS. I don’t know the reason why I cannot join it. Is there anyone else getting identical RSS problems? Anyone who knows the answer will you kindly respond? Thanks!!

  30. Hello there, I discovered your site via Google whilst looking for a comparable matter, your website came up, it looks good. I’ve bookmarked it in my google bookmarks.
    Hi there, just became alert to your blog thru Google, and found that it is truly informative. I am gonna be careful for brussels. I will appreciate should you proceed this in future. Numerous people can be benefited out of your writing. Cheers!

  31. Hi I am so grateful I found your webpage, I really found you by accident, while I was researching on Aol for something else, Anyways I am here now and would just like to say cheers for a fantastic post and a all round entertaining blog (I also love the theme/design), I don’t have time to read through it all at the minute but I have saved it and also included your RSS feeds, so when I have time I will be back to read a lot more, Please do keep up the excellent work.

  32. Someone necessarily help to make severely posts I’d state. This is the very first time I frequented your website page and so far? I surprised with the analysis you made to make this particular submit extraordinary. Magnificent process!

  33. I’m really enjoying the theme/design of your site. Do you ever run into any web browser compatibility problems? A small number of my blog audience have complained about my blog not operating correctly in Explorer but looks great in Firefox. Do you have any tips to help fix this problem?

  34. Please let me know if you’re looking for a writer for your blog. You have some really good posts and I feel I would be a good asset. If you ever want to take some of the load off, I’d absolutely love to write some content for your blog in exchange for a link back to mine. Please send me an email if interested. Regards!

  35. Hi, i read your blog occasionally and i own a similar one and i was just wondering if you get a lot of spam responses? If so how do you stop it, any plugin or anything you can suggest? I get so much lately it’s driving me insane so any support is very much appreciated.

  36. WOW just what I was looking for. Came here by searching for
    Excellent article. Keep writing such kind of information on your blog. Im really impressed by your site.
    Hi there, You have performed a fantastic job. I will definitely digg it and personally suggest to my friends. I’m sure they’ll be benefited from this web site.

Leave a Comment

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