How to Install Ansible in AWS and Manage servers Via Ansible

How to Install Ansible in AWS

Commands to Install Ansible in AWS EC2 are given below

sudo dnf install ansible

ansible --version

How to Connect the Master Instance to the Child Instances

Copy the SSH Keys of Child Instances

Copy the SSH keys of the child instances in /home/ec2-user/key.pem

Then, create the hosts.ini file by running “nano hosts.ini” in /home/ec2-user/ directory

[web]
child1 ansible_host={child_ip} ansible_user=ec2-user ansible_ssh_private_key_file=/home/ec2-user/{file_path}

Verify if it’s able to connect

ansible -i hosts.ini web -m ping

How to Install Nginx using Ansible Playbook

The Ansible Playbook Code is Given Below

---
- name: Configure Nginx on Amazon Linux 2023
  hosts: web
  become: true
  tasks:
    - name: Install nginx (via dnf)
      dnf:
        name: nginx
        state: latest
        update_cache: yes

    - name: Ensure nginx is started and enabled
      service:
        name: nginx
        state: started
        enabled: yes

Now, run this to install nginx in the child instances

ansible-playbook -i hosts.ini nginx.yml

A detailed Video Tutorial is given below

Atiqur Rahman

I am MD. Atiqur Rahman graduated from BUET and is an AWS-certified solutions architect. I have successfully achieved 6 certifications from AWS including Cloud Practitioner, Solutions Architect, SysOps Administrator, and Developer Associate. I have more than 8 years of working experience as a DevOps engineer designing complex SAAS applications.

Leave a Reply