Table of Contents
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
