Troubleshoot EC2 Instances Using Ansible

I am very confused about how you should run EC2 instances using Ansible.

I am trying to use ec2.py inventory scripts. I'm not sure which one is supposed to be used, because there are three installed with Ansible:

  • ansible / Library / ansible / module_utils / ec2.py
  • anzibl / Library / ansibl / modules / core / cloud / Amazon /ec2.py
  • ansible / plugins / inventory / ec2.py

I thought running it in inventory / would make more sense, so I run it using:

ansible-playbook launch-ec2.yaml -i ec2.py

which gives me:

msg: Either region or ec2_url must be specified

So, I add the scope (even if I have vpc_subnet_id specified), and I get:

msg: Region us-east-1e does not seem to be available for aws module boto.ec2. If the region definitely exists, you may need to upgrade boto or extend with endpoints_path

, Amazon, , ec2, VPC? Amazon, "EC2 Classic" .

ec2.py script cloud/amazon/, :

ERROR: Inventory script (/software/ansible/lib/ansible/modules/core/cloud/amazon/ec2.py) had an execution error:

.

, ec2.py /module _utils , . , :

. /software/ansible/lib/ansible/module _utils/ec2.py , . script, chmod -x /software/ansible/lib/ansible/module_utils/ec2.py.

, , ec2.py, :

ERROR: /software/ansible/lib/ansible/module_utils/ec2.py:30: Invalid ini entry: distutils.version - need more than 1 value to unpack

- , ? ? , .

+4
1

. :

  • EC2 Classic ( VPC)?
  • EC2 Ansible?
  • ec2.py?

1. EC2 Classic

, AWS, AMI. Refs: aws account, .

EC2 classic, VPC.

2. EC2 Ansible

, (ec2.py) . , .

, . new_hosts :

[localhost]
127.0.0.1

-, . create_instance.yml hosts: localhost. . :

--- # Create ec2 instance playbook

- hosts: localhost
  connection: local
  gather_facts: false
  vars_prompt:
    inst_name: "What the name of the instance?"
  vars:
      keypair: "your_keypair"
      instance_type: "m1.small"
      image: "ami-xxxyyyy"
      group: "your_group"
      region: "us-west-2"
  tasks:
    - name: make one instance
      ec2: image={{ image }}
           instance_type={{ instance_type }}
           keypair={{ keypair }}
           instance_tags='{"Name":"{{ inst_name }}"}'
           region={{ region }}
           group={{ group }}
           wait=true
      register: ec2_info

    - name: Add instances to host group
      add_host: hostname={{ item.public_ip }} groupname=ec2hosts
      with_items: ec2_info.instances

    - name: Wait for SSH to come up
      wait_for: host={{ item.public_dns_name }} port=22 delay=60 timeout=320 state=started
      with_items: ec2_info.instances

EC2, IP- host host ec2hosts .. . , , hosts: ec2hosts.

, :

export ANSIBLE_HOST_KEY_CHECKING=false
export AWS_ACCESS_KEY=<your aws access key here>
export AWS_SECRET_KEY=<your aws secret key here>

ansible-playbook -i new_hosts create_instance.yml

ANSIBLE_HOST_KEY_CHECKING=false , ssh .

. boto , .

3. ec2

EC2 2 , ec2.py ec2.ini. , , ec2.py ec2.ini.

, ec2.py ec2.ini , , , /etc/ansible/.

Pre Ansible 2.0 release ( ).

cd /etc/ansible
wget https://raw.githubusercontent.com/ansible/ansible/stable-1.9/plugins/inventory/ec2.py
wget https://raw.githubusercontent.com/ansible/ansible/stabe-1.9/plugins/inventory/ec2.ini
chmod u+x ec2.py

Ansible 2:

cd /etc/ansible
wget https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.py
wget https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.ini
chmod u+x ec2.py

ec2.ini ec2.py, stdout.

+8

Source: https://habr.com/ru/post/1584572/


All Articles