Ansible and ec2 installation

Eran Goldman-Malka · January 18, 2019

I need to deploy and manage ec2 machines easily, and I want to try ansible for a while so I’ll log the process of how to deploy an ec2 machine with one ansible script

In this post I’ll use ubuntu 18.04 and ansible claim that they don’t need anything else.

Ansible installation from the Ansible documentation

sudo apt-get update
sudo apt-get install software-properties-common
sudo apt-add-repository --yes --update ppa:ansible/ansible
sudo apt-get install ansible

First test:

I edited the /etc/ansible/hosts file and added to the [webservers] section my server ip


And I ran :

ansible webservers -m ping

The results :

ERROR! Unexpected Exception, this is probably a bug: 'type' object is not iterable
the full traceback was:

Traceback (most recent call last):
  File "/usr/bin/ansible", line 97, in <module>
    mycli = getattr(__import__("ansible.cli.%s" % sub, fromlist=[myclass]), myclass)
  File "/usr/lib/python2.7/dist-packages/ansible/cli/", line 38, in <module>
    from import Name
  File "/usr/lib/python2.7/dist-packages/cryptography/x509/", line 28, in <module>
    _ASN1_TYPE_TO_ENUM = dict((i.value, i) for i in _ASN1Type)
TypeError: 'type' object is not iterable
ubuntu@ip-172-31-22-170:~$ sudo ansible app -m ping

After some googling I Found the solution

pip uninstall enum
pip install enum34
ansible webservers -m ping | SUCCESS => {
    "changed": false,
    "ping": "pong"
} | SUCCESS => {
    "changed": false,
    "ping": "pong"

And now to communicate with ec2, I found the ansible inventory script

mkdir -p /tmp/ansibleexample
cd /tmp/ansibleexample
python --list

And you should have a json with all your aws inventory

  • dont forget to put your aws key and secret in ~/.aws/credentials

at last to create new machines I use the code from agix :

  - name: Provision an EC2 Instance
    hosts: local
    connection: local
    gather_facts: False
    tags: provisioning
    # Necessary Variables for creating/provisioning the EC2 Instance
      instance_type: t2.micro
      security_group: security-group # Change the security group name here
      image: ami-0a00713584ff737a1 # This is an AMI i created myself
      keypair: Keypair #This is one of my keys that i already have in AWS
      region: us-east-1 # Change the Region
      count: 1 

    # Task that will be used to Launch/Create an EC2 Instance

      - name: Launch the new EC2 Instance
        local_action: ec2
        register: ec2

      - name: Add the newly created EC2 instance(s) to the local host group (located inside the directory)
        local_action: lineinfile
                      insertafter="[webserver]" line=
        with_items: ""

      - name: Wait for SSH to come up
        local_action: wait_for
        with_items: ""

      - name: Add tag to Instance(s)
        local_action: ec2_tag resource= region= state=present
        with_items: ""
            Name: webserver

And that’s all .. I have a running machine in AWS … apps and installation in another post

Twitter, Facebook