Ansible with AWS EC2 — Part 1

Sachith Muhandiram
3 min readMay 13, 2020

--

A few months ago,I did some RnD tasks using ansible at VizuaMatix. After that I didnt use Ansible for anything useful, today I got YouTube suggestion for ansible again. After watching it I thought, I should get my hands dirty again with Ansible. I decided to do some Ansible tasks using AWS EC2.

For this , I have used a master node and a worker node. Both running Ubuntu 18.04.4 And task was very simple. Install VLC media player in our worker node, and then removing it.

First thing first, we must have Ansible installed in master and worker/s. To install just use :

sudo apt install ansible

Then we need to share our master’s public-key to worker/s. We create ssh key from master node and share it with worker node/s.

ssh-keygen -t rsa -N “” -f /home/ubuntu/.ssh/id_rsa

Then cat and copy the content cat .ssh/id_rsa.pub

Go to worker/s terminal and vim ~/.ssh/authorized_keys paste the content at the bottom of this file, save and exit. This step was explained here.

and check the connectivity, from master node try :

ubuntu@ec2–......-1.compute.amazonaws.com

If our key sharing is success, then you should be able to login to worker node without any password.

Then lets add our worker node/s to ansible. In master node open /etc/ansible/hosts file and add a group to it. Here I have named it as [workers] . Now onward, when you specify workers in your playbook, ansible knows what hosts to use when running playbook.

[workers]ubuntu@ec2-......1.compute.amazonaws.com

Save and then lets try to ping our worker node/s : ansible workers -m ping

This should generate an output similar to this with SUCCESS and pong

ubuntu@ec2-...-1.compute.amazonaws.com | SUCCESS => {
"changed": false,
"ping": "pong"
}

If you get something else, you would need to recheck all your settings. Upto now we didn't change any default setting in AWS-EC2 or Ansible. Now we are ready for installing VLC.

Installing a package using playbook

To deal with packages , Ansible uses play-books. Its written in yaml and its straightforward task. You can learn more about playbooks from official ansible guide.

This is our playbook to install vlc : installvlc.yaml

---
- name: installvlc # name of the playbook
hosts: workers # where we need to install
become: true # run as sudo user
tasks:
- name: Install VLC Media Player
apt: # the package manager
name: vlc-bin
state: latest
# if you want specific version you can specify in . state : 3.0.0

If playbook ran successfully, to run this simply use

ansible-playbook installvlc.yaml

If this works ,you will see something like this :

PLAY [installvlc] *********************************************************************************************************************TASK [Gathering Facts] ****************************************************************************************************************
ok: [ubuntu@ec2-....-1.compute.amazonaws.com]
TASK [Install VLC Media Player] *******************************************************************************************************
changed: [ubuntu@ec2-....-1.compute.amazonaws.com]
PLAY RECAP ****************************************************************************************************************************
ubuntu@ec2-....-1.compute.amazonaws.com : ok=2 changed=1 unreachable=0 failed=0

Here changed=1 means, playbook made a change in worker nodes. If we run the playbook again, you would see this changed=0 . Which means there is no state change.

From worker node : when you type vlc in terminal, it sould give you something like VLC media player 3.0.8 Vetinari (revision 3.0.8–0-gf350b6b5a7).

Uninstalling a Package with playbook

To remove a package, we just have to use the same syntax as installation playbook. But

state: absent

Create a new playbook, and make sure it’s state is changed and also task should be a meaningful name to distinguish the task. and run that playbook as ansible-playbook uninstallvlc.yaml

Conclusion :

Ansible is a great tool to do automation tasks. We no need to go and do changes in all the nodes, just change the yaml file and run the playbook. These are basics task, but ansible can be used to configure routers, IoT devices and many more.

Here I have shown how to install and remove a package using ansible. In next guides, lets do some complex stuff like adding patches, changing configurations etc.

--

--

Sachith Muhandiram
Sachith Muhandiram

Written by Sachith Muhandiram

DevOps who is willing to learn and try new things.

Responses (1)