【发布时间】:2020-11-13 21:51:53
【问题描述】:
我一直在尝试创建一个将比较来自不同主机的文件计数的剧本。
- shell: << code to count number of lines >>
register: record_count
- debug:
msg: "The count is {{ record_count.stdout }}"
- hosts: localhost
tasks:
- name: Fail if the count doesnt match
fail:
msg: "The record count is not matching on both the export files."
when: "{{ hostvars[groups.apa[0].record_count.stdout }} != {{ hostvars[groups.apa[1].record_count.stdout }}"
但由于我们的库存非常动态,我们似乎无法使用主机组。我也尝试过将库存主机名保存在变量上,但据我所知,它保存在托管主机上,而不是保存在我将进行比较的本地主机上,我无法使主机变量工作
- name: get current record count in from Latest ldapexport file
shell: << count code here >>
register: record_count
- name: save inventory hostname to variable 1
set_fact:
server1: "{{ inventory_hostname }}"
when: server1 is not defined
run_once: true
- debug:
msg: value of Server 1 is {{ server1 }}
- name: save inventory hostname to variable 2
set_fact:
server2: "{{ inventory_hostname }}"
when: server1 is defined and server2 is not defined
- debug:
msg: value of Server 2 is {{ server2 }}
- name: compare
debug:
msg: they are equal
when: "{{ hostvars[vars[server1]].record_count.stdout }} == {{ hostvars[vars[server2]].record_count.stdout }}"
delegate_to: localhost
run_once: true
任何想法都将不胜感激。谢谢!
【问题讨论】:
-
缺少右括号
hostvars[groups.apa[0]。
标签: dynamic ansible ansible-inventory