Deploy of web server on AWS through Ansible

Divya Kurothe
3 min readNov 2, 2020

--

Ansible is mostly known for configuration management. It’s used when provisioning new servers. We can also use it to deploy our web applications. We can launch and configure many instances with the help of ansible.

Statement : Deploy Web Server on AWS through ANSIBLE!

♦️Provision EC2 instance through ansible.

♦️Retrieve the IP Address of instance using dynamic inventory concept.

♦️Configure the web server through ansible

♦️Create role for webserver to customize the Instance and deploy the webpage to root directory.

Solution:

Most Ansible modules are written in Python, including the ones central to letting Ansible work. . Hence for AWS Cloud Python has one library called boto which connect the Ansible to contact with AWS API. Boto is a Python package that provides interfaces to Amazon Web Services, so we need to install boto library using following command.

pip3 install boto

To launch an Instance on AWS cloud using Ansible we use Ansible ec2 module. For AWS login we require two types of keys as public access key and secret access key . We can use secret key and access key directly in playbook but for security purpose we rather store these credentials in Ansible vault. For this we need to create the yaml file and store the values of access key and secret key and after this we use the following command to encrypt the file using Ansible vault

ansible-vault encrypt file_name

We now have to configure a web server for ec2 instance .For that we have to do SSH login to go inside ec2 Instance .As all these things we going to do using Ansible which requires public ip of ec2 instance and SSH Private key to login.

To add the ssh-key to your system so that Ansible can log in without any problem. this can be done using the below commands

host_key_checking = FALSE
ssh-add /path/../your_key.pem

Now we create ec2 yaml file to mention required details for launching webserver.

- hosts: localhost
gather_facts: no
vars_files:
- keys.yml
tasks:
- name: ec2 webserver
ec2:
key_name: "key_name"
instance_type: "t2.micro"
image: "your_AMI"
wait: "yes"
count: 1
vpc_subnet_id: "subnet_id"
assign_public_ip: yes
region: "data-center_region"
state: present
group_id: "security_group"
instance_tags:
Name: webserver
aws_access_key: "{{ accesskey}}"
aws_secret_key: "{{ secretkey }}"
register: ec2
- debug:
var: ec2.instances[0].public_ip

keys.yaml :

accesskey: "Your ACCESSKEY"
secretkey: "Your SECRETKEY"

We are going to add ec2 instance to the host group of Ansible dynamically using keyword add_host .This add_host keyword used to add the hosts to ansible inventory file dynamically by creating host group

- name: SSH Group to login dynamically 
add_host:
hostname: {{ item.public_ip }}
groups: ec2_server
loop: "{{ec2.instances}}"
- name: wait for ssh to start
wait_for:
host: "{{ item.public_ip }}"
port: 22
state: started
loop: "{{ec2.instances}}"

Now we can configure the web server and put up our code inside the root directory.

- hosts: all
remote_user: "ec2-user"
gather_facts: yes
become: yes
tasks:
- name: install httpd and php
package:
name:
- httpd
- php
state: present
- name: add index.html
copy:
content: "AWS from Ansible"
dest: /var/www/html/index.html

- name: start httpd
service:
name: httpd
state: started

We can now run the playbook after doing all the above tasks using command

ansible-playbook  --ask-vault-pass  playbook_file_name

Now we can run the playbook

--

--

No responses yet