Using Ansible Dynamic Inventory to deploy Web Server on AWS

In Ansible a static inventory file is a plain text file that contains a list of managed hosts declared under a host group using either hostnames or IP addresses. So we have to manually add managed host information if any new host come. So to overcome this issue Ansible also has concept of Dynamic inventory file which can pull inventory information from dynamic sources, like cloud sources, container services, etc.

So in this blog i will be deploying web server using Ansible dynamic inventory file on AWS. If you want to deploy it using static inventory file, you can refer to my previous blog .

Before we move further as usual here are few requirements:

  1. Basic knowledge of Cloud Computing (here AWS)
  2. Basic knowledge of Ansible like roles, playbook, vault, etc.
  3. Already configured Ansible tool
  4. Boto library pip3 install boto
  5. plugin file for ec2 dynamic inventory
  6. Optional: My Github repository

So here are the steps:

Step 1: Check your ansible configuration file

and edit it like given below

Step 2: Create Ansible Dynamic inventory directory. Create only where you have specified it in ansible.cfg file like below

Step 3: Download ansible dynamic inventory in your inventory directory and make it executable. You can get it from here using wget command.

Step 4: Provide your AWS IAM credentials as environment variable where you want to launch and manage ec2 services.
The dynamic inventory will use credentials directly from here.

Step 5: You can check the file by running command. It will show all information in your AWS profile

Step 6: Create 2 roles one for launching instance and one for configuring web server where you have specified roles_path in ansible configuration file

You can verify by using command

Step 7: This step is for creating roles file for both files. If you don’t want to create your own, you may skip to step 8 and get roles directory from my github repo

Role 1 => For launching ec2 instance

  • Now edit tasks/main.yml file of ec2_instance and write below module to launch ec2 instance
  • Provide credentials of AWS in vars/main.yml file

Optional: You can also encrypt your var file as shown below to protect your credentials

Step 8: Role 2=> For configuring web server

  • Edit tasks/main.yml file of web_server role
  • Create a template file in <templates/> directory in web_server role and you can write your configuration in this.
  • Also edit handlers/main.yml file and provide service module here so if any change made in configuration file and playbook is ran again then it will restart the service

Step 8: Create two playbook files one for ec2_instance role and one for web_server role

Step 9: Run the playbooks, first that launch instance and then which configure web server

Note: Use ask-vault-pass option in ec2_instance playbook and enter Vault password that if you have encrypted it earlier.

Launch ec2_instance playbook

Now launch web_server playbook

That’s it now you can access your web page from your browser

Note: If you are not able to see webpage then you may have not enable that port in Security Group of AWS that you have provided.

Here’s what are the inbound rules shoul look like:

So that’s it folks for this blog. Hope you liked it. See you in next blog.

Github repo: https://github.com/Apeksh742/Ansible_dynamic_inventory

LinkedIn Profile: https://www.linkedin.com/in/apeksh-agarwal-0543bb192/