Hi, I am writing a simple blog on how to use Ansible to interact with AWS EC2 instanaces using the dynamic libraries. Here I am demonstarting a simple ping command to ping your EC2 instances in AWS using the dynamic inventories.
Step by step procedure:
I am assuming that, you have already setup Ansible in your EC2 infrastructure. If not yet done, I am strongly recommend you to get it done with the help of my blog here: AWS EC2 step-by-step Automation by Ansible
1. Download the following files 3 files into your ansible working directory. /etc/ansible is my ansible working directory
2. Make the following changes. We are actually going to use the dynamic inventory as a default ansible inventory. So, you need to edit the ansible.cfg file present in /etc/ansible directory.
- Search for inventory parameter in the ansible.cfg. Change the inventory parameter value as shown below:
inventory= /etc/ansible/ec2.py
3. If you run the ping command straightaway, you will see lot of permission errors due to the private key used in your EC2 instance is not authorized to execute a Ping command in your AWS infrastructure. So, you have to give the SSH permission to your key file as below.
I assume that, your key file ansible.pem is already saved to the directory ~/.ssh in your ec2 instance from which you are running ansible
ssh-agent bash
ssh-add ~/.ssh/ansible.pem
Now you are ready to start connecting your ec2 instances with a ping command as below:
ansible all -m ping
Also, verify the host names updated in the file vi ~/.ssh/known_hosts
I wanted to share you guys the step-by step procedure to deploy, configure the tool in an AWS EC2 Virtual machine. Also, a simple procedure is given at the end of the article to deploy new virtual machines in AWS using Ansib;le playbooks
Okay, let's start...I have deployed a new RedHat EC2 Virtual machine in AWS with the following details:
You may select appropriate disk, memory, cpu etc.. as per your convenience and future use of this machine. It's a staraight forward way to create an AWS virtual machine
Few things, you have to make a note is
Virtual machine's public IP address, to connect the machine through SSH using the tool like Putty.
Make sure that your AWS security groups have the rules allowed to connect the machine through SSH port 22
Make a note of the keypair name. This name we need to be used in Ansible Playbook. Keep the key in your computer so that you can connect the machine through SSH
Also, note the AMI ID of the Virtual machine in AWS. This can be noted from the machine properties and looks like the follwoing. We need the following hilighted name only for using this in Ansible playbook
Connect the machine using the SSH tool. If you are using putty, browse the key file as below (SSH->Auth)
ec2-user is the default login username for this AWS AMI. We don't need to type the password as there is a keyfile attached.
Here are the further steps by step procedure I have listed as below
Excecute sudo su so that you have root permission to install the required packages further
Execute sudo yum update to update the AMI with latest available packages. Note that the system will prompt to ask your permission to install the packages and press 'y' when prompted. Once the pacges are successfully installed, you can observe the following at the shell
Next, we need to install pip (pip is a package management system used to install and manage software packages written in Python) using the command sudo easy_install pip
Successfull installation will show the prompt as follows:
Using pip, we can install the ANSIBLE pakage with the following command:
sudo pip install ansible
Finally, we have successfully installed Ansible package as shown below:
Ansible version can be re-cheked with the below command
ansible --version
This will provide you the following details also along with the version of Ansible we just have installed
We also need an important package named 'boto' to be installed so that we can create AWS EC2 Virtual machines using Ansible. Boto is the Amazon Web Services (AWS) SDK for Python, which allows Python based programs like Ansible to create EC2 virtual machines in AWS. Boto3 can be installed using the following command:
sudo pip install boto3
Here is how it shows a successful Boto3 installation
So, we have successfully completed the infrastructure requirements for Ansible. Now we can create the AWS EC2 Virtual machines with the help of Ansible as I am describing in following section:
Ansible requires important files : ansible.cfg, hosts, yml playbook file
If you just want to try out ansible, it generally works without an ansible.cfg file too. (You do need to specify some inventory but you can do that on the command line rather than a file if you want).
Although we have installed ansible the necessary files are to be created separately by the following procedure
First of all create a folder under /etc as ansible
Host file (Inventory file) details are explained here: http://docs.ansible.com/ansible/intro_inventory.html. I am going to use a simple hosts file with the following contents only for the test EC2 creation purpose. Create a file named hosts under /etc/ansible
[localhost]<br />local
Simply run the ping command to test our newly installed Ansible!
Now we need to export the AWS Access keys so that the AWS account can be authenticated sucessfully to the AWS platform. There are many ways available to achive this. For this test purpose, I am going to add the keys to my bashrc file as below
vi ~/.bashrc
Add the following lines at the end of the file as below. Following keys are obtained from the IAM dashboard. If you don't have these keys saved yet, you may create these keys again from your AWS user account.
Save the bashrc file and source the file as below:
source ~/.bashrc
Next, I am going to create an ansible paybook file. The file is in yml format. You may use the following contents to create your ansible playbook file. Copy the contents to the yml file for example ec2-create.yml
--- # sample playbook for clouditspace.com by Manu - name: Provision a Redhat AMI EC2 node in AWS hosts: local connection: local gather_facts: False tags: provisioning vars: instance_type: t1.micro security_group: default # This is the default security group I have pre-created in my AWS image: ami-10bb2373 # Note that this AMI name should be a valid name in the region we have selected region: ap-southeast-1 # The region name and following keypair must match keypair: ansible # Make sure that the region name selected above has got the keypair for the machine we use tasks: - name: Launch new Instance local_action: ec2 instance_tags="Name=Ansible-Test" group={{ security_group }} instance_type={{ instance_type}} image={{ image }} wait=true region={{ region }} keypair={{ keypair }} register: ec2
Finally change the folder permission on /etc/ansible directory so that there is execute permission to run ansible commands as below:
chmod 777 *
Now, we are ready to create our first AWS EC2 Virtual machine through Ansible :). Following is the syntax. Run it at /etc/ansible directory
ansible-playbook ec2-create.yml
Successful creation of the ec2 instance can be indicated as below:
wow ! it's the time to check the AWS EC2 dash board and to see how it's been created oever there ! Check it and let me know :)