Cloud Institution

Installing and Configuring Ansible on an EC2 Instance

STEP 1 : LAUNCH EC2 INSATANCE

Ansible Configuration

STEP 2 : CREATE THREE EC2 INSATANCE

STEP 3 : NAME OF THE EC2 INSATANCE

STEP 4 : SELECT AMAZON LINUX

ansible configuration

STEP 5 : SELECT THE ARCHITECTURE

ansible configuration

STEP 6 : SELECT THE INSTANCE TYPE

ansible configuration

STEP 7 : CREATE THE KEY PAIR

ansible configuration

STEP 8 : CREATE THE KEY PAIR

ansible configuration

STEP 9 : SELECT THE KEYPAIR

ansible configuration

STEP 10 : ALLOW HTTP AND SSH TRAFFIC

ansible configuration

STEP 11 : LAUNCH INSTANCE

ansible configuration

STEP 12 : GO TO INSTANCE

ansible configuration

STEP 13 : RENAME THE INSTANCE

ansible configuration

STEP 14: CONNECT THE ANSIBLE SERVER INSTANCE

ansible configuration

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.)

ansible configuration
ansible configuration

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
				
			
ansible configuration

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

ansible configuration
ansible configuration

Select and copy the entire content of the file.

ansible configuration

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

ansible configuration
				
					$ 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”

ansible configuration

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

				
					$ cp host.pem /tmp/host.pem
				
			
ansible configuration
ansible configuration

Change the permission to readable.

ansible configuration

Step 18: Configure host.ini

Open the host.ini file using a text editor.

ansible configuration
				
					$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

ansible configuration

Step 19: Configure Host1 for Docker and Host2 for HTTPD

“vi playbook.yaml” It is used to edit the file.

ansible configuration

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.

ansible configuration

Step 20: Configure Host1 for Docker and Host2 for HTTPD

  • ansibleplaybook: 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
				
			
ansible configuration
  • host1 will have Docker installed and running.
  • host2 will have Apache HTTPD installed and running.

Now both servers are configured automatically using Ansible!

ansible configuration

For  more information  Click here 

For more information Visit Cloud Institution

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top