Ansible playbooks are amazing, as you learned yesterday. What if you deploy a simple web app using Ansible, sounds like a good project, right?
- Create 3 EC2 instances. make sure all three are created with the same key pair.
Navigate to the EC2 service and launch three EC2 instances.
The three EC2 instances should be created, with one being designated as the Ansible control node (ansible master) and the other two as managed nodes (node1 and node2).
- Install Ansible on the host server
Connect to your EC2 instance using SSH.
Add the Ansible PPA repository using the following command:
sudo apt-add-repository ppa:ansible/ansible
Update the package
Install Ansible using the following command:
sudo apt install ansible
Once the installation is complete, you can check the version of Ansible using the following command:
ansible --version
Copy the private key from the local to the Host server (ansible master) at (/home/ubuntu/.ssh)
Copy the private key from your local.
Create a new file at /home/ubuntu/.ssh, paste the private key in it, and then continue with the setup by copying the private key to the Ansible control node (ansible master).
Give permissions to the private key file using chmod command.
In case of facing any error, you can elevate the permission of the key*( eg: sudo chmod 666 key_name)*
Access the inventory file using sudo nano /etc/ansible/hosts
Create inventory file at location /etc/ansible/hosts which is by default location of the file. An Ansible hosts file is a configuration file that contains a list of hosts or servers. Add the IP addresses of the servers and also add private key file locations to use for authentication.
Create a playbook to install Nginx
In this playbook, we first give the hosts we want to target as well as the playbook's name (Install nginx). (servers). In order to start the tasks with root permissions, we also set become: yes.
The first task uses the apt module to update the apt cache on the controlled nodes. Using the same module, the second task installs the most recent version of Nginx. The third step starts the Nginx service by supplying the service name (nginx) and setting the status parameter to start using the service module.
Use the ansible-playbook command to execute a playbook.
ansible-playbook file-name.yml
Check the status of Nginx on the two EC2 instances,
Deploy a sample webpage using the Ansible playbook
Create a new file index.html in the playbook directory, and add some sample content
Update ansible playbook file, install_nginx.yml, in the playbook directory:
This playbook will copy the index.html file to the default Nginx web server document root directory at /var/www/html/.
Run the playbook
Open a web browser and type the public IP address of one of the EC2 instances running Nginx after the playbook has finished running.
The website will be present on all the servers, so check them all!
You can add more layers or improvise more on your own; add this project to your résumé! Share it with your peers if it even slightly expands your understanding🙌❤️