gpt4 book ai didi

用于检查主机在关机后是否真的离线的 Ansible 任务

转载 作者:行者123 更新时间:2023-12-03 23:37:24 26 4
gpt4 key购买 nike

我正在使用以下 Ansible playbook 一次性关闭远程 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

关于这个的两个问题:

  1. 是否有任何可用的模块可以以比运行两个自定义命令更优雅的方式处理关机?
  2. 有什么方法可以检查机器是否真的停机了?还是从同一个剧本中检查这一点是一种反模式?

我尝试了 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"}

最佳答案

在更受限制的环境中,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 任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48423272/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com