【发布时间】:2021-07-29 12:11:40
【问题描述】:
下面是我如何调用我的 ansible 游戏
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
我在此处从 stackoverflow 获取代码:Ansible: Abort Execution if a host is unreachable
我使用的是 ansible 版本 2.9.x
注意:这段代码曾经在旧版本的 ansible 上运行良好,比如 2.4
你能推荐一下吗?
【问题讨论】:
标签: ssh ansible ping reachability connectivity