Module 5: Ansible Assignment - 1

Tasks To Be Performed:**

  1. Setup Ansible cluster with 3 nodes
  2. On slave 1 install Java
  3. On slave 2 install MySQL server

Do the above tasks using Ansible Playbooks

EC2 instances

Ansible Control Machine 10.0.1.223 Slave1 10.0.1.65 Slave2 10.0.1.233

^a2c939

  1. I install Ansible on the control node:
  • If I’m using a Debian-based system such as Ubuntu, I install Ansible with:
sudo apt update -y
sudo apt install ansible -y


Another install option

This method adds the Ansible PPA (Personal Package Archive) to your system and install Ansible from that source:

sudo add-apt-repository -y ppa:ansible/ansible
sudo apt update -y
sudo apt install ansible -y

This method will install the latest version of Ansible from the Ansible PPA, which is often more recent than the version available in the default repositories.

  1. Setup SSH .pem

In order to establish a temporary and secure connection to slave instances for inspection purposes, I load the key into the session once at the start using ssh-agent utility. ^1cba58

ubuntu@ip-10-0-1-223:~$ ls
daro.io.pem
ubuntu@ip-10-0-1-223:~$ chmod 600 daro.io.pem
ubuntu@ip-10-0-1-223:~$ eval $(ssh-agent -s)
Agent pid 2707
ubuntu@ip-10-0-1-223:~$ ssh-add daro.io.pem
Identity added: daro.io.pem (daro.io.pem)
ubuntu@ip-10-0-1-223:~$ ssh-add -l
2048 SHA256:wq3aSIyEU9dbylJv50YOhgXXPfcxZT7J5EmRIjLXZeA daro.io.pem (RSA)

Tested connection to one of the slaves Slave1

  1. I set up the inventory file:
  • I’ll create inventory file, at current working directory /home/ubuntu, using a text editor like nano: ^29ce4e
nano inventory.ini
  • Inside the file, I create a group [nodes] and add my three nodes under this group, replacing the placeholders with the actual IP addresses and usernames:
[nodes]
slave1 ansible_host=10.0.1.65 ansible_user=ubuntu
slave2 ansible_host=10.0.1.233 ansible_user=ubuntu

I’ll ping the hosts using the ping module to test connection

ansible all -m ping -i inventory.ini

^c4a4b0

Success


  1. I create the Ansible playbook:
  • I open a new file called myplaybook.yml with my text editor:
nano myplaybook.yml
  • I then proceed to write my playbook, defining the tasks to install Java on slave1 and MySQL on slave2.
---
- hosts: slave1
  become: yes
  tasks:
    - name: Install Java on Slave 1
      ansible.builtin.apt:
	    update_cache: yes
        name: default-jdk
        state: present
 
- hosts: slave2
  become: yes
  tasks:
    - name: Install MySQL server on Slave 2
      ansible.builtin.apt:
	    update_cache: yes
        name: mysql-server
        state: present
  1. I execute the Ansible playbook:
  • In the terminal, I navigate to the directory where my myplaybook.yml is located.
  • I run the playbook with:
ansible-playbook -i inventory.ini myplaybook.yml

Success