gpt4 book ai didi

Ansible 剧本 : How to write unreachable inventory host names to a file

转载 作者:行者123 更新时间:2023-12-04 16:43:00 26 4
gpt4 key购买 nike

我正在为 list 主机列表编写剧本:-

如果主机可访问,写入“connection=1”并写入文件。如果主机不可访问,写入同一个文件“connection=0”

据我了解,Ansible 不会以可访问的方式存储无法访问的热点信息(当 ssh 失败时)。

你能帮我解决一下吗?我的剧本贴在下面。shell 任务根本不执行,因为主机不可访问

下面是我的剧本

- hosts: '{{ host }}'
gather_facts: False
vars:
dest: /tmp/trace
tasks:
- copy:
content: ''
dest: "{{ dest }}"
run_once: yes
delegate_to: 127.0.0.1
- shell: ping {{ inventory_hostname }} -c 1
register: ping_status
ignore_errors: yes
- setup:
filter: ansible_*
- lineinfile:
dest: "{{ dest }}"
line: 'Host:{{ inventory_hostname }},OS:{{ ansible_distribution }},Kernel:{{ansible_kernel}},OSVersion:{{ansible_distribution_version}},FreeMemory:{{ansible_memfree_mb}},connection:{{ping_status.rc}}'
ignore_errors: true
delegate_to: 127.0.0.1

最佳答案

您的剧本存在一些问题。首先是您正在尝试同时执行 shell和一个 setup远程主机上的任务,如果该主机不可用,这当然不会工作。

运行ping 甚至没有意义远程主机上的任务:您想使用委托(delegate)在本地主机上运行该任务。我们可以做这样的事情来记录每个主机的可用性作为主机变量:

---
- hosts: all
gather_facts: false
tasks:
- delegate_to: localhost
command: ping -c1 "{{ hostvars[inventory_hostname].ansible_host|default(inventory_hostname) }}"
register: ping
ignore_errors: true

- set_fact:
available: "{{ ping.rc == 0 }}"

您正在尝试运行 setup针对远程主机的模块,但这只有在远程主机可用时才有意义,因此我们需要以 ping 的结果为条件任务:

- setup:
filter: "ansible_*"
when: ping.rc == 0

有了它,我们可以生成一个文件,其中包含有关每个主机可用性的信息。我正在使用 lineinfile在这里因为那是你在你的例子中使用的,但如果我自己写这个我可能会使用 template任务:

- hosts: localhost
gather_facts: false
tasks:
- lineinfile:
dest: ./available.txt
line: "Host: {{ item }}, connection={{ hostvars[item].available }}"
regexp: "Host: {{ item }}"
create: true
loop: "{{ groups.all }}"

当然,在您的示例中,您试图包含有关主机的各种其他事实:

        line: 'Host:{{ inventory_hostname }},OS:{{ ansible_distribution }},Kernel:{{ansible_kernel}},OSVersion:{{ansible_distribution_version}},FreeMemory:{{ansible_memfree_mb}},connection:{{ping_status.rc}}'

如果目标主机不可用,这些事实将不可用,因此您需要使用 {% if <condition> %}...{% endif %} 使所有这些成为条件。构造:

line: "Host:{{ item }},connection:{{ hostvars[item].available }}{% if hostvars[item].available %},OS:{{ hostvars[item].ansible_distribution }},Kernel:{{ hostvars[item].ansible_kernel }},OSVersion:{{ hostvars[item].ansible_distribution_version }},FreeMemory:{{ hostvars[item].ansible_memfree_mb }}{% endif %}"

这使得最终的剧本看起来像这样:

---
- hosts: all
gather_facts: false
tasks:
- delegate_to: localhost
command: ping -c1 "{{ hostvars[inventory_hostname].ansible_host|default(inventory_hostname) }}"
register: ping
ignore_errors: true

- set_fact:
available: "{{ ping.rc == 0 }}"

- setup:
when: ping.rc == 0

- hosts: localhost
gather_facts: false
tasks:
- lineinfile:
dest: ./available.txt
line: "Host:{{ item }},connection:{{ hostvars[item].available }}{% if hostvars[item].available %},OS:{{ hostvars[item].ansible_distribution }},Kernel:{{ hostvars[item].ansible_kernel }},OSVersion:{{ hostvars[item].ansible_distribution_version }},FreeMemory:{{ hostvars[item].ansible_memfree_mb }}{% endif %}"
regexp: "Host: {{ item }}"
create: true
loop: "{{ groups.all }}"

关于Ansible 剧本 : How to write unreachable inventory host names to a file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55935582/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com