我是新来的 Ansible 并在检查系统是否需要重启后尝试操作,这将由ansible本身处理,我能够识别系统是否需要重启,之后系统需要一段时间才能正常运行(启动状态),默认情况下,它会执行5次ssh连接尝试来验证系统是否启动。我想补充几点。默认设置为5次ssh连接尝试,在 ansible.config 是否可以在playbook中覆盖该值。
如果ssh尝试次数不变,有没有办法增加重试的延迟(例如:5),我试过同样的方法,但失败了。
- name: Check for require reboot
hosts: "{{ target_host }}"
remote_user: root
gather_facts: False
tasks:
- name: Performing reboot check now after patch integration
command: needs-restarting -r
register: needsrestarting
changed_when:
- needsrestarting.rc != 0
failed_when:
- needsrestarting.rc != 1
- needsrestarting.rc != 0
- name: Reboot the server
tags: reboot
command: reboot
async: 1
poll: 0
# when:
# - needsrestarting.rc == 1
- name: Adding delay for system to come up
wait_for:
timeout: 300
- name: Wait for the nodes to wake up for login with fixed timer
wait_for:
host={{ ansible_ssh_host }}
port={{ ansible_ssh_port }}
timeout=300
state=started
delegate_to= "{{ inventory_hostname }}"
ignore_errors= yes
delay: 60
retries: 20
日志。
UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: OpenSSH_7.4p1, OpenSSL 1.0.2k-fips 26 Jan 2017
你需要 委托 的 wait_for
赋予 Ansible控制器:
- name: Adding delay for system to come up
wait_for:
timeout: 300
delegate_to: localhost
否则, wait_for
任务要执行 对准由于它正在重启,所以无法访问。这是一个通用模块,有很多用途。有时远程使用有意义,有时没有。
另一方面,人们可能更喜欢在本地执行剧本,最终将任务委托给目标。这个决定可能由多少任务要在本地执行和多少任务不在本地执行来驱动。
- name: Check for require reboot
hosts: "{{ target_host }}"
remote_user: root
gather_facts: False
connection: local
tasks:
...
请注意,本地执行与目标主机,库存等无关,因为它们无论如何都会被迭代。你可以只通过本地执行来处理一整个目标列表(如 vmWare模块例如,在控制器上执行并在库存目标上创建磁盘)。)
从Ansible文档中的 wait_for
模块实际上,你可能会改善你的等待条件,对于特定的ssh连接(默认 "超时 "值为300s)。
# Do not assume the inventory_hostname is resolvable and delay 10 seconds at start
- name: Wait 300 seconds for port 22 to become open and contain "OpenSSH"
wait_for:
port: 22
host: '{{ (ansible_ssh_host|default(ansible_host))|default(inventory_hostname) }}'
search_regex: OpenSSH
delay: 10
connection: local
或者使用特定的 wait_for_connection
模块 的目的。
# Wake desktops, wait for them to become ready and continue playbook
- hosts: all
gather_facts: no
tasks:
- name: Send magic Wake-On-Lan packet to turn on individual systems
wakeonlan:
mac: '{{ mac }}'
broadcast: 192.168.0.255
delegate_to: localhost
- name: Wait for system to become reachable
wait_for_connection:
- name: Gather facts for first time
setup:
...
请注意第一个任务,在本地执行,因为目标还没有找到,但却是魔法包的目的地。
在这之后,就不需要再使用 wait_for_connection
拟下放的任务。因为它是这样运作的.