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

[webservers]
1.1.1.1
example.com

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/__init__.py", line 38, in <module>
.
.
.
    from cryptography.x509.name import Name
  File "/usr/lib/python2.7/dist-packages/cryptography/x509/name.py", 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
1.1.1.1 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
example.com | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

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

mkdir -p /tmp/ansibleexample
cd /tmp/ansibleexample
wget https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.py
python ec2.py --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
    vars:
      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
    tasks:

      - name: Launch the new EC2 Instance
        local_action: ec2
                      group=
                      instance_type=
                      image=
                      wait=true
                      region=
                      keypair=
                      count=8
        register: ec2

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


      - name: Wait for SSH to come up
        local_action: wait_for
                      host=
                      port=22
                      state=started
        with_items: ""

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

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

Twitter, Facebook