Bri*_*asa 4 ansible ansible-playbook
我对如何使用Ansible启动EC2实例感到困惑.
我正在尝试使用ec2.py库存脚本.我不确定应该使用哪一个,因为有三个安装了Ansible:
我认为在库存中运行一个/最有意义,所以我使用:
ansible-playbook launch-ec2.yaml -i ec2.py
Run Code Online (Sandbox Code Playgroud)
这给了我:
msg: Either region or ec2_url must be specified
Run Code Online (Sandbox Code Playgroud)
所以我添加一个区域(即使我指定了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
Run Code Online (Sandbox Code Playgroud)
我想亚马逊最近必须改变ec2所以你需要使用VPC吗?即使我尝试从亚马逊的控制台启动实例,也会禁用"EC2 Classic"选项.
当我尝试在cloud/amazon /中使用ec2.py脚本时,我得到:
ERROR: Inventory script (/software/ansible/lib/ansible/modules/core/cloud/amazon/ec2.py) had an execution error:
Run Code Online (Sandbox Code Playgroud)
没有比这更详细的细节了.
经过一些搜索,我发现/ module_utils中的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
Run Code Online (Sandbox Code Playgroud)
有没有人对如何使这个工作有任何想法?什么是正确的文件使用?在这一点上,我完全迷失了如何使这个工作.
小智 8
您的帖子中有几个问题.我将尝试将它们总结为三个项目:
ec2.py?您的选项将根据您何时创建AWS账户,实例类型和使用的AMI虚拟化类型而有所不同.参考:aws帐户,实例类型.
如果上述参数均未限制EC2 classic的使用,则应该能够创建新实例而无需定义任何VPC.
由于您的实例不存在,因此动态库存文件(ec2.py)无效.尝试指示ansible在本地计算机上运行.
创建新的库存文件,例如new_hosts,包含以下内容:
[localhost]
127.0.0.1
Run Code Online (Sandbox Code Playgroud)
然后你的剧本,例如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
Run Code Online (Sandbox Code Playgroud)
这个游戏将创建一个EC2实例,它将其公共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
Run Code Online (Sandbox Code Playgroud)
环境变量的目的ANSIBLE_HOST_KEY_CHECKING=false是避免在连接到实例时提示添加ssh主机密钥.
注意: boto需要安装在运行上述ansible命令的机器上.
EC2动态库存由2个文件组成,ec2.py和ec2.ini.在您的特定情况下,我认为您的问题是由于ec2.py无法找到ec2.ini文件的事实.
要解决您的问题,请复制ec2.py并ec2.ini打印到您打算运行ansible的计算机中的同一文件夹,例如/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
Run Code Online (Sandbox Code Playgroud)
对于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
Run Code Online (Sandbox Code Playgroud)
配置ec2.ini并运行ec2.py,应该将一个ini格式的主机列表打印到stdout.