# Ansible and ec2 installation

Date: 2019-01-18

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

<!--more-->

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

[Ansible installation](https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html) from the Ansible documentation  


```shell
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

```shell
[webservers]
1.1.1.1
example.com
```

And I ran :

```shell
ansible webservers -m ping
```

The results :

```shell
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 &lt;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 &lt;module>
.
.
.
    from cryptography.x509.name import Name
  File "/usr/lib/python2.7/dist-packages/cryptography/x509/name.py", line 28, in &lt;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](https://stackoverflow.com/questions/49509790/typeerror-when-importing-requests-from-python)

```shell
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](https://docs.ansible.com/ansible/latest/user_guide/intro_dynamic_inventory.html#inventory-script-example-aws-ec2)

```shell
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](https://www.agix.com.au/build-an-ec2-using-ansible-step-by-step/) :

```shell
---
  - 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={{ security_group }}
                      instance_type={{ instance_type}}
                      image={{ image }}
                      wait=true
                      region={{ region }}
                      keypair={{ keypair }}
                      count={{count}}
        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={{ item.public_ip }}
                      insertafter="[webserver]" line={{ item.public_ip }}
        with_items: "{{ ec2.instances }}"


      - name: Wait for SSH to come up
        local_action: wait_for
                      host={{ item.public_ip }}
                      port=22
                      state=started
        with_items: "{{ ec2.instances }}"

      - name: Add tag to Instance(s)
        local_action: ec2_tag resource={{ item.id }} region={{ region }} state=present
        with_items: "{{ ec2.instances }}"
        args:
          tags:
            Name: webserver
```

And that's all .. I have a running machine in AWS &#8230; apps and installation in another post
