用于检查主机在关机后是否真正离线的 Ansible 任务
Ansible task for checking that a host is really offline after shutdown
我正在使用以下 Ansible 剧本一次性关闭远程 Ubuntu 主机列表:
- hosts: my_hosts
become: yes
remote_user: my_user
tasks:
- name: Confirm shutdown
pause:
prompt: >-
Do you really want to shutdown machine(s) "{{play_hosts}}"? Press
Enter to continue or Ctrl+C, then A, then Enter to abort ...
- name: Cancel existing shutdown calls
command: /sbin/shutdown -c
ignore_errors: yes
- name: Shutdown machine
command: /sbin/shutdown -h now
关于这个的两个问题:
- 是否有可用的模块可以以比 运行 两个自定义命令更优雅的方式处理关闭?
- 有什么方法可以检查机器是否真的宕机了?或者从同一个剧本中检查它是一种反模式?
我用 net_ping module 尝试了一些东西,但我不确定这是否是它的真正目的:
- name: Check that machine is down
become: no
net_ping:
dest: "{{ ansible_host }}"
count: 5
state: absent
然而,这失败了
FAILED! => {"changed": false, "msg": "invalid connection specified, expected connection=local, got ssh"}
没有shutdown
模块。您可以使用一次性调用:
- name: Shutdown server
become: yes
shell: sleep 2 && /sbin/shutdown -c && /sbin/shutdown -h now
async: 1
poll: 0
至于net_ping
,是针对交换机、路由器等网络设备。如果你依赖 ICMP 消息来测试关机过程,你可以使用这样的东西:
- name: Store actual host to be used with local_action
set_fact:
original_host: "{{ ansible_host }}"
- name: Wait for ping loss
local_action: shell ping -q -c 1 -W 1 {{ original_host }}
register: res
retries: 5
until: ('100.0% packet loss' in res.stdout)
failed_when: ('100.0% packet loss' not in res.stdout)
changed_when: no
这将等待 100% packet loss
或重试 5 次后失败。
在这里你想使用 local_action
因为否则命令在远程主机上执行(应该是关闭的)。
并且您想使用技巧将 ansible_host
存储到临时事实中,因为当委派给本地主机时 ansible_host
被替换为 127.0.0.1
。
在更受限的环境中,ping 消息被阻止,您可以监听 ssh 端口,直到它关闭。在我的例子中,我将超时设置为 60 秒。
- name: Save target host IP
set_fact:
target_host: "{{ ansible_host }}"
- name: wait for ssh to stop
wait_for: "port=22 host={{ target_host }} delay=10 state=stopped timeout=60"
delegate_to: 127.0.0.1
我正在使用以下 Ansible 剧本一次性关闭远程 Ubuntu 主机列表:
- hosts: my_hosts
become: yes
remote_user: my_user
tasks:
- name: Confirm shutdown
pause:
prompt: >-
Do you really want to shutdown machine(s) "{{play_hosts}}"? Press
Enter to continue or Ctrl+C, then A, then Enter to abort ...
- name: Cancel existing shutdown calls
command: /sbin/shutdown -c
ignore_errors: yes
- name: Shutdown machine
command: /sbin/shutdown -h now
关于这个的两个问题:
- 是否有可用的模块可以以比 运行 两个自定义命令更优雅的方式处理关闭?
- 有什么方法可以检查机器是否真的宕机了?或者从同一个剧本中检查它是一种反模式?
我用 net_ping module 尝试了一些东西,但我不确定这是否是它的真正目的:
- name: Check that machine is down
become: no
net_ping:
dest: "{{ ansible_host }}"
count: 5
state: absent
然而,这失败了
FAILED! => {"changed": false, "msg": "invalid connection specified, expected connection=local, got ssh"}
没有shutdown
模块。您可以使用一次性调用:
- name: Shutdown server
become: yes
shell: sleep 2 && /sbin/shutdown -c && /sbin/shutdown -h now
async: 1
poll: 0
至于net_ping
,是针对交换机、路由器等网络设备。如果你依赖 ICMP 消息来测试关机过程,你可以使用这样的东西:
- name: Store actual host to be used with local_action
set_fact:
original_host: "{{ ansible_host }}"
- name: Wait for ping loss
local_action: shell ping -q -c 1 -W 1 {{ original_host }}
register: res
retries: 5
until: ('100.0% packet loss' in res.stdout)
failed_when: ('100.0% packet loss' not in res.stdout)
changed_when: no
这将等待 100% packet loss
或重试 5 次后失败。
在这里你想使用 local_action
因为否则命令在远程主机上执行(应该是关闭的)。
并且您想使用技巧将 ansible_host
存储到临时事实中,因为当委派给本地主机时 ansible_host
被替换为 127.0.0.1
。
在更受限的环境中,ping 消息被阻止,您可以监听 ssh 端口,直到它关闭。在我的例子中,我将超时设置为 60 秒。
- name: Save target host IP
set_fact:
target_host: "{{ ansible_host }}"
- name: wait for ssh to stop
wait_for: "port=22 host={{ target_host }} delay=10 state=stopped timeout=60"
delegate_to: 127.0.0.1