This article is outdated and may not work correctly for current operating systems or software.
Ansible is a configuration management system. Configuration management allows for automatic deployment, configuration, and centralized management of applications. This is handy if you need to deploy your application on multiple servers without the need for having to do this manually on all your servers. You can also add identical servers to your cluster. You could set up a load balancer/cluster/failover with this, for example.
Ansible provides configuration management so you can add identical servers to your cluster very easily. You can also do centralized management for all of your servers in one place. You can run an apt-get update
on all servers at once!
Personally, I prefer Ansible because it does the job without having to manually install software on all your servers. It does deployment and management over SSH. SSH is a standard medium for server management, but a disadvantage here is that you need to enable SSH. Some might consider that a security risk, although Ansible works with SSH keys, which are more secure.
In this tutorial we'll see how we can install Ansible on Ubuntu 14.04.
You can install Ansible with:
apt-get install ansible
You need to put all the servers that you want to manage with Ansible in the /etc/ansible/hosts
file.
You will need to comment out all lines. Go to the latest line of the hosts
file to create a category. Say you have a cluster of web and database servers. You could create two separate categories: web
and db
. If you would want to make a change on all database servers, you could use db
as selection so only all database servers would be affected and not other servers such as your web servers in the web
category.
Example:
[web]
localhost ansible_ssh_host=127.0.0.1
web1 ansible_ssh_host=192.168.2.2
web2 ansible_ssh_host=0.0.0.0
[db]
db1 ansible_ssh_host=192.168.2.3
db2 ansible_ssh_host=192.168.2.4
db3 ansible_ssh_host=192.168.2.5
db4 ansible_ssh_host=192.168.2.6
Format: name ansible_ssh_host=ip
Note that if you're using Ansible 2.0, the ssh_
part has been deprecated. Instead use ansible_host
.
name
is just a name to refer to your server, ip
is the actual IP.
This tells Ansible that you have 3 web servers on the IP addresses 127.0.0.1
, 192.168.2.2
and 0.0.0.0
and 4 database servers on the IP addresses 192.168.2.3-6
.
Ansible works with SSH keys. If your SSH key on nodes isn't the same as on your Ansible server, it will give you an error. Therefore, we need to generate an SSH key.
ssh-keygen
Now add your SSH key to your nodes. If all your nodes are at Vultr, go to the Vultr SSH key manager and add your public key. It can be found here:
cat ~/.ssh/id_rsa.pub
Then, when deploying your server, choose your SSH key in the "SSH Keys" section.
To see if you can ping all your servers in the hosts
file, you can use the following command:
ansible -m ping all
This confirms whether or not your servers are online.
You can also execute a command:
ansible web -m command -a 'shutdown -h now'
We've just executed the command shutdown -h now
on all servers in the web
category.
At this point, Ansible is setup and ready to go. Seems pretty simple, right? Well, we haven't covered the most powerful feature of Ansible yet: playbooks. Learn more about playbooks in this tutorial.