Saturday, December 15, 2018

Ansible Playbook for provisioning a new EC2 instance in AWS - Create a new EC2 Using Ansible Playbook

Please find the Ansible Playbook for provisioning a new EC2 instance. Please follow the below steps in the machine where you installed Ansible.

Steps to create EC2 instance using Ansible:

1. Login to AWS console, click on username and go to My security credentials.
2. Continue on security credentials, click on access keys
3. Create a new access key if you dont have one. Make sure you download the keys.
4. Login to EC2 instance using Git bash or ITerm where you installed Ansible.

execute the below command

sudo vi ~/.boto

add below three lines in the above file, replace the ?? with access key and secret key values.
aws_access_key_id = ??
aws_secret_access_key = ??

5. Edit Ansible hosts or inventory file
sudo vi /etc/ansible/hosts 
Add the below two lines in the end of the file:

6. cd ~
7. mkdir playbooks  
8. cd playbooks

Create Ansible playbook
9. sudo vi create_jenkins_ec2.yml 
(copy the below content in green color)
edit the create_jenkins_ec2.yml to make sure you update the key which is red marked below:
 - name:  provisioning EC2 Lab Exercises using Ansible
   hosts: localhost
   connection: local
   gather_facts: False
   tags: provisioning

     keypair: MyinfraCodeKey
     instance_type: t2.small
     image: ami-07c1207a9d40bc3bd
     wait: yes
     group: webserver
     count: 1
     region: us-east-2
     security_group: my-jenkins-security-grp

     - name: Create my security group
         module: ec2_group
         name: "{{ security_group }}"
         description: Security Group for webserver Servers
         region: "{{ region }}"
            - proto: tcp
              from_port: 22
              to_port: 22
            - proto: tcp
              from_port: 8080
              to_port: 8080
            - proto: tcp
              from_port: 80
              to_port: 80
            - proto: tcp
              from_port: 443
              to_port: 443
            - proto: all
       register: basic_firewall
     - name: Launch the new EC2 Instance
       local_action:  ec2 
                      group={{ security_group }} 
                      instance_type={{ instance_type}} 
                      image={{ image }} 
                      region={{ region }} 
                      keypair={{ keypair }}
       register: ec2
     - name: Add Tagging to EC2 instance
       local_action: ec2_tag resource={{ }} region={{ region }} state=present
       with_items: "{{ ec2.instances }}"
           Name: MyTargetEc2Instance

10. now execute the ansible playbook by
sudo ansible-playbook create_jenkins_ec2.yml

Fix the warnings by executing below command
pip install --upgrade requests==2.20.1

If everything is good, you should see the new instance created on AWS console. make sure you are able to connect to that instance.

That's it!! That is how you create a new EC2 instance using Ansible.


  1. Ekhorutomwen OsasogieAugust 15, 2019 at 4:00 PM

    The accuracy of these labs are genius. Thank you Coach.