Deploy webserver on AWS using Ansible Dynamic inventory

Deploy webserver on AWS using Ansible Dynamic inventory

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

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
No alt text provided for this image
  • 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
No alt text provided for this image
No alt text provided for this image

And then run your playbook

No alt text provided for this image

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..

No alt text provided for this image

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

No alt text provided for this image

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 ...
No alt text provided for this image
No alt text provided for this image

Now OUTPUT:

Copy the public ip of instance on broweser and check it is working or not:

No alt text provided for this image

Thanks for reading this article , hope you like it ........


要查看或添加评论,请登录

Rahul Kumar的更多文章

社区洞察

其他会员也浏览了