Installing and Configuring Ansible on an EC2 Instance
STEP 1 : LAUNCH EC2 INSATANCE

STEP 2 : CREATE THREE EC2 INSATANCE
STEP 3 : NAME OF THE EC2 INSATANCE
STEP 4 : SELECT AMAZON LINUX

STEP 5 : SELECT THE ARCHITECTURE

STEP 6 : SELECT THE INSTANCE TYPE

STEP 7 : CREATE THE KEY PAIR

STEP 8 : CREATE THE KEY PAIR

STEP 9 : SELECT THE KEYPAIR

STEP 10 : ALLOW HTTP AND SSH TRAFFIC

STEP 11 : LAUNCH INSTANCE

STEP 12 : GO TO INSTANCE

STEP 13 : RENAME THE INSTANCE

STEP 14: CONNECT THE ANSIBLE SERVER INSTANCE

STEP 15: INSTALL ANSIBLE IN ANSIBLE SERVER INSTANCE
Run the following command in the terminal to install Ansible:
$ sudo su -
$ yum install ansible -y
(running sudo su – grants you superuser (root) access on the system.)


Step 16 : create host.ini , host.pem , playbook.yaml file
- touch host.ini → Creates an empty inventory file for Ansible hosts.
- touch host.pem → Creates an empty private key file (likely for SSH authentication).
- touch playbook.yaml → Creates an empty Ansible playbook file.
- ls→ Lists the files in the current directory.
$ touch host.ini
$ touch host.pem
$ touch playbook.yaml
$ ls

Step 17: Copy the vpckey.pem File Content
Go to the downloads from your system and search for the key pair here we have created the vpc as keypair file open the pem file and Copy the content of the vpc.pem file that was downloaded while creating the three EC2 instances


Select and copy the entire content of the file.

Select and copy the entire content of the file.
Open a file (host.pem) to store the copied content

$ vi host.pem
press on ‘I’ from the keyboard to make it insert mode by pressing: i
Paste the copied content into the host.pem file.
Exit insert mode by pressing: Esc
Save and close the file by typing “:wq”

copy the host.pem file and paste into /tmp/host.pem
$ cp host.pem /tmp/host.pem


Change the permission to readable.

Step 18: Configure host.ini
Open the host.ini file using a text editor.

$vi host.ini
Enter insert mode by pressing: i
[docker]
ec2-34-203-225-21.compute-1.amazonaws.com
[httpd]
ec2-54-86-174-49.compute-1.amazonaws.com
[docker:vars]
ansible_user=ec2-user
ansible_ssh_private_key_file=/tmp/host.pem
[httpd:vars]
ansible_user=ec2-user
ansible_ssh_private_key_file=/tmp/host.pem
host 1 public DNS- ec2-34-203-225-21.compute-1.amazonaws.com -docker
host 2 public DNS – ec2-34-203-225-21.compute-1.amazonaws.com -httpd
Exit insert mode by pressing: Esc
Save and exit the file: “:wq“

Step 19: Configure Host1 for Docker and Host2 for HTTPD
“vi playbook.yaml” It is used to edit the file.

Please copy the content above and add it to the playbook.yaml file.
- name: Install Docker using Ansible Playbook
hosts: docker
become: true
tasks:
- name: Install Docker package
package:
name: docker
state: present
- name: Start and enable Docker service
service:
name: docker
state: started
enabled: yes
- name: Install HTTPD using Ansible Playbook
hosts: httpd
become: true
tasks:
- name: Install HTTPD package
package:
name: httpd
state: present
- name: Start and enable HTTPD service
service:
name: httpd
state: started
enabled: yes
Press i to enter “insert mode” and make the necessary changes.
After editing, press Esc to exit insert mode.
To save the file and exit, type :wq and press Enter.

Step 20: Configure Host1 for Docker and Host2 for HTTPD
- ansible–playbook: Executes an Ansible playbook.
- -i host.ini: Specifies the inventory file (host.ini) that contains the target hosts for deployment.
- playbook.yaml: The Ansible playbook file defining the tasks to be executed on the target hosts.
$ ansible-playbook -i host.ini playbook.yaml

- host1 will have Docker installed and running.
- host2 will have Apache HTTPD installed and running.
Now both servers are configured automatically using Ansible!

For more information Click here
For more information Visit Cloud Institution