Deploying Webserver on AWS using Ansible
Srishti Jain
DevOps Engineer || Azure 1x || CI/CD || AKS || Jenkins || Kubernetes || Git
This task involves the following steps:
1. Launching an EC2 instance.
2. Update inventory dynamically for EC2 instance.
3. Configure Web Server using the ROLE in Ansible.
Ansible
Ansible is a tool for configuration management. Its provided by Python internally & comes with Python 3. Hence for Ansible installation we need to use pip3 python package manager. pip3 install Ansible is the command to install Ansible. We can verify installation using Ansible — version command. This should be the latest Ansible version i.e. 2.9.11.
Automation approach of ansible
This uses declarative language & is independent of the O.S. configuration on which it needs to be run. It’s intelligent enough because it knows how to do the configuration/installation of softwares on other O.S. platforms. e.g. Ansible
Ansible uses inventory which is list of IP addresses of managed nodes. By default Config file name is ansible.cfg , its stored under /etc. Pip does not create config file during Ansible installation.
Ansible has 2 types of nodes .
- Controller node — On this node Ansible is installed.
- Managed nodes — These nodes are managed by controller node on which Ansible performs configuration (software installation,any other config changes)
- Inventory — This is a database which stores IP of managed nodes which is required by Ansible config file.
- Ansible config file contains the path of inventory file to access managed nodes & perform configurations. Inventory path is given under defaults header.
Lets Get started
First install ansible and boto in rhel8 using commad: pip3 install anisble boto3 .Boto is the Amazon Web Services (AWS) SDK for Python. It enables Python developers to create, configure, and manage AWS services, such as EC2 and S3. Boto provides an easy to use, object-oriented API, as well as low-level access to AWS services. In my system i have already installed .
Next we have to setup the hostfile with contains the ip address of the managed nodes and also we need to configure file under /etc/ansible/ansible.cfg where we will provide the path of hostile.
Now we can ping to ensure the connectivity with ansible .
Now In we’ll need to use ansible-vault to store senstive information of IAM user’s access key id & secret key using which will be used at the AWS console during authentication.We can achieve this using ansible-vault encrypt (file name) .
We’ll be using Ec2 module & provide required parameters as under
This playbook will create ec2 instance in AWS and will fetch the public ip of the ec2 instance add it will add the ip to hostfile we have used add_host module to add the ec2 host in ansible and ec2.instances[0].public_ip command to fetch the ip of newly created instance. Now we will run the playbook.
Now we can see that the ec2 ip have been added in our host file now we can create our inventory file in Controller Node ,since we know the IP address of EC2 instance. In the inventory file we’ll need to mention the path of private key file which was used to launch the EC2 instance.
In the ansible config file we need to include privilege_escalation so that code will be run as root user & it will install Apache Web Server on EC2 instance which in our case is a managed node now.Ansible Config file in controller node will be as under.
We can create index.html in controller node as under
Use below code to install Apache Server, copy index file onto EC2 instance & then enable the service. and finally run the playbook
We have finally configured the webserver in out ec2 we can check the instance and the webserver by typing the ip/index.html of the e2 instance.
Thank you for reading !!!!!!!!
Cloud Architect ? 6X Azure ? 1X Databricks ? ACE Certified ? MCT
4 年Keep-Going Srishti Jain ??
DevOps Engineer @Amdocs
4 年Nice work ?