gpt4 book ai didi

ansible - 使用 Ansible 配置 EC2 实例时遇到问题

转载 作者:行者123 更新时间:2023-12-02 20:58:28 25 4
gpt4 key购买 nike

我对如何使用 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

我认为在 inventory/中运行这个是最有意义的,所以我使用以下方式运行它:

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

我想 Amazon 最近一定更改了 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

有人对如何让它发挥作用有任何想法吗?使用的正确文件是什么?我现在完全不知道如何让它工作。

最佳答案

您的帖子中有几个问题。我将尝试将它们总结为三项:

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

1。 EC2经典

您的选项会有所不同,具体取决于您创建 AWS 账户的时间、实例类型和使用的 AMI 虚拟化类型。引用:aws account , instance type .

如果上述参数均不限制 EC2 classic 的使用,您应该能够在不定义任何 VPC 的情况下创建新实例。

2。使用 Ansible 创建新的 EC2 实例

由于您的实例尚不存在,动态 list 文件 (ec2.py) 毫无用处。尝试指示 ansible 在本地计算机上运行。

创建一个新的 list 文件,例如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 即。就像您已在 list 文件中定义它一样。如果您想要配置刚刚创建的实例,只需使用 hosts: ec2hosts 添加新的 play 即可,这非常有用。

最终,按如下方式启动 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 主机 key 。

注意: boto 需要安装在运行上述 ansible 命令的机器上。

3。使用ansible的ec2动态 list

EC2 动态 list 由 2 个文件组成:ec2.pyec2.ini。在您的特定情况下,我认为您的问题是由于 ec2.py 无法找到 ec2.ini 文件所致。

要解决您的问题,请将 ec2.pyec2.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 - 使用 Ansible 配置 EC2 实例时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29857688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com