Deploy webserver on AWS using Ansible Dynamic inventory
Rahul Kumar
DevOps Engineer || 3x GCP Certified || DevOps || Docker || kubernetes || OpenShift || Ansible || Jenkins || AWS || Terraform || Immediate joiner
Deploy Web Server on AWS through ANSIBLE!
??Provision EC2 instance through ansible.
??Retrieve the IP Address of instance using a dynamic inventory concept.
??Configure the webserver through ansible!
Steps for configuring dynamic inventory
- Go to your os
- create a ansible config file
- make a dir /mydb
- For the dynamic inventory, download ec2.py and ec2.ini from this given URL, and paste in /mydb folder:
- https://github.com/ansible/ansible/tree/stable-2.9/contrib/inventory
- now add this path to ansible.cfg
- and make a new inventory file , in that inventory mention this loaction /mydb
Configure key for aws ec2-instance
- After that you also need to copy the key.pem for ec2 instance launch.
- After copying your key, make it executable by this following command.
- chmod 600 keyname.pem
- Here you also need privilege escalation because in aws we have to configure all the configuration done by the user root only.
- After this edit your ec2.py
- change your python path in my case it is /usr/bin/python3.
- Save it.
- After this make this file executable by this following command:
- chmod +x ec2.py
- After this you need to configure by your IAM user credentials, so that we can commute aws cloud and launch ec2 instance.
- Here we need to type some command to configure IAM user
- export AWS_REGION='region'
- export AWS_ACCESS_KEY_ID= 'access_id'
- export AWS_SECRET_ACCESS_KEY='secret_key'
- Here u need to type region, AWS access key, AWS secret key provided by the IAM user.
Launch ec2 instance using ansible
- first we have yo install boto using command .
- pip/pip3 install boto/boto2
- now after that create a playbook or you can also create roles
- here i have created playbook in which i kept variable in a diffrent var file secure.yml
- secure.yml is password protected file , no one can open it without using password...
- here is my playbook and secure.yml file
And then run your playbook
Now we have succesfully launched our instance and using dynamic inventory we will get the ip of this instance ..
After getting ip create other inventory file and write this ip and provide credential and all privillages in .cfg file..
Then check list host using command
ansible all --list-hosts
[root@localhost ~]# ansible all --list-hosts
hosts (1):
35.154.191.16
check it is pinging ot not
Now we are done with 2 step that is
- we have launched ec2 instance using ansible playbook and
- got dynamic ip using which can connect to our ec2 instance
Now our final step is to configure web server on our managed node
- for this i have created a playbook which will configure webserver in managed node
- as we know there is 3 step to configure web server
- so , i have written three task in my playbook
- first tasks is to install httpd.
- 2nd task is to copy the source file to destination ., /var/www/httpd
- 3rd and last task is to restart httpd service ...
Now OUTPUT:
Copy the public ip of instance on broweser and check it is working or not: