?? Ansible Task-2??
Tushar Dighe
DevOps/Cloud Engineer | Kubernetes | GCP | AWS | Azure | Wiz | Databricks| Cycode | Security
Deploying Web Server on AWS through ANSIBLE!
TASK DESCRIPTION:
?? 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.
Let’s start with basic concepts.
What is Ansible??
Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code.It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows. It includes its own declarative language to describe system configuration. Ansible was written by Michael DeHaan and acquired by Red Hat in 2015. Ansible is agentless, temporarily connecting remotely via SSH or Windows Remote Management (allowing remote PowerShell execution) to do its tasks.
What is Amazon Web Services??
Amazon Web Services (AWS) is a subsidiary of Amazon providing on-demand cloud computing platforms and APIs to individuals, companies, and governments, on a metered pay-as-you-go basis. These cloud computing web services provide a variety of basic abstract technical infrastructure and distributed computing building blocks and tools. One of these services is Amazon Elastic Compute Cloud (EC2), which allows users to have at their disposal a virtual cluster of computers, available all the time, through the Internet.
What is Role??
Roles provide a framework for fully independent, or interdependent collections of variables, tasks, files, templates, and modules. In Ansible, the role is the primary mechanism for breaking a playbook into multiple files. This simplifies writing complex playbooks, and it makes them easier to reuse.
So Let’s start to our task !!!
Step 1: Install Amazon Web Service (AWS) Software Development Kit (SDK)
Boto is AWS SDK. For connecting AWS we need to install boto library in our local environment.
pip3 install boto
Step 2: Launch AWS Instance
We have to write yml code for launch AWS instance.
->aws.yml
- name : Launch AWS Instance hosts : localhost vars_files : - secure.yml tasks : - name : Launch EC2 Instance ec2 : key_name : task2 instance_type : t2.micro image : ami-0ebc1ac48dfd14136 wait : "yes" count : 1 vpc_subnet_id : subnet-c9f6cca1 group_id : sg-0045c82f1f125e7e4 assign_public_ip : yes region : ap-south-1 state : present aws_access_key : "{{ username }}" aws_secret_key : "{{ password }}" register: x - debug: var: x.instances[0].public_ip
Now we have to create one more playbook called secure.yml to store aws_access_key and aws_secret_key.As we know this keys can’t be leave as public so we encypt this playbook with vault encyption.
->secure.yml
Now we launch instance by running aws.yml playbook.Let’s see what happen..
AWS Instance Launched Successfully !!!
Step 3: Fetching IP of instance dynamically we want to download ec2.py and ec2.ini scripts from github.
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
This scripts help to fetch IP dynamically.
Now to execute this files for retrive IP we should change their permission
chmod +x ec2.py chmod +x ec2.ini
For execution we need to export some variables.
export AWS_REGION='ap-south-1' export AWS_ACCESS_KEY_ID='aws_access_key' export AWS_SECRET_ACCESS_KEY='aws_secret_key'
This variables export to our system to AWS and retrive IP.
Now we can check our IP
./ec2.py
Now check that our ip is properly pinging or not.
Step 4: Create role for webserver and deploy it
First we create one role
ansible-galaxy init role_name
Now we have to configure ansible.cfg to find path of role.
Now in task folder we write our code in main.yml file.
Lets create ansible playbook to run instance role and run playbook.
Now our Ansible playbook runs successfully.
Now we connect our AWS instance and check whether our webserver is running or not.Its running…..
Finally it's Done....
Thanks for reading..................