需要代码来检查服务器连接使用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   
Ansible: Abort Execution if a host is unreachable

I'm on ansible version 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上迭代,从文档:

ansible_play_hosts

当前运行的主机列表,不受串行限制。失败/不可达的主机从此列表中排除。

也就是说,无法访问的主机将不包含在anisble_play_hosts,所以您的任务是检查是否可达主机是可访问的,这将始终为真。

最新更新