API 向客户端发送重启后 Ansible 失去连接

Ansible loses connection after API sends reboot to client

我创建了一个角色来设置我们的新服务器,但 运行 遇到了一个问题。播放会触发一个 Python 脚本。此脚本将有关服务器的信息提交给我们的 API。该脚本最终从 API 触发一个作业,并且服务器由该作业重新启动。在 Python 脚本完成之前,播放不会结束。但是,Ansible 在重启期间失去连接,因为 play 本身没有启动重启,并且 playbook 失败。我已经尝试过以下方法。

- name: Run setup.py
  command: "{{ run_setup_py }} --username {{ username }} --password {{ password }} --ip {{ ansible_host }} --hostname {{ host_name }}"
  async: 1800
  poll: 60

async 超时后失败。看来 Ansible 无法识别脚本已完成并失败。我尝试了其他一些 async 游戏,例如

- name: Run setup.py
  command: "{{ run_setup_py }} --username {{ username }} --password {{ password }} --ip {{ ansible_host }} --hostname {{ host_name }}"
  async: 600
  poll: 0
  register: run_setup
- name: check on async task
  async_status:
    jid: "{{ run_setup.ansible_job_id }}"
  register: job_result
  until: job_result.finished
  retries: 1000
  delay: 450

以下任何一项都没有运气。出于某种原因 wait_for_connection 在播放级别完全跳过 Python 脚本并导致后面的播放失败。

- name: Wait until remote system is reachable
  wait_for_connection:
    delay: 180
    sleep: 15
  delegate_to: localhost
- name: Wait until remote system is reachable
  wait_for_connection:
    delay: 180
    sleep: 15

我尝试在剧本级别添加 ignore_unreachable: yes。 Ansible 尝试立即重新连接但由于服务器仍在 POST.

而失败

脚本 运行 在远程主机上 运行 时运行良好,因此这不是脚本的问题。我们设置的剩余步骤 不能 运行 直到脚本 运行 之后。 在这一点上,任何关于如何维护 Ansible 连接的答案都将不胜感激。如果可能的话,最好不要浪费时间,例如持续连接检查。

如果有任何信息遗漏或混淆,我深表歉意,我现在才使用 Ansible 大约一个月。目前使用 ansible-core 2.12

您需要 wait_for 而不是 wait_for_connection。本地是运行:

- name: Run setup.py
  command: "{{ run_setup_py }} --username {{ username }} --password {{ password }}

- name: Wait for the reboot and reconnect
  wait_for:
    port: 22
    host: '{{ (ansible_ssh_host|default(ansible_host))|default(inventory_hostname) }}'
    search_regex: OpenSSH
    delay: 10
    timeout: 60
  connection: local

- name: Check the Uptime of the servers
  shell: "uptime"
  register: Uptime

- name: Show uptime
  debug:
    var: Uptime