我的剧本有问题,应该通过内置模块创建新的EC2实例并连接到它们以设置一些默认内容。
我浏览了很多教程/帖子,但没有一个提到同样的问题,因此我在那里问。
在创建方面,一切都进展顺利,但是当我创建了实例并成功等待SSH启动时。我收到错误,说机器无法访问。
UNREACHABLE! => {"changed": false, "msg": "ERROR! SSH Error: data could not be sent to the remote host. Make sure this host can be reached over ssh", "unreachable": true}
我尝试手动连接(从终端连接到同一主机(并且成功了(在剧本等待连接时(。我还尝试在 ansible.cfg 中增加超时。我验证了给定的主机名是有效的(确实如此(,并且还尝试了公共 ip 而不是公共 DNS,但没有任何帮助。
基本上我的剧本看起来像那样
---
- name: create ec2 instances
hosts: local
connection: local
gather_facts: False
vars:
machines:
- { type: "t2.micro", instance_tags: { Name: "machine1", group: "some_group" }, security_group: ["SSH"] }
tasks:
- name: lunch new ec2 instances
local_action: ec2
group={{ item.security_group }}
instance_type={{ item.type}}
image=...
wait=true
region=...
keypair=...
count=1
instance_tags=...
with_items: machines
register: ec2
- name: wait for SSH to come up
local_action: wait_for host={{ item.instances.0.public_dns_name }} port=22 delay=60 timeout=320 state=started
with_items: ec2.results
- name: add host into launched group
add_host: name={{ item.instances.0.public_ip }} group=launched
with_items: ec2.results
- name: with the newly provisioned EC2 node configure basic stuff
hosts: launched
sudo: yes
remote_user: ubuntu
gather_facts: True
roles:
- common
注意:在许多教程中,创建以不同方式访问的 ec2 实例的结果,但这可能是针对不同的问题。
谢谢
解决:
我不知道怎么做,但它突然开始工作了。没有线索。如果我能找到一些新信息,将更新这个问题
有几点可能会有所帮助:
-
我猜这是版本差异,但我从未在注册的"ec2"变量中看到"结果"键。无论如何,我通常使用"tagged_instances"——这确保了即使重头戏没有创建实例(即,因为之前的运行中已经存在匹配的实例(,该变量仍将返回可用于将新主机添加到清单的实例数据。
-
尝试将"search_regex:"OpenSSH"添加到"wait_for"播放中,以确保在SSH守护程序完全启动之前它不会尝试运行。
修改后的重头戏如下所示:
- name: wait for SSH to come up
local_action: wait_for host={{ item.public_dns_name }} port=22 delay=60 timeout=320 state=started search_regex="OpenSSH"
with_items: ec2.tagged_instances
- name: add host into launched group
add_host: name={{ item.public_ip }} group=launched
with_items: ec2.tagged_instances
当然,您还希望确保 Ansible 知道在对远程主机进行 SSH 时使用指定的密钥,方法是在清单条目中添加"ansible_ssh_private_key_file"或指定"--private-key=..."。在命令行上。