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.
Provisioning Ec2 Instance:-
First of all we will Provision the Ec2-instance by using Ansible.We can first create a key-pair using Ansible and then we can attach this key to our instance.For this we also have to set the environment variable like this :
$ export AWS_ACCESS_KEY="xxxxxxxxxxxxxxxx" $ export AWS_SECRET_KEY="xxxxxxxxxxxxxxxxxxxxx" $ export AWS_REGION_NAME="ap-south-1"
Now we will run this playbook on localhost
Now if we go to our was cloud we can see that key-pair has been successfully created
Now, We have to create a playbook and then we can create the instance and attach this key-pair for login into the Instance.For this we will again write a playbook and run it by using localhost and configure the Aws ec2 instance.
Now again we need Credentials of the Aws account so we can Secure them by using Ansible-Vault.We will encrypt our credentials with a key and while running the playbook we will provide the key so it will decrypt the credentials and run the task.
Now if we go to our AWS cloud we can see our instance has been successfully launched.
SO Our Instance has been successfully provisioned.Now we have to Create the Inventory file.
What is Inventory in ansible?
ansible inventory is a collection of IP addresses and groups upon which all the commands and module run.We can say its something like IP database.
In this Dynamic World, we can't go manually and fetch the IP Address we use automation to save our time and to develop quickly. Here we have to use a dynamic Inventory Concept to fetch the IP Address.
https://github.com/ansible/ansible/blob/stable-2.9/contrib/inventory/ec2.
Now we will download this file and make this executable and set the environmental variable as mentioned above then if we will run this file so we can see that we can Dynamically get the IP address of the Ec2 instance we also need one more file that is ec2.ini file
https://github.com/ansible/ansible/blob/stable-2.9/contrib/inventory/ec2.ini
Set the Region in which we want to work(i.e. ap-south-1)
Now we Have to Update our configuration file so that our inventory can be dynamically updated and we can do further things in our instance.
AS we know in Linux Super User is Root and Root can do anything in the system so if want to install something in our system we need to login via root but in general ssh via root is by-default disabled by all the cloud Providers to make the OS very Secure so we will change the privileges of our normal user so that normal user can also perform root tasks .For this we will have to change the ansible Configuration file and update the location of our private key so when the Ip will be fetched we can also ping and perform our tasks.
Here we have to also tell the inventory file location because it is the one through which our IP will be fetched and here our both the two files resides.
So We can now perform the task on our instance by creating the Ansible Playbook.
Now we can configure the web server and put up our code inside the root directory.
Now we can see our Website
Thank You so much for visiting my article!
DevOps Engineer | Graduate Student at Pace University
4 年well explained....
DevOps @Forescout ?? | Google Developer Expert | AWS | DevOps | 3X GCP | 1X Azure | 1X Terraform | Ansible | Kubernetes | SRE | Platform | Jenkins | Tech Blogger ??
4 年Great work bro ?