LAUNCHING EC2 INSTANCE AND CONFIGURING WEB SERVER WITH DYNAMIC INVENTORY USING ANSIBLE
In this article, you will see how we can launch an ec2 instance using ansible.
Task Description
Task Description Launch an AWS instance with the help of ansible. Retrieve the public IP using dyanamic inventory which is allocated to the launched instance. With the help of the retrieved Public IP configure the web server in the launched instance.
Ansible is one of the best tool to configure any os. We don’t need to which command to run to configure the OS this intelligence comes from modules. Also, ansible has the capability to provision the os for this we have to use the ec2 Module.
Ansible role is an independent component that allows the reuse of common configuration steps. Ansible role has to be used within the playbook. Ansible role is a set of tasks to configure a host for certain purpose like configuring a service.
ansible-galaxy role init myweb
A role directory structure contains directories: defaults, vars, tasks, files, templates, meta, handlers. Each directory must contain a main.yml file that contains relevant content. Let’s look a little closer to each directory.
- defaults: contains default variables for the role. Variables in default have the lowest priority so they are easy to override.
- vars: contains variables for the role. Variables in vars have higher priority than variables in the defaults directory.
- tasks: contains the main list of steps to be executed by the role.
- files: contains files that we want to be copied to the remote host. We don’t need to specify a path of resources stored in this directory.
- templates: contains a file template that supports modifications from the role. We use the jinja templating language for creating templates.
- meta: contains metadata of role like an author, support platforms, dependencies.
- handlers: contains handlers that can be invoked by “notify” directives. Then only handlers executed.
Create one directory /etc/myrole only here we have to create roles because we have to set role directory in the ansible config file which is at /etc/ansible/ansible.conf
Create one role for ec2
Creating Vault
ansible-vault create --vault-id name@prompt vault-name.yml
This will create one vault here we have to store aws_access_key and aws_secret_key. create this file in the vars folder.
Role Task:
rolename/tasks/main.yml file
- name: "provisioning os on aws"
ec2:
key_name: "mykey"
instance_type: "t2.micro"
image: "ami-0ebc1ac48dfd14136"
wait: yes
count: 1
region: "ap-south-1"
instance_tags:
name: "ansible_ec2"
vpc_subnet_id: "subnet-061d276e"
group_id: "sg-0275750a36aa3fb24"
state: present
assign_public_ip: yes
aws_access_key: "{{access_key}}"
aws_secret_key: "{{secret_key}}"
the above code will launch the ec2 instance IAM user access key and the secret key stored in the vault.
Create Playbook
for running the playbook create any fileame.yml anywhere
give the vault file path find according to your finename.yml
- hosts: localhost
vars_files:
— cred.yml
roles:
— role: ec2ansible-playbook --vault-id name@prompt ec2.yml
Run Playbook
ansible-playbook --vault-id name@prompt ec2.yml
Create a new user and set a password to it and also give root power to it
useradd user_name
passwd user_name
Run command: visudo
Now generate SSH and key and copy to ec2
ssh-keygen
ssh-copy-id username@instanceIP
Create directory /etc/mydb and download the following files.
Setting Dynamic Inventory
wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.py
wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.ini
make ec2.py executable : chmod +x ec2.py export this command
export AWS_REGION= 'set region'
export AWS_ACCESS_KEY= xxxxxxxxxxxxxxxxxx
export AWS_SECRET_KEY= xxxxxxxxxxxxxxxxxx
Set file path in the ansible config file and run the ping module
Create a new role and store the following code in tasks/main.yml
- name: install httpd package
package:
name: "httpd"
state: present- name: starting web service
service:
name: "httpd"
state: started- name: copy web page from url
get_url:
dest: "/var/www/html"
url: "https://raw.githubusercontent.com/rohitraut3366/mulicloud/master/index.html"
create playbook
- hosts: all
roles:
- role: webserver
run the playbook
ansible-playbook name.yml
GitHub link: https://github.com/rohitraut3366/aws_webserver-using-ansible-on-ec2.git
I hope you had found this article interesting !!
Thank You !!