Ansible for Devops

   


      All my ansible code which is mentioned in this article is in my github repo in below link just download and run it in your own machine.

       Link is i.e.  Ansible4devops


Ansible is a IT automation tool useful to build and deploy from servers to applications to monitoring. let's say end-to-end IT automation tool.

Unlike other automation tools like puppet,chef etc, Ansible doesn't need any agent setup in client side.
Only thing ansible needs is python, by default all Linux flavours has python by default and Ansible works over SSH-KEYGEN exchange from control server to target server.

Most importantly it easy to implement and understand.

This blog is for how to work effectively with Ansible which will help to solve your day-2-day problem in IT.

Install Ansible

Assuming if you already have python in your Linux box.(Comes with OS itself :)  )


In order to install ansible just do #yum install ansible  , if and only if you have added an EPEL repo in your /etc/yum.repos.d folder in your LINUX box.

In order to add EPEL repo just do #yum insttall epel-release


We call the Linux machine as ansible-controller in which ansible got installed.

After installing ansible just type below command to confirm it is running.


In above ansible config file is source of your ansible setup. In this file inventory which consists list of hosts in which ansible runs its configurations.

How ansible controller machine connects those list of hosts automatically?

At the first time one should create password less authentication to all the list of hosts from the ansible contoller server. It means as shown in above file remote_user: bharath   user "bharath" ssh-keys exchanged to all the list of hosts mentioned in inventory file.

So that ansible-contoller server can be able to login to all the list of hosts without prompting password all the time.

Here goes testing...

inventory file: /etc/ansible/hosts


Let's say we have below ansible-controller and node servers are available as below

10.0.3.146  ansible-controller
10.0.3.103 ansible-node1
10.0.3.36 ansible-node2

should be able to login to all the ansible-nodes from ansible controller without prompting password as below..





Let's do magic how ansible makes your life more easier with it's modules. Let's run some adhoc commands to taste the power of ansible.

According to our inventory i.e. /etc/ansible/hosts ansible-controller executed below commands on all nodes and displayed the result.


you can even specify dynamic inventory file at the time running adhoc commands as below with -i option.


Not only inventory file you can even change the path of ansible configuration file ansible.cfg but you should specify your shell to identify the ansible.cfg file, so you should export ANSIBLE_CFG env variable to your bashrc or bash_profile file.

ansible.cfg lookup precedence
1). Current directory , from where you run your ansible. ./ansible.cfg
2). Users home directory lets say /home/bharath/ansible.cfg, In which user you run your ansible
3). If above paths ansible.cfg is not present ansible will automatically look into the /etc/ansible/ansible.cfg 

Inventory: /etc/ansible/hosts, Inventory provides more than just system-names and groupings. Data about the systems can be passed along as well.

Variables:

Variables are powerful construct within ansible and can be used in soooo many sources and even one source may override the other source.
Ansible discover the data about system during the setup phase.

example: Declaring variables in the inventory file itself...i.e. /etc/ansible/hosts file.


 

Adding Behavioural inventory parameters:

These variables intended to alter the way Ansible behaves when operating with the host.

For example in ubuntu group ansible_ssh_port parameter instruct ansible to connect all the hosts in that group using port 314 for SSH, rather than the default of 22.

Indeed there are so many Behavior inventory parameters that ansible provides us. like ansible_ssh_host,ansible_ssh_port,ansible_ssh_user,ansible_ssh_pass,ansible_sudo_exe,ansible_connection....these behavioral parameters you can even declare in the ansible configuration file as well. i.e. ansible.cfg file.


In all above examples we declared static inventory(hosts) file but we can also declare dynamic inventory files, Ansible provides a inventory plugins for such as AmazonEC2,Google Compute Engine,Microsoft Azure,Docker,Openstack Nova,Rackspace Public cloud....etc.



Ansible Playbooks


Let's Jump to playbooks and see how ansible executes playbooks.

There are so many ways you can structure your playbooks that based on inventory hosts groups you can declare variables in the group_vars directory or you can declare under vars directory and include it in playbooks. Now it is bit tricky to understand if you go through below steps you will comfortable along the way.

Playbooks in ansible written simple YAML based synatx. So that everyone can understands it even novice LINUX users as well.


playbook1: How to parse dynamic hosts inside  playbooks.

There is an anisble module called add_host in which it accepts name,group as its arguments.



Comments

  1. Excellent and Informative. Please keep writing .

    ReplyDelete
    Replies
    1. All are your blessings only thala naa ellaam Vunga kaaalthoosi

      Delete

Post a Comment

Popular posts from this blog

How to check the hardware information in Linux Systems?

All About Amazon Web Services(AWS)