使用Ansible设置EC2实例时遇到问题 [英] Having trouble provisioning EC2 instances using Ansible

查看:126
本文介绍了使用Ansible设置EC2实例时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何使用Ansible启动EC2实例感到非常困惑.

I'm very confused on how you are supposed to launch EC2 instances using Ansible.

我正在尝试使用ec2.py广告资源脚本.我不确定应该使用哪一个,因为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

这给了我

msg: Either region or ec2_url must be specified

所以我添加了一个区域(即使我指定了vpc_subnet_id),但我得到了:

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

我认为Amazon必须最近更改了ec2,因此您需要使用VPC?即使当我尝试从Amazon控制台启动实例时,"EC2 Classic"选项也被禁用.

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.

当我尝试在cloud/amazon/中使用ec2.py脚本时,我得到了:

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.

经过一些搜索,我发现/module_utils中的ec2.py模块已更改,因此无需指定区域.我尝试运行此文件,但得到:

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:

错误:文件/software/ansible/lib/ansible/module_utils/ec2.py被标记为可执行文件,但未能正确执行.如果不应将其视为可执行脚本,请使用chmod -x /software/ansible/lib/ansible/module_utils/ec2.py进行更正.

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.

正如错误所提示的,我删除了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:

  1. 是否仍然可以在EC2 Classic(无VPC)中启动实例?
  2. 如何使用Ansible创建新的EC2实例?
  3. 如何启动动态清单文件ec2.py?

1. EC2经典版

根据您何时创建您的AWS账户,实例类型和所使用的AMI虚拟化类型,您的选项会有所不同.参考: aws帐户实例类型.

如果以上参数均未限制EC2经典版的使用,则您应该能够在不定义任何VPC的情况下创建新实例.

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.

由于您的实例不存在,所以动态清单文件(ec2.py)无效.尝试指示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.

创建一个新的清单文件,例如new_hosts具有以下内容:

Create a new inventory file, e.g. new_hosts with the following contents:

[localhost]
127.0.0.1

然后是您的剧本,例如create_instance.yml应该使用本地连接,而hosts: localhost.请参见下面的示例:

Then your playbook, e.g. create_instance.yml should use a local connection and hosts: localhost. See an example below:

--- # Create ec2 instance playbook

- hosts: localhost
  connection: local
  gather_facts: false
  vars_prompt:
    inst_name: "What's 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注册为一个可用的主机变量ec2hosts,即.就像您在清单文件中定义它一样.如果您要提供刚创建的实例,只需添加hosts: ec2hosts新的播放,这将很有用.

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 with hosts: ec2hosts.

最终,按如下所示启动ansible:

Ultimately, launch ansible as follows:

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主机密钥.

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.

注意:需要在运行上述ansible命令的计算机上安装boto.

Note: boto needs to be installed on the machine that runs the above ansible command.

EC2动态清单由2个文件ec2.pyec2.ini组成.在您的特定情况下,我认为您的问题是由于ec2.py无法找到ec2.ini文件而引起的.

EC2 dynamic inventory is comprised of 2 files, ec2.py and ec2.ini. In your particular case, I believe that your issue is due to the fact that ec2.py is unable to locate ec2.ini file.

要解决您的问题,请将ec2.pyec2.ini复制到要运行ansible的计算机上的同一文件夹中,例如到/etc/ansible/.

To solve your issue, copy ec2.py and ec2.ini to the same folder in the machine where you intend to run ansible, e.g. to /etc/ansible/.

Pre Ansible 2.0版本(相应更改分支).

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,这应该将ini格式的主机列表打印到stdout.

Configure ec2.ini and run ec2.py, which should print an ini formatted list of hosts to stdout.

这篇关于使用Ansible设置EC2实例时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆