Introduction to Ansible - Part 2
If you have not go through the first part of the Introduction to Ansilble series, you can find it here:
In this part we will focus on installing Ansible on a Ubuntu server on DigitalOcean and some of the main configuration files that you need to keep in mind. To make things even better you can use my referral link to get a free $50 credit that you could use to deploy your virtual machines and test the guide yourself on a few Digital Ocean servers:
Digital Ocean $50 Free Credit
Create 3 ubuntu servers and take note of their IP addresses!
Once you've created your Digital Ocean Ubuntu servers, you would need to install Ansible only on your Master server.
To do that just SSH to the Master server and run these commands:
That is pretty much it, now you have an Ansible control server! To test that just run this commnad:
You should see a simliar output:
Ansible configuration file
You can find the main Ansible configuration file at:
It is really well documented! I would suggest going though the file itself carefully as there are a lot of useful comments. As this article is just a basic introduction I would be using the default settings.
Ansible inventory file
The Ansible inventory file is a file that defines the hosts and the groups of hosts which we could run our commands, tasks, playbooks and etc. on. You can create custom inventory files for different projects in other locations.
The default Ansible inventory file is at:
You could have your inventory file in different formats, depending on the inventory plugins that you have. The default format is INI-like, here's an example:
db[1:5].bobbyiliev.com # That way we specify a range of hosts on one line
cache ansible_host=cache.bobbyiliev.com # This way we are creating an alias so we could then easily reach the server in question
Again this file is really well documented, I would suggest going though the comments so you could get a better understanding on how you could group your servers.
You can go ahead and add your Digital Ocean server IPs to the inventory file already! It should look something like this:
web ansible_host=22.214.171.124 ansible_python_interpreter=/usr/bin/python3
db ansible_host=126.96.36.199 ansible_python_interpreter=/usr/bin/python3
Note: change the 188.8.131.52 and 184.108.40.206 with your droplets' IP addesses!
As I already mentioned all you need to manage your nodes is SSH access. You could either use SSH keys or you could use the -k argument and use SSH password instead.
I believe that it's much more convenient to use SSH keys so I would just generate a new SSH key pair and copy the public key to the new nodes. To do that just run this command on your master server:
By default your public ssh key would be added to this file here:
Just copy the content of the file and add it to your 2 other servers in the following file:
This is pretty much all you need, now your all setup!
As a quick test you could run the following command:
ansible -m ping all
The output that you would see should look something like this:
Side note: It's also a common practice to create a new user called ansible that we would use to run your playbooks. You can add the new user with the useradd command, but as this is just a basic introduction we will skip this step.
This is pretty much it for part 2, your main take away from this should be:
- It is easy to install Ansible
- The two important files to remember are the ansible.cfg and the ansible intentory file
- All you need to do is have SSH access to the machines that you would like to manage!
You can find Part 3 here:
Feel free to let me know in case that you have any questions!