使用 Ansible 配置 EC2 实例时遇到问题
Having trouble provisioning EC2 instances using Ansible
我很困惑你应该如何使用 Ansible 启动 EC2 实例。
我正在尝试使用 ec2.py 清单脚本。我不确定应该使用哪个,因为 Ansible 安装了三个:
- ansible/lib/ansible/module_utils/ec2.py
- ansible/lib/ansible/modules/core/cloud/amazon/ec2.py
- ansible/plugins/inventory/ec2.py
我认为 运行在库存/ 中最有意义,所以我 运行 它使用:
ansible-playbook launch-ec2.yaml -i ec2.py
这给了我:
msg: Either region or ec2_url must be specified
所以我添加了一个区域(即使我指定了 vpc_subnet_id)并且我得到:
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
我想亚马逊一定是最近更改了 ec2,所以您需要使用 VPC?即使我尝试从 Amazon 的控制台启动实例,"EC2 Classic" 的选项也被禁用。
当我尝试在 cloud/amazon/ 中使用 ec2.py 脚本时,我得到:
ERROR: Inventory script (/software/ansible/lib/ansible/modules/core/cloud/amazon/ec2.py) had an execution error:
没有比这更详细的了。
经过一些搜索,我在 /module_utils has been changed 中看到 ec2.py 模块,因此不需要指定区域。我尝试 运行 这个文件但是得到:
错误:文件 /software/ansible/lib/ansible/module_utils/ec2.py 被标记为可执行文件,但未能正确执行。如果这不是可执行脚本,请使用 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
有没有人知道如何让它工作?要使用的正确文件是什么?在这一点上,我完全不知道如何让它工作。
您的 post 中有几个问题。我将尝试将它们概括为三个项目:
- 是否仍然可以在 EC2 Classic(无 VPC)中启动实例?
- 如何使用 Ansible 创建新的 EC2 实例?
- 如何启动动态清单文件
ec2.py
?
1。 EC2 经典
您的选项会有所不同,具体取决于您创建 AWS 帐户的时间、实例类型和使用的 AMI 虚拟化类型。参考:aws account,instance type.
如果上述参数中的 none 限制了 EC2 classic 的使用,您应该能够在不定义任何 VPC 的情况下创建新实例。
2。使用 Ansible
创建一个新的 EC2 实例
由于您的实例不存在,因此动态清单文件 (ec2.py
) 没有用。尝试在本地计算机上指示 ansible 运行。
创建一个新的清单文件,例如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'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 实例,并将其 public IP 注册为一个 ansible 主机变量 ec2hosts
即。就像您在清单文件中定义它一样。如果你想提供刚刚创建的实例,这很有用,只需添加一个新的 hosts: ec2hosts
.
最终,按如下方式启动ansible:
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需要安装在运行执行上述ansible命令的机器上。
3。使用ansible的ec2动态库存
EC2 动态清单由 2 个文件组成,ec2.py
和 ec2.ini
。在您的具体情况下,我认为您的问题是由于 ec2.py
无法找到 ec2.ini
文件。
要解决您的问题,请将 ec2.py
和 ec2.ini
复制到您打算 运行 ansible 的计算机中的同一文件夹,例如至 /etc/ansible/
.
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 格式的主机列表打印到标准输出。
我很困惑你应该如何使用 Ansible 启动 EC2 实例。
我正在尝试使用 ec2.py 清单脚本。我不确定应该使用哪个,因为 Ansible 安装了三个:
- ansible/lib/ansible/module_utils/ec2.py
- ansible/lib/ansible/modules/core/cloud/amazon/ec2.py
- ansible/plugins/inventory/ec2.py
我认为 运行在库存/ 中最有意义,所以我 运行 它使用:
ansible-playbook launch-ec2.yaml -i ec2.py
这给了我:
msg: Either region or ec2_url must be specified
所以我添加了一个区域(即使我指定了 vpc_subnet_id)并且我得到:
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
我想亚马逊一定是最近更改了 ec2,所以您需要使用 VPC?即使我尝试从 Amazon 的控制台启动实例,"EC2 Classic" 的选项也被禁用。
当我尝试在 cloud/amazon/ 中使用 ec2.py 脚本时,我得到:
ERROR: Inventory script (/software/ansible/lib/ansible/modules/core/cloud/amazon/ec2.py) had an execution error:
没有比这更详细的了。
经过一些搜索,我在 /module_utils has been changed 中看到 ec2.py 模块,因此不需要指定区域。我尝试 运行 这个文件但是得到:
错误:文件 /software/ansible/lib/ansible/module_utils/ec2.py 被标记为可执行文件,但未能正确执行。如果这不是可执行脚本,请使用 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
有没有人知道如何让它工作?要使用的正确文件是什么?在这一点上,我完全不知道如何让它工作。
您的 post 中有几个问题。我将尝试将它们概括为三个项目:
- 是否仍然可以在 EC2 Classic(无 VPC)中启动实例?
- 如何使用 Ansible 创建新的 EC2 实例?
- 如何启动动态清单文件
ec2.py
?
1。 EC2 经典
您的选项会有所不同,具体取决于您创建 AWS 帐户的时间、实例类型和使用的 AMI 虚拟化类型。参考:aws account,instance type.
如果上述参数中的 none 限制了 EC2 classic 的使用,您应该能够在不定义任何 VPC 的情况下创建新实例。
2。使用 Ansible
创建一个新的 EC2 实例由于您的实例不存在,因此动态清单文件 (ec2.py
) 没有用。尝试在本地计算机上指示 ansible 运行。
创建一个新的清单文件,例如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'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 实例,并将其 public IP 注册为一个 ansible 主机变量 ec2hosts
即。就像您在清单文件中定义它一样。如果你想提供刚刚创建的实例,这很有用,只需添加一个新的 hosts: ec2hosts
.
最终,按如下方式启动ansible:
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需要安装在运行执行上述ansible命令的机器上。
3。使用ansible的ec2动态库存
EC2 动态清单由 2 个文件组成,ec2.py
和 ec2.ini
。在您的具体情况下,我认为您的问题是由于 ec2.py
无法找到 ec2.ini
文件。
要解决您的问题,请将 ec2.py
和 ec2.ini
复制到您打算 运行 ansible 的计算机中的同一文件夹,例如至 /etc/ansible/
.
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 格式的主机列表打印到标准输出。