Table of contents
Task 1
Create 3 EC2 instances. make sure all three are created with the same key pair
Install Ansible on a host server
Connect the master server by using SSH.
To add the Ansible PPA (Personal Package Archive) repository, you can execute the command below:
sudo apt-add-repository ppa:ansible/ansible
Update the package.
Install Ansible using the following command:
sudo apt install ansible
Verify the installation.
Copy the private key from local to the Host server (Ansible_host) at (/home/ubuntu/.ssh)
Go to the .ssh folder and copy the path.
Run the below command to copy the ansible key file on the server followed by the copied path
sudo scp -i "ansible-key.pem" ansible-key.pem ubuntu@ec2-18-232-124-214.compute-1.amazonaws.com:/home/ubuntu/.ssh
Check the folder again and see if the key file is there. Now change the permission of the key file
Access the inventory file using sudo vim /etc/ansible/hosts
Edit the hosts file by using
sudo vim /etc/ansible/hosts
command.Once you have added the host information to the file, you can check the list of hosts that Ansible can work with by using the "
ansible-inventory --list -y
" command.Ping the nodes by using
ansible servers -m ping
command.
Create a playbook to install Nginx
Create a playbooks folder and inside that folder create a file.
Paste the below code in the file.
--- - name: This playbook will install nginx hosts: all become: true tasks: - name: Update apt apt: update_cache: yes - name: Install nginx apt: name: nginx state: latest - name: start and enable nginx service: name: nginx state: started enabled: yes
Run the playbook by using the ansible-playbook command
Check on the server to verify if the Nginx is installed or not.
Deploy a sample webpage using the ansible-playbook
Create an index.html file on the master server.
Modify the ansible-playbook to transfer the index.html file to the Nginx location.
Run the playbook again.
Enter the public IP address of one of the EC2 instances, it is working fine.
Check another server, it is working on all the servers.
Thank you for reading!!
~Shreya Gupta
Great initiative by the #trainwithshubham community. Thank you Shubham Londhe
#devops #90daysofdevops #ansible #ansibleproject