我有一组任务,这些任务由单独的角色组成,这些角色以“何时”条件运行,仅在某些主机上执行,在这些任务中,我使用产生我所寻找内容的vars / set_facts拉取特定值-但整个过程的最后一个角色是从所有人那里获取结果/事实,并将其应用于Jinja HTML模板。
最初,我在一个角色中混合了多个任务,这些任务将与包含子组的单个组一起运行-这些是由“何时”条件决定的-因为我已经将它们分开,所以请确保没有重复的变量/事实,甚至引入了一个桥接角色,该角色将set_facts用作vars,然后再次将其设置为vars,但使用了proxy_to:localhost。
hosts: "{{ site_code }}"
ignore_errors: True
gather_facts: yes
tasks:
- name: DMVPN Primary Check
include_role:
name: dmvpn-primary
when: inventory_hostname in groups['dmvpn_primary']
- name: DMVPN Backup Check
include_role:
name: dmvpn-secondary
when: inventory_hostname in groups['dmvpn_secondary']
- name: Fact Cache
include_role:
name: fact-gather
- name: HTML Report
include_role:
name: html-report
run_once: true
当我将主机限制在一个组中时,proxy_to会选择该主机,然后选择它自己的事实,但是将多台主机与{{site_code}} var组合在一起会导致冲突,最终导致错误“ AnsibleUndefinedVariable” < / p>
编辑:这是dmvpn角色,事实收集角色和html.j2文件的片段,显示了几个变量的流,这些变量最终以致命的形式出现:[site-vpn01-> 127.0.0.1]:失败! => { “已更改”:错误, “ msg”:“ AnsibleUndefinedVariable:'dmvpn1_host_name'未定义”
roles \ dmvpn-primary
- name: Primary Tunnel Up/Down
vars:
dmvpn1_tun_stat_0: "{{ dmvpn1_result.stdout[0] }}"
dmvpn1_tun_stat_1: "{{ dmvpn1_tun_stat_0.split() }}"
dmvpn1_tun_stat_2: "{{ dmvpn1_tun_stat_1[3] }}"
set_fact:
dmvpn1_tun_status: "{{ dmvpn1_tun_stat_2 }}"
dmvpn1_hostname: "{{ inventory_hostname }}"
dmvpn1_host_ip: "{{ ansible_host }}"
cacheable: yes
角色\事实收集:
- name: Primary Inventory Facts
vars:
dmvpn1_host_name: "{{ dmvpn1_hostname }}"
dmvpn1_host_ipv4: "{{ dmvpn1_host_ip }}"
dmvpn1_google_dns1_icmp: "{{ dmvpn1_icmp1_loss }}"
set_fact:
dmvpn1_host_name: "{{ dmvpn1_host_name }}"
dmvpn1_host_ipv4: "{{ dmvpn1_host_ipv4 }}"
dmvpn1_google_dns1_ip: "{{ dmvpn1_google_dns1_ip }}"
dmvpn1_google_dns1_icmp: "{{ dmvpn1_google_dns1_icmp }}"
dmvpn1_google_dns1_status: "{{ 'SUCCESS' if ('100' in dmvpn1_google_dns1_icmp) else 'FAIL' }}"
cacheable: yes
delegate_to: 127.0.0.1
delegate_facts: True
when: inventory_hostname in groups['dmvpn_primary']
templates \ html.j2:
<tr>
{% if inventory_hostname in groups['dmvpn_primary'] %}
<td>{{ location }}</td>
<td>{{ sitecode[2]|upper }}</td>
<td>{{ dmvpn1_host_name }}</td>
<td>{{ dmvpn1_host_ipv4 }}</td>
<td>ICMP</td>
<td>Google DNS1</td>
<td>{{ dmvpn1_google_dns1_icmp }}%</td>
<td>{{ dmvpn1_google_dns1_status }}</td>
<td>
{% if 'SUCCESS' in dmvpn1_google_dns1_status %}
-
{% else %}
{{ slack_url }}
{% endif %}
</td>
{% else %}
{% endif %}
</tr>