Introduction to Ansible - Part 2

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:

https://bobbyiliev.com/blog/introduction-to-ansible-part-1

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!

Ansible Digital Ocean Introduction

 

Ansible installation

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:
 
apt update
apt install ansible
 
That is pretty much it, now you have an Ansible control server! To test that just run this commnad:
ansible --version
You should see a simliar output:
 
Ansible Version
 
 

Ansible configuration file

You can find the main Ansible configuration file at:
/etc/ansible/ansible.cfg
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:
/etc/ansible/hosts
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:
 
ansible.bobbyiliev.com

[webservers]
web1.bobbyiliev.com
web2.bobbyiliev.com

[dbservers]
db[1:5].bobbyiliev.com # That way we specify a range of hosts on one line

[cacheservers]
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:
 
[webservers]
web ansible_host=1.1.1.1 ansible_python_interpreter=/usr/bin/python3

[dbservers]
db ansible_host=2.2.2.2 ansible_python_interpreter=/usr/bin/python3

Note: change the 1.1.1.1 and 2.2.2.2 with your droplets' IP addesses!

SSH configuration

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:
ssh-keygen
By default your public ssh key would be added to this file here:
~/.ssh/id_rsa.pub
Just copy the content of the file and add it to your 2 other servers in the following file:
~/.ssh/authorized_keys
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:
Ansible ping
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.
 

Wrap-up 

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!

Coffee For Me
Recent Posts
Proxies Demystified
Proxies Demystified
2019-11-05 09:31:05