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.
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.
Getting your facts straight!
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:
- Inventory is the list of the computer systems you want to configure using your host system.
- Playbook is markup file instructing Ansible to execute a set of statements on managed hosts.
- 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.
- Install using package manager or follow Official guide,
sudo dnf install ansible
- Check the version of your ansible,
ansible --versionand make sure, it is no older than 2.6.0.
Now briefing the instructions by @trishna_g during the #dgplug session:
- 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, use,
ssh-copy-id -i <absolutepath_to_key> <username>@127.0.0.1.
- You can verify the connection by SSHing into the managed host.
ansible all -i "localhost," -c local -m ping, where
all -i "localhost"signifies all the nodes in inventory “localhost” to be used, defining “local” connection protocol using
-c localto execute module “ping”.
- Then, try,
ansible all -i "localhost," -c local -m shell -a '/bin/echo hello'where echo program is used to run on shell module with message “hello”.
- 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.
ansible all -i hosts -m shell -a '/bin/echo hello'
- The way we added “localhost” in our custom inventory, we call it “ungrouped” hosts.
- 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.
- Now in the “hosts” file, on the top of your localhost add
[webserver]. Do not put it on the same line.
ansible webserver -i hosts -m shell -a '/bin/echo hello'
- Now, create a playbook, by creating a file,
demo.ymlin the same directory with contents as https://gist.github.com/trishnaguha/18352fc138168111d0d015e84c3ace67.
- Now, run,
ansible-playbook demo.yml -i hosts -v.
That’s all for now, demonstrating about Ansible.
Filling the gaps!
So, Yes! Ansible is one of the most important Devops tools, that will come handy not only for enterprises but also for individual developers. Full logs for the session can be found at dgplug.org/irclogs. Join us at #dgplug on freenode server IRC
If you find any inconsistency on the content, ping me over IRC. I am philomath over freenode server. You can also find me at #dgplug on freenode server.