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

查看:29
本文介绍了使用 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

我认为在inventory/中运行一个最有意义,所以我使用:

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 Classic 的使用,您应该可以在不定义任何 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 注册为 ansible 主机变量 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 格式的主机列表打印到标准输出.

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

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

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