# Ansible101

In the realm of Devops, where there is always fight to provide the better and efficient ways of production in an enterprise, Ansible is one of the solution to help enterprise achieve that. This blog post will brief you about what ansible is and how learning ansible can save hours of debugging and setting up the same configurations again and again. Later this post will brief the “Ansible 101” session taken by @trishna_g at #dgplug on freenode server, IRC. Feel free to skip to “Are we having fun yet?" section to try Ansible right now.

## Let’s Start

As a developer, atleast once in our lifetime, we all faced the frustration of configuring our systems/servers with same configurations again and again, due to system failures, or while changing to another operating system. If you know what I mean, that configuration and system setup process can be tiresome and boring, sucking all your excitement and energy to work on the project later on, right?

Or as a user, any time, we change our Operating System, upgrade our system or recover from any failure, we have to setup our system again, as it was before. All the services, packages, libraries and most important system configurations should be as it was, so that we can work more efficiently on our project rather than worrying about configurations and missing packages during development.

And we all thought, that’s the way it has to be. Well, not exactly. There is a solution to this problem, by automating the process, using Ansible. Ansible is what we all heard of, every now and then, but if didn’t, may be, that’s what you’re lacking.

## Ansible is the need of the hour!

In the enterprise, there are many tools that are needed to maintain the efficiency and workflow. And if we have to configure those 100s of systems manually, will you? I mean, it would be so easy if we could just instruct one of the system to configure all the remaining ones. Ansible does just the job for you, automating these tiresome and boring work for you.

Ansible has various benefits, over manual configurations of 100s of systems. With the help of Ansible, you can easily setup all the systems at once, with same configuration and in case of any changes later, you just need to update the Ansible Playbook for all systems to update. Easy, no? Ansible is one of the criticial tools, used in enterprises and thus, definitely worth your time. In case of users, even to configure single system, learning Ansible might feel a bit overhead, but it will be helpful in future, when you have to recover from any failures, switch to different operating system etc.

To find more on, why Ansible, check their official page here.

It goes without saying, if foundation’s strong, so the building is! Let’s work on our foundations of Ansible first. Feel free to skip to “Are we having fun yet?" section to give it a try and come back here to fill in the gaps later. Though I prefer the former method. To understand on how Ansible works, check out their webpage, here.

Few Keywords used in the realm of Ansible:

1. Inventory is the list of the computer systems you want to configure using your host system.
2. Playbook is markup file instructing Ansible to execute a set of statements on managed hosts.
3. Modules are the pre-written programs in the Ansible, to perform common tasks such as Ping (to test the connectivity). List of modules can be found at here.

TL;DR? In brief, A control system is used to configure your other systems known as Managed nodes, connected through a secured remote connection protocol, such SSH or Kerberos. All the systems enlisted in an inventory files can be controlled/managed by the control node, to execute a set of instructions or programs known as Ansible Modules using playbook written in a Markup Language (YAML) to setup, and configure the managed nodes at once.

## Are we having fun yet?

What’s the use of learning so many intricacies if we don’t know how to use them, right? So, Let’s get started! Feel free to follow the official Ansible Documentation.

##### Install and setting up

For your linux distribution, use the default package manager to install the “ansible” package. To install from source, follow the official documentation [here][]. In this case, we will take fedora as our operating system.

1. Install using package manager or follow Official guide,

sudo dnf install ansible

2. Check the version of your ansible, and make sure, it is no older than 2.6.0 using ansible --version.

Now briefing the instructions by @trishna_g during the #dgplug session:

1. Set up the SSH Key on your system and copy that key to your managed hosts, in this case your local machine, in order to allow your control node to connect with managed hosts, using,

ssh-copy-id <username>@127.0.0.1


In case you have multiple keys on your system,

ssh-copy-id -i <absolutepath_to_key> <username>@127.0.0.1


You can verify the connection by SSH-ing into the managed host.

2. Execute the following instruction , where all -i "localhost" signifies all the nodes in inventory “localhost” to be used, defining “local” connection protocol using -c local to execute module “ping”.

ansible all -i "localhost," -c local -m ping

3. Now, execute shell module to invoke echo program with message “hello”.

ansible all -i "localhost," -c local -m shell -a '/bin/echo hello'

4. Create a file in any directory named, “hosts” and write, “localhost ansible_connection=local” within the file. This file will now act as inventory for further demonstration. To execute the shell using the hosts described in the “hosts” file, run,

ansible all -i hosts -m shell -a '/bin/echo hello'

5. The way we added “localhost” in our custom inventory, we call it “ungrouped” hosts.

6. To create a group of systems, say webserver, to control all systems from a particular group, we add the group as [groupname] in the above line. Your file should have the following content

[groupname]
localhost ansible_connection=local

7. Now in the “hosts” file, on the top of your localhost add [webserver] for [groupname]. Do not put it on the same line and run the following command.

ansible webserver -i hosts -m shell -a '/bin/echo hello'

8. Now, create a playbook, by creating a file, demo.yml in the same directory with contents as https://gist.github.com/trishnaguha/18352fc138168111d0d015e84c3ace67 and execute all the above commands using yaml by executing following instruction on shell.

ansible-playbook demo.yml -i hosts -v

- hosts: webserver
connection: local