DEPLOYING WEB SERVER ON AWS THROUGH ANSIBLE
Hello connections!!!!! I am back with the another article. In this we'll know how to launch an ec2 instance using ansible and deploying web server on AWS through Ansible. Its an very interesting task!!!!
First, let's have an description of task:
??Provision EC2 instance through ansible. ??Retrieve the IP Address of instance using dynamic inventory concept. ??Configure the web server through ansible!
Ansible is an open-source automation tool that uses playbooks to enable you to make deployments faster and scale to various environments. Think of playbooks as recipes that lay out the steps needed to deploy policies, applications, configurations, and IT infrastructure. You can use playbooks repeatedly across multiple environments. Customers who use Ansible playbooks typically deploy periodic changes manually. As complex workloads increase, you might be looking for ways to automate them. In this post, we show you how to automate an Ansible playbook deployment using Amazon Elastic Compute Cloud (Amazon EC2) and GitHub.
We can use our localhost IP address to behave as a managed node and we will use Software Development Kit(SDK) to launch EC2 instance on AWS as ansible is built on python language so will be using boto3. For installing boto3, just run the command(remember that yum is configured on redhat local machine) : pip3 install boto3
Now, create an IAM(Identity Access Management) user. While creating, remember it to give "ALL THE ADMINISTRATOR ACCESS". Note down the SECRET ACCESS KEY and ACCESS KEY ID. Then use the default security groups and key pair you created or you have.
NOTE: KEY PAIR MUST BE IN .pem form only.
Finally our IAM user is created.
Now let's start to write our playbook i.e. our yaml file for provisioning EC2 instance. (First copy your key pair in your local host using winscp). Following is the yaml file:
Use the command chmod 400 key_name.
Now, write your variable file(in my case secret.yml) and inside that initialize the AWS_ACCESS_KEY_ID and SECRET_ACCESS_KEY as variables.
Now, encrypt the variable file using command: ansible-vault encrypt filename. Set the Vault Pass and you have successfully encrypted your secret file for security reasons. Output will be similar to the below pic:
Run the playbook using command: ansible-playbook --ask-vault-pass filename. Following must be the result:
Finally our ec2 instance will be created.
Now install wget command. Go to newly created workspace directory(in my case mydb) and run the following commands:
wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.py
The above command will create a ec2.py dynamic inventory file.
wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.ini
The above command will create a ec2.ini dynamic inventory file.
Now to make the both the dynamic inventory files executable, use the following commands:
chmod +x ec2.py chmod +x ec2.ini
Now, edit AWS_REGION='ap-south-1', AWS_ACCESS_KEY_ID=XXXX, AWS_SECRET_KEY=XXXX in the ec2.ini file.
Now export all those commands:
Now edit the ec2.py file. This file is written in python2 but we are using python3 so we need to edit the header, #!/usr/bin/python3.
WHAT EXACTLY IS DYNAMIC INVENTORY?
As described in working with dynamic inventory, Ansible can pull inventory information from dynamic sources, including cloud sources, using the supplied inventory plugins. If the source you want is not currently covered by existing plugins, you can create your own as with any other plugin type.
Ansible supports dynamic inventory scripts that retrieve current information from these types of sources whenever Ansible executes, allowing the inventory to be updated in real time. These scripts are executable programs that collect information from some external source and output the inventory in JSON format.
Dyanmic inventory scripts are used just like static inventory text files. The location of the inventory is specified either directly in the current ansible.cfg file, or using the -i option. If the inventory file is executable, then it is treated as a dynamic inventory program and Ansible attempts to run it to generate the inventory. If the file is not executable, then it is treated as a static inventory.
Now do the necessary changes in the config file of ansible,
Now ansible will know that how many ec2 instances are running on AWS. You can check via below commands:
Now let's configure the web-server using the ansible playbook and also write .html file and copy .html file to document root of httpd server i.e., /var/www/html/
Below is my html code:
Let's write the playbook for configuring WEB-SERVER:
Now let's run the playbook:
As the code successfully run. then we can check the webpage via. public ip of ec2 instance/file.html.
Finally, our task is completed!!!!
I hope you find this article interesting!!!!!!!!
Thank You!!!!!!!!!
Sr DevOps at barq | 2xAWS Certified | 3xAzure Certified | MLOps | Terraform | Ansible | Jenkins | ArgoCD | CloudComputing | K8s | Blogger
4 年Nice one Vrushali Mahajan