Launching An EC2 Instance On AWS Using Ansible

Launching An EC2 Instance On AWS Using Ansible

Hello guys, I'm back with another article. In this article,we'll do the following task:

  • Launch an AWS instance with the help of ansible.
  • Retrieve the public IP which is allocated to the launched instance.
  • With the help of the retrieved Public IP configure the web server in the launched instance.

What is Ansible?

Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, intraservice orchestration, and provisioning. Automation is crucial these days, with IT environments that are too complex and often need to scale too quickly for system administrators and developers to keep up if they had to do everything manually.

What is playbook file?

Playbooks are the files where Ansible code is written. Playbooks are written in YAML format. YAML stands for Yet Another Markup Language. Playbooks are one of the core features of Ansible and tell Ansible what to execute. They are like a to-do list for Ansible that contains a list of tasks.

Playbooks contain the steps which the user wants to execute on a particular machine. Playbooks are run sequentially. Playbooks are the building blocks for all the use cases of Ansible.

Task Ahead?

Ansible is great tool for configuration on any OS. What we we have to do is to launch an OS using Ansible. This is possible using Ansible, but it is meant usually for configuration and provisioning an OS can be done using Terraform. Though Ansible can manage Configuration as well as provisioning, here we will be utilising Ansible to provision an ec2 instance and also for configuring a web server inside the instance. The task description can be cleared using below figure.

No alt text provided for this image

As you can see in the above figure, we can use our localhost ip address to behave as a managed node and then will use the SDK to lauch ec2 instance on AWS. As Ansible is built on python language, we will be using boto as it is an API so it has the capability to contact with AWS.

Steps Taken

1. First, we have to insert a local host IP address (look back IP) in hosts of Ansible.

No alt text provided for this image

2. After that, try to ping using ping command, and then try via ansible localhost -m ping.

3. Now we are ready to create ansible-plabook, which we create as follows:

No alt text provided for this image

The above code will only launch ec2 instance on AWS. Below code will extract IP of the newly launched ec2 instance.

No alt text provided for this image

After running the code successfully, we can see below that the new ec2 instance named "aws_ansible_ec2" has been launched.

No alt text provided for this image

4. Now we have to set the user name and password of the newly launched ec2 instance and also set the hostname.

No alt text provided for this image

5. After that we need to update the user in /etc/sudoers.

No alt text provided for this image

6. Next we need to give password authentication in /etc/ssh/sshd_config. and restart the sshd services.

No alt text provided for this image

7. Now, we restart the services using service sshd restart.

8. Thus we generate the ssh key, using the command ssh-keygen.

No alt text provided for this image

9. After generating the ssh key, we have to copy the ssh key in the ec2 instance using the command ssh-copy-id.

No alt text provided for this image

10. After copying ssh key to newly launched ec2 instance then we have to update the private IP address in the ansible hosts.

11. Also, write the access permission code in the ansible.cfg file.

No alt text provided for this image
No alt text provided for this image

Here, there is no need to give password as we have already generated ssh key and copied it to the desired ec2 instance. Now we can ping the ec2 Node.

No alt text provided for this image

12. After this we can configure the web serve in that ec2 node with the help of Ansible.

No alt text provided for this image

The above code will configure the webserver in that ec2 node.

No alt text provided for this image

After your code run successfully now you can check the web server using public IP of that ec2 instance.

Alas! Now the web server is successfully configured !!

Thank you for reading my article. I hope it will help you understand the concepts worked on. If any improvement required, please let me know.

要查看或添加评论,请登录

Shubhankar Thapliyal的更多文章

社区洞察

其他会员也浏览了