Loops 获取Ansible中任务失败的循环中的项值
有一件有趣的事,我试图用Ansible来表演,但出了点问题 例如:Loops 获取Ansible中任务失败的循环中的项值,loops,ansible,Loops,Ansible,有一件有趣的事,我试图用Ansible来表演,但出了点问题 例如: 文本文件包含某些主机的IP地址 我需要读取文件中的每一行,并检查SSH端口是否为每个IP地址打开 如果我在检查主机端口时超时,那么我应该知道哪个IP地址似乎有问题,并将此项传递到变量中以执行其他检查 文件内容: 1.1.1.1 1.1.1.2 1.1.1.2 Ansible剧本: - hosts: localhost connection: local gather_facts: no tasks: - nam
1.1.1.1
1.1.1.2
1.1.1.2
Ansible剧本:
- hosts: localhost
connection: local
gather_facts: no
tasks:
- name: Get list of IP adresses
shell: cat /home/file
register: ip_addrs
- name: Check SSH port
wait_for:
host: "{{ item }}"
port: 22
timeout: 5
with_items: "{{ ip_addrs.stdout_lines }}"
ignore_errors: true
我的剧本以以下结果结尾:
ok - 1.1.1.1
ok - 1.1.1.2
timeout - 1.1.1.3
循环的结果包括一个回复中每个任务的结果。
我的问题:如何在导致任务失败的循环中提取项的值
类似于
为循环中的任务注册:result
,当:result | failed时为项目注册一些命令,注册循环任务的结果并检查每个项目的子结果,如下所示:
- name: Check SSH port
wait_for:
host: "{{ item }}"
port: 22
timeout: 5
with_items: "{{ ip_addrs.stdout_lines }}"
ignore_errors: true
register: loop_res
- debug:
msg: bad host
when: item is failed
with_items: "{{ loop_res.results }}"