Ansible wait_for
ssh停止工作。 Ansible剧本能够启动ec2实例,wait_for
模块用于等待ssh启动。等待ssh时Ansible超时。
我的剧本
- name: Configuring and lanuching EC2 instance
hosts: localhost
connection: local
vars_files:
- general_vars
tasks:
- name: Launch instance
ec2:
instance_type: "{{ instance_type }}"
image: "{{ image_ami }}"
region: "{{ region }}"
vpc_subnet_id: "{{ subnet_id }}"
assign_public_ip: yes
group_id: "{{ security_group }}"
key_name: "{{ key_pair }}"
volumes:
- device_name: /dev/xvda
volume_type: gp2
volume_size: 8
register: ec2
- name: Wait for ssh to come up
wait_for: host="{{ ec2.instances[0].public_dns_name }}" port=22 delay=10 timeout=300
with_items: "{{ ec2.instances }}"
- name: Save the Ip Address of the machine
add_host:
hostname: "{{ item.public_ip }}"
groupname: ec2_instance_ips
with_items: "{{ ec2.instances }}"
错误跟踪
The full traceback is:
File "/var/folders/y3/t87nx4q95w9_jmg80csrn6bc0000gn/T/ansible_SpGSRH/ansible_module_wait_for.py", line 540, in main
s = _create_connection(host, port, min(connect_timeout, alt_connect_timeout))
File "/var/folders/y3/t87nx4q95w9_jmg80csrn6bc0000gn/T/ansible_SpGSRH/ansible_module_wait_for.py", line 405, in _create_connection
connect_socket = socket.create_connection((host, port), connect_timeout)
File "/usr/local/opt/python@2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 575, in create_connection
raise err
}) => {
"changed": false,
"elapsed": 300,
"invocation": {
"module_args": {
"active_connection_states": [
"ESTABLISHED",
"FIN_WAIT1",
"FIN_WAIT2",
"SYN_RECV",
"SYN_SENT",
"TIME_WAIT"
],
"connect_timeout": 5,
"delay": 10,
"exclude_hosts": null,
"host": "",
"msg": null,
"path": null,
"port": 22,
"search_regex": null,
"sleep": 1,
"state": "started",
"timeout": 300
}
},
"item": {
"ami_launch_index": "0",
"architecture": "x86_64",
"block_device_mapping": {},
"dns_name": "",
"ebs_optimized": false,
"groups": {
"sg-09664e62": "yyyzzzzbbbbbb"
},
"hypervisor": "xen",
"id": "i-XXxxxxxxxxxxxxxxx",
"image_id": "XXXXXXXXXXXXX",
"instance_type": "t2.micro",
"kernel": null,
"key_name": "XXXX",
"launch_time": "XXXXXX",
"placement": "ccccccccccccccc",
"private_dns_name": "",
"private_ip": "XXXXX",
"public_dns_name": "",
"public_ip": null,
"ramdisk": null,
"region": "XXXX",
"root_device_name": "/dev/sda1",
"root_device_type": "ebs",
"state": "pending",
"state_code": 0,
"tags": {},
"tenancy": "default",
"virtualization_type": "hvm"
},
"msg": "Timeout when waiting for :22"
Ansible不会为public_dns_name
返回任何内容,并且public_ip
为“ null”。
结果是,即使正确生成了ec2,wait_for
ssh也失败了,而且我能够通过适当的密钥手动ssh到计算机。
我做错了什么?
答案 0 :(得分:1)
- name: Wait for ssh to come up wait_for: host="{{ ec2.instances[0].public_dns_name }}" port=22 delay=10 timeout=300 with_items: "{{ ec2.instances }}"
您正在使用with_items
构造,但是我看不到item
变量的任何使用。我不使用AWS,但也许您需要
- name: Wait for ssh to come up
wait_for: host="{{ item.public_dns_name }}" port=22 delay=10 timeout=300
with_items: "{{ ec2.instances }}"
答案 1 :(得分:0)
Ansible ec2模块具有wait
布尔值,以等待ec2实例达到理想状态。
https://docs.ansible.com/ansible/2.6/modules/ec2_module.html
一旦添加了您的wait_for ssh即可。
答案 2 :(得分:0)
您可以尝试在下面的剧本中将公共IP保存到主机文件中,以备将来使用。
- name: Create an EC2 instance
ec2:
key_name: "{{ project_name }}-{{ env }}-key"
region: "{{ region }}"
group_id: "{{ test_firewall.group_id }}"
instance_type: "{{ instance_type }}"
image: "{{ ami }}"
wait: yes
instance_tags:
env: "{{ env }}"
count_tag: env
exact_count: 1
vpc_subnet_id: subnet-0e4be06e12efe8eca
assign_public_ip: yes
register: ec2
- name: Add the newly created EC2 instance(s) to host group
lineinfile: dest={{ hostpath }}
regexp={{ item.public_ip }}
insertafter="[webserver]"
line="{{ item.public_ip }} {{hoststring}}"
state=present
with_items: ec2.instances
- wait_for: path={{ hostpath }} search_regex={{hoststring}}
- name: Wait for SSH to come up
local_action: wait_for
host={{ item.public_ip }}
port=22
state=started
with_items: ec2.instances
- name: Add IP to ec2_hosts group
add_host: hostname={{ item.public_ip }} groups=ec2_hosts
with_items: ec2.instances
谢谢
答案 3 :(得分:-1)
模块wait_for_connection对我有用。我认为,由于Ansible使用ssh连接到实例,因此该模块隐式检查SSH连接。