Provisioning EC2 Instance and deploying Webserver on it using single ANSIBLE playbook
This article is about provisioning a AWS Instance using Ansible Playbook, and after its creation, same ansible playbook will deploy apache webserver on this EC2 instance and hence we can see our Webpage running on PublicIP of Instance.
Technologies Used :-
Pre-requistes :- Basic Knowledge regarding ansible playbook (i.e how to write it), and basics of AWS EC2 Instance.
→ I have used IAM user “akhil” created in my AWS Account. For creating IAM user, follow this link below, to see how to create IAM user.
I have created a folder “task2” which is the main folder and contains, private_key , main.yml, vault.yml .
— I have created a vault named as “vault.yml” which contains my access key and secret access key and these 2 are stored in ak and sak variables respectively.
So, Let’s Start Building the Playbook : —
Initially in this playbook, we have installed boto, boto3 which are python libraries used to give interface to Amazon Web Services.
→ We have created our own Security Group for our instance using the ec2_group module.
- Name is “launch-wizard-1” and in deployed in default VPC in Mumbai region.
- I have allowed SSH and HTTP port (22 and 80) respectively for ingress and allowed traffic from anywhere.
We will use this security group in our instance.
→ Now, we will provision our EC2 Instance using our playbook, and we will ec2 module for this.
- Again, we have provided access key and secret access key.
- In this, we have given the details of our instance, such as instance_type, ami_id, region,no of instances to be created, security group, key_name, assigning public_ip and VPC_id.
We have to change the permissions of our private_key, to make it executable.
We want to create host group which will contain the IP Address of our Instance as our main task is to deploy webserver there. To do this, we will use add_host module.
I have used debug module to print the Public IP of our Instance.
Now, we want that after the creation of our instance, automatically, webserver is deployed on our instance. So, we want the IP Address of our Instance and then do ssh there to go inside and then deploy webserver there.
- We have installed webserver and then Created a file index.html and copy the content part there in the file, and then started the service and then enabling it which will make the state of our webserver as started permanently.
Last step is to provide our private_key to ansible for ssh to instance as it is the only way for our authentication. For this, we have to add path of our key to ansible configuration file.
- Privilege Escalation block is used to give special powers to ec2-user and hence can install webserver and start the service.
Now, everything had been setup, and good to go and Provision EC2 Instance.
We will run ansible command to run our playbook. Using Below Command :
# ansible-playbook — ask-vault-pass main.yml
As no error come in the output, it means that everything has been created successfully. We can run one time the same command and hence everything we will see be in the green color.
Now, we can see AWS Management Console to see deployment.
Now, we will do ssh using browser to see, whether our webserver is deployed or not.
Now, everything is deployed and running perfectly, now we check that if webpage is working or not.
Ansible Docs link for reference :
- For EC2_Group module
ec2_group - maintain an ec2 VPC security group - Ansible Documentation
AWS access key. If not set then the value of the AWS_ACCESS_KEY_ID, AWS_ACCESS_KEY or EC2_ACCESS_KEY environment…
2. For EC2 module
ec2 - create, terminate, start or stop an instance in ec2 - Ansible Documentation
When provisioning within vpc, assign a public IP address. Boto library must be 2.13.0+. debug_botocore_endpoint_logs…
You can refer this site for more modules such as debug, package, copy, service, add_host, wait_for_connection and lots more :
GITHUB URL :-
“I have practiced and gained all knowledge of this project(task) under the mentorship of Mr. VIMAL DAGA Sir during the Ansible Training by Linux World India.”
I hope this article is Informative and Explanatory. Hope you like it !!!
For any suggestions or if any reader find any flaw in this article, please email me to “email@example.com”
Thank You Readers!!!