Ansible with AWS and EC2
Let's get our hands dirty with Ansible and tasks using AWS EC2. We'll go over how to install and remove a package using Ansible.
Join the DZone community and get the full member experience.
Join For FreeA few months ago, I did some RnD tasks using ansible at VizuaMatix. After that I didn't use Ansible for anything useful, today I got a 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 the task was very simple. Install the 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 a ssh key from the 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 :
If our key sharing is successful, then you should be able to login to the worker node without any password.
Then let's add our worker node/s to ansible. In master node, open /etc/ansible/hosts
the 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 the playbook.
[workers]
Save and then let's try to ping our worker node/s : ansible workers -m ping
This should generate an output similar to this with SUCCESS
and pong
| SUCCESS => { "changed": false, "ping": "pong" }
If you get something else, you would need to recheck all your settings. Up to now we didn't change any default setting in AWS-EC2 or Ansible. Now we are ready for installing VLC.
Installing a Package Using the Playbook
To deal with packages, Ansible uses playbooks. It's written in yaml
and its straightforward task. You can learn more about playbooks from the 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 the 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: []
TASK [Install VLC Media Player] ******************************************************************************************************* changed: []
PLAY RECAP **************************************************************************************************************************** : 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 should 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 the installation playbook. But
state: absent
Create a new playbook, and make sure it states 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 tasks, 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 the next guides, let's do some complex stuff like adding patches, changing configurations, etc.
Opinions expressed by DZone contributors are their own.
Trending
-
Leveraging FastAPI for Building Secure and High-Performance Banking APIs
-
GitLab Pages Preview
-
Overcoming Challenges in UI/UX Application Modernization
-
Idempotent Liquibase Changesets
Comments