Ansible with AWS and EC2
Translation of the article was prepared on the eve of the start of the course “Infrastructure platform based on Kubernetes”…
I suggest you roll up your sleeves and dive into Ansible tasks using AWS EC2. We’ll walk through how to install and uninstall a package using Ansible.
Several months ago I had a chance to work with RnD tasks in VizuaMatix using ansible… Since then I have not used Ansible for anything more or less useful, but today through YouTube I received an offer back to ansible again… After watching it, I thought I should roll up my sleeves again and tackle Ansible. I decided to complete several Ansible tasks using AWS EC2.
To do this, I used the master node (hereinafter “master”) and the slave worker (worker node, hereinafter “worker”) nodes. Both are running Ubuntu 18.04.4. The task was very simple. Install VLC media player on our work node and then uninstall it.
First, we need to install Ansible on master and worker (s)… To install, just use:
sudo apt install ansible
Next, we need to share our master’s public key with the worker. We create an ssh key from the master and pass it to the workers.
ssh-keygen -t rsa -N “” -f /home/ubuntu/.ssh/idrsa
Then run cat
and copy the content cat .ssh/idrsa.pub
Go to the worker terminal and vim ~/.ssh/authorized_keys
paste content at the bottom of this file, save and exit. This step is explained here…
Check the connection, from the wizard try:
ssh -i ~/.ssh/id_rsa username@instance_ip
If our key transfer is successful, then you can log into the worker without a password.
Next, let’s add our workers to ansible. On the wizard, open the file /etc/ansible/hosts
and add a group to it. I named her [workers]
… Now when you point workers
in its playbook, ansible knows which hosts to use when starting the playbook.
[workers]
[workers]
node_ip_address
Save and then we will try to check the connection with our workers: ansible workers -m ping
This should return a result similar to the following with SUCCESS
and pong
| SUCCESS => {
"changed": false,
"ping": "pong"
}
If you get anything else, you should double-check all your settings. So far, we have not changed any of the default settings in AWS-EC2 or Ansible. We are now ready to install VLC.
Installing a package using Playbook
Ansible uses a playbook to work with packages. It is written in yaml
and has a fairly straightforward structure. You can read more about playbooks from the official ansible guides…
This is our new playbook for vlc installation: installvlc.yaml
---
- name: installvlc # имя playbook
hosts: workers # куда нужно устанавливать
become: true # запускать как sudo user
tasks:
- name: Install VLC Media Player
apt: # управление пакетами
name: vlc-bin
state: latest
# если вам нужна конкретная версия, вы можете указать это в state: 3.0.0
If the playbook started successfully, just use
ansible-playbook installvlc.yaml
If everything is working correctly, 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
changed=1
here means the playbook has made changes to the worker. If we run the playbook again, you will see changed=0
… This means that there was no state change.
From the worker: when you enter vlc
in the terminal, it should return you something like VLC media player 3.0.8 Vetinari (revision 3.0.8–0-gf350b6b5a7)
…
Removing a package using Playbook
To remove a package, we just need to use the same syntax as in the installation instructions, except:
state: absent
Create a new playbook and make sure its state is changed. Also, the task should have a descriptive name so that you can distinguish tasks. Run this playbook as ansible-playbook uninstallvlc.yaml
Conclusion
Ansible is a great tool for solving automation tasks. We don’t need to go and make changes to all nodes ourselves, just change yaml
file and run playbook. These are basic tasks, but ansible can be used to configure routers, IoT devices, and more.
Here I showed you how to install and uninstall a package using ansible. In the next tutorials, we will tackle more advanced things like adding patches, changing configurations, etc.
Free Demo-lesson on the topic: “Improving the reliability of deployment in Kubernetes”