Welcome back guys, it’s been a busy few weeks and I haven’t had a chance to write a post but today I am back!! and it’s going to be a good one!. My initial idea for this blog was to write uniquely about deployment tools and technologies but my curiosity got the best of me and I started writing about everything that interested me. I am glad I am doing it this way but this week we are going to start diving into Deployment/configuration tools and first up is Ansible.
A little bit of history...
I have used a few Configuration management tools and tools to aid deployments such as Chef, Jenkins, Rundeck and a few more. I recently heard about a team that favoured Ansible over Chef and I asked why. They were saying they didn’t like the way it was implemented and some of the complications around Chef, after chatting with him my curiosity jumped. Honestly, before I only heard of Ansible by name and I just assumed it was just another tool why should I look into this tool when we have the other ones well I was entirely wrong…
What is Ansible?
I am going to take it directly from their website…“Ansible is a radically simple IT automation engine that automates cloud provisioning, configuration management, application deployment, intra-service orchestration, and many other IT needs.” While that is quite a lot of features many tools do the same thing such as Chef and Puppet so what’s different? Well honestly the main thing that jumped out at me was this line “…Ansible relies on an agentless architecture” This means we can literally run it anywhere at anytime Ansible uses ssh which by itself brings up a lot of advantages (better security, installed everywhere and easy to use).
Let’s get started:
I am only going to jump into running ad-hoc commands as I am writing this blog as I am learning. To get up and running it’s extremely easy to install Ansible pretty much as simple as running the following:
$ sudo apt-get install software-properties-common
$ sudo apt-add-repository ppa:ansible/ansible
$ sudo apt-get update
$ sudo apt-get install ansible
That is it!. You can now run ad-hoc commands on any machine that you want and have access to. What is the use of ad-hoc commands? Well, I constantly run into situations where I need to run a command across multiple servers. I have written python/bash scripts that would take a list of servers and run the command I need to, I am sure anybody that has ever had to manage servers has written a similar script well Ansible makes them scripts obsolete and its 10x easier than scripting it. For example, let’s say I want to find the uptime of every server:
ansible all -a "uptime"
127.0.0.1 | SUCCESS | rc=0 >>
23:12:55 up 55 min, 2 users, load average: 0.31, 0.25, 0.22
That’s it…note I am running these commands locally but you get the picture. So I lied a bit in order for me to be able to do that I needed to install ssh keys on the remote hosts. Note this is not a requirement you can run this and have it prompt you for a password. What I would suggest is look for a playbook( will get into later) or simply the first time you run an ad-hoc command have it install your ssh keys therefore no more typing all your passwords! 🙂
You might be asking what “all” is? well, it takes all the servers in your configuration file and runs the command specified. The configuration file is : /etc/ansible/hosts. If you open this file you see it has a lot of templates’ already setup. The tags in the file are extremely powerful. Let’s say you want to run a command on all your servers inside your London datacenter. Open the configuration file and add the tag “London”
# ansible london -a "uptime"
127.0.0.1 | SUCCESS | rc=0 >>
23:25:29 up 1:08, 2 users, load average: 0.38, 0.22, 0.19
There you are! You just ran it on all your servers inside your London datacenter. For full list of ad-hoc flags: http://docs.ansible.com/ansible/intro_adhoc.html
OK, Jason, that is great but the script I wrote also copies files to all my hosts…does Ansible support that?? Well yes of course it does!! Let’s copy some file we have to all our hosts inside our London data center:
ansible london -m copy -a "src=/etc/hosts dest=/tmp/hosts"
That’s it one line and it copies it everywhere! How awesome is that? Pretty much anything that you can code in a script Ansible can do…update packages, pull from git, manage services yep all. I recommend taking a look at: http://docs.ansible.com/ansible/intro_adhoc.html for some examples on what it can do.
Guys this tutorial is incredibly short but it lays out the basics of what Ansible is and how powerful it can be and guess what it is even more powerful do you want to deploy your custom application to your server? It can do that, so there is a lot more for me and you to learn. I am going to start using this in my “Day Job” as its extremely powerful and so easy to use, we do use Rundeck which I would describe as a bit more complicated version of Ansible with a pretty UI but this is a hell of a lot easier to setup in my opinion. I hope you enjoyed this short introduction to Ansible
Until Next time