需要代码来使用 Ansible 检查服务器连接

Need code to Check for server connectivity using Ansible

以下是我调用 ansible play 的方式

ansible-playbook -v -i /web/admin/playbooks/applist.hosts /web/admin/playbooks/restart.yml -e ENV=dev -e ACTION=start --tags checkserver

下面是我的剧本:

- hosts: "{{ENV}}_*"
  user: "{{USER}}"
  gather_facts: yes
  pre_tasks:

       - name: Check PING
         ping:
         tags: checkserver


- hosts: "{{ENV}}_*"
  user: "{{USER}}"
  gather_facts: yes
  pre_tasks:

       - name: Check if all hosts are reachable
         fail:
           msg: >
             [REQUIRED] ALL hosts to be reachable, so flagging {{ inventory_hostname }} as failed,
              because host {{ item }} has no facts, meaning it is UNREACHABLE.
         when: hostvars[item].ansible_facts is defined
         with_items: "{{ ansible_play_hosts }}"
         run_once: true
         tags: checkserver

我确定连接良好并且可以访问目标主机。

从以下成功的 ansible ping 模块输出中可以明显看出:

PLAY [dev_*] *******************************************************************

TASK [Gathering Facts] *********************************************************
[1;35m[WARNING]: Platform sunos on host remotehost7.com is using the[0m
[1;35mdiscovered Python interpreter at /usr/bin/python, but future installation of[0m
[1;35manother Python interpreter could change the meaning of that path. See https://d[0m
[1;35mocs.ansible.com/ansible/2.10/reference_appendices/interpreter_discovery.html[0m
[1;35mfor more information.[0m
[0;32mok: [remotehost7.com][0m

PLAY [dev_*] *******************************************************************

TASK [Gathering Facts] *********************************************************
[0;32mok: [remotehost7.com][0m

TASK [Check PING] **************************************************************
[0;32mok: [remotehost7.com] => {"changed": false, "ping": "pong"}[0m

PLAY [dev_*] *******************************************************************

TASK [Gathering Facts] *********************************************************
[0;32mok: [remotehost7.com][0m

TASK [Check if all hosts are reachable] ****************************************
[0;31mfailed: [remotehost7.com] (item=remotehost7.com) => {"ansible_loop_var": "item", "changed": false, "item": "remotehost7.com", "msg": "[REQUIRED] ALL hosts to be reachable, so flagging remotehost7.com as failed,\n because host remotehost7.com has no facts, meaning it is UNREACHABLE.\n"}[0m

NO MORE HOSTS LEFT *************************************************************

PLAY RECAP *********************************************************************
[0;31mremotehost7.com[0m    : [0;32mok=4   [0m changed=0    unreachable=0    [0;31mfailed=1   [0m skipped=0    rescued=0    ignored=0   

我在这里从 Whosebug 中获取了代码:Ansible: Abort Execution if a host is unreachable

我使用的是 ansible 版本 2。9.x

注意:此代码过去在 ansible 的旧版本(如 2.4)上运行良好

你能推荐一下吗?

当定义 ansible_facts 时,您明确地失败了,这通常意味着主机是可访问的。也许您只是倒退了逻辑?我想你的意思是:

- name: Check if all hosts are reachable
  fail:
    msg: >
      [REQUIRED] ALL hosts to be reachable, so flagging {{ inventory_hostname }} as failed,
       because host {{ item }} has no facts, meaning it is UNREACHABLE.
  when: hostvars[item].ansible_facts is not defined
  with_items: "{{ ansible_play_hosts }}"
  run_once: true
  tags: checkserver

但即使逻辑固定,我也不认为这会给你带来任何好处: 您正在遍历 ansible_play_hosts,并且从 documentation:

ansible_play_hosts

List of hosts in the current play run, not limited by the serial. Failed/Unreachable hosts are excluded from this list.

也就是说,无法访问的主机不会包含在 anisble_play_hosts,因此您的任务是检查是否可达 主机是可达的,这将永远是真的。