I'm very confused on how you are supposed to launch EC2 instances using Ansible.
I'm trying to use the ec2.py inventory scripts. I'm not sure which one is supposed to be used, because there is three installed with Ansible:
- ansible/lib/ansible/module_utils/ec2.py
- ansible/lib/ansible/modules/core/cloud/amazon/ec2.py
- ansible/plugins/inventory/ec2.py
I thought running the one in inventory/ would make most 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 a region (even though I have a 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
I'm thinking Amazon must have recently changed ec2 so you need to use a VPC? Even when I try and launch an instance from Amazon's console, the option for "EC2 Classic" is disabled.
When I try and use the ec2.py script in cloud/amazon/ I get:
ERROR: Inventory script (/software/ansible/lib/ansible/modules/core/cloud/amazon/ec2.py) had an execution error:
There are no more details than this.
After some searching, I see that ec2.py module in /module_utils has been changed so a region doesn't need to be specified. I try to run this file but get:
ERROR: The file /software/ansible/lib/ansible/module_utils/ec2.py is marked as executable, but failed to execute correctly. If this is not supposed to be an executable script, correct this with chmod -x /software/ansible/lib/ansible/module_utils/ec2.py
.
So as the error suggests, I remove the executable permissions for the ec2.py file, but then get the following error:
ERROR: /software/ansible/lib/ansible/module_utils/ec2.py:30: Invalid ini entry: distutils.version - need more than 1 value to unpack
Does anyone have any ideas on how to get this working? What is the correct file to be using? I'm completely lost at this point on how to get this working.
There are several questions in your post. I'll try to summarise them in three items:
ec2.py
?1. EC2 Classic
Your options will differ depending on when did you create your AWS account, the type of instance and the AMI virtualisation type used. Refs: aws account,instance type.
If none of the above parameters restricts the usage of EC2 classic you should be able to create a new instance without defining any VPC.
2. Create a new EC2 instance with Ansible
Since your instance doesn't exist yet a dynamic inventory file (
ec2.py
) is useless. Try to instruct ansible to run on your local machine instead.Create a new inventory file, e.g.
new_hosts
with the following contents:Then your playbook, e.g.
create_instance.yml
should use a local connection andhosts: localhost
. See an example below:This play will create an EC2 instance and it will register its public IP as an ansible host variable
ec2hosts
ie. as if you had defined it in the inventory file. This is useful if you want to provision the instance just created, just add a new play withhosts: ec2hosts
.Ultimately, launch ansible as follows:
The purpose of the environment variable
ANSIBLE_HOST_KEY_CHECKING=false
is to avoid being prompted to add the ssh host key when connecting to the instance.Note: boto needs to be installed on the machine that runs the above ansible command.
3. Use ansible's ec2 dynamic inventory
EC2 dynamic inventory is comprised of 2 files,
ec2.py
andec2.ini
. In your particular case, I believe that your issue is due to the fact thatec2.py
is unable to locateec2.ini
file.To solve your issue, copy
ec2.py
andec2.ini
to the same folder in the machine where you intend to run ansible, e.g. to/etc/ansible/
.Pre Ansible 2.0 release (change the branch accordingly).
For Ansible 2:
Configure
ec2.ini
and runec2.py
, which should print an ini formatted list of hosts to stdout.