ClickCease

How to Connect AWS to Ansible

Step 1: Creating an AWS Account

In this article, we will see how to launch an Amazon Web Services (AWS) Elastic Compute Cloud (EC-2) instance and manage it using Ansible. The first here is to create an AWS account. Do not worry, it has 12 months of free usage for some instances. Luckily, the process that we are going to follow today will ensure that nothing gets charged to your Debit Card.

Use this link to create an account and then verify it using your e-mail: AWS Account Create

Step 2: Launching an AWS Instance

The next step is to choose and launch an instance. Click on EC2 under compute on your AWS Dashboard. Now, click on ‘Launch Instance’ under the ‘Create Instance’ option. Now you will see a screen with the choice of Choosing an AWS Machine Image. Since Ansible uses python to run modules, you will need to choose a Machine Image which has python pre-installed with it.
The best option here is to go with ‘Amazon Linux AMI 2018.03.0 (HVM), SSD Volume Type’ since it is Linux based and comes with Python installed.

How to connect AWS to Ansible

Now, ensure that you have ‘General Purpose t2.micro Free Tier Eligible’ chosen and click on the ‘Review and Launch’ option. You will now be prompted regarding the AWS Key Pair. Choose create a new key pair, name your file and download it to a secure location.

How to connect AWS to Ansible

If you check your instances now, you will see that the instance we created is running.

How to connect AWS to Ansible

Step 3: Connect Instance with SSH

The next step is to ensure that we are able to connect with this instance before linking it to Ansible. Let us try to see if we can SSH to the AWS Instance. In order to use SSH, We will require the Public IP address or the IPv4 Public IP of the instance which you can check under the instance description.

How to connect AWS to Ansible


Once you have the instance public IPV4 address, you can SSH it. You should know that the ssh will only work if you also input your identity file. Without the identity file, you will get a permission denied error and will not be able to connect your instance.
Note: The username for all AWS EC2 modules by default is: ec2-user

ssh -i ansible-ec2.pem ec2-user@35.154.210.201

Step 4: Input Instance in Hosts File

Congratulations on having to successfully being able to SSH to an AWS instance. As you can imagine, the final step would be to input the instance details in the host or inventory file for Ansible and try to reach the instance through the Ansible ping module. When connecting with local machines, inputting the IP or the hostname of the remote machine in the Ansible hosts file is enough but in the case of Ansible, you will need to input some more information in order to successfully be able to connect to your AWS instance via Ansible.

The information you will need is:

  • A name for your instance
  • IP Address of your AWS instance
  • The user present on your AWS instance
  • Location to your private key (.pem) file

You will need to input the information somewhat like this in the hosts file:

ec2-instance ansible_host=15.206.116.102 ansible_user=ec2-user
ansible_ssh_private_key_file=/home/aryan/Desktop/ansible-ec2.pem

Step 5: Run Ansible Ping Module

If you try running ansible all -m ping now, you should receive an output like this:

How to connect AWS to Ansible


Voila! You have successfully connected your machine to an AWS instance and can now manage it using Ansible. Try running basic Ansible modules like creating users and files.

Step 6: Terminate AWS Instance

Be sure to ‘Terminate’ the AWS instance once you are done using it otherwise you could face some transactions on your bank account linked to the AWS account.

How to connect AWS to Ansible

Step 7: More Knowledge

If you’d like to go on further with Ansible, I would recommend that you check out my article on how to connect to a Windows machine using Ansible here: Ansible with Windows

If you are completely new to Linux, I would suggest that you go through the basic Linux program also known as RHCSA: Linux Training
If you are an expert with Linux, grow your career with RHCE, AWS, Devops, Openstack or Openshift.