需要代码来使用 Ansible 检查服务器连接
Posted
技术标签:
【中文标题】需要代码来使用 Ansible 检查服务器连接【英文标题】:Need code to Check for server connectivity using Ansible 【发布时间】: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
我在此处从 *** 获取代码:Ansible: Abort Execution if a host is unreachable
我使用的是 ansible 版本 2.9.x
注意:这段代码曾经在旧版本的 ansible 上运行良好,比如 2.4
你能推荐一下吗?
【问题讨论】:
【参考方案1】:当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
当前播放运行的主机列表,不受序列号限制。此列表中排除了失败/无法访问的主机。
也就是说,无法访问的主机不会包含在
anisble_play_hosts
,所以你的任务是检查是否可达
主机是可访问的,这将永远是正确的。
【讨论】:
以上是关于需要代码来使用 Ansible 检查服务器连接的主要内容,如果未能解决你的问题,请参考以下文章