我使用set_fact和hostvars在剧本之间传递变量。我的代码如下所示:
- name: Staging play
hosts: localhost
gather_facts: no
vars_prompt:
- name: hostname
prompt: "Enter hostname or group"
private: no
- name: vault
prompt: "Enter vault name"
private: no
- name: input
prompt: "Enter input for role"
private: no
tasks:
- set_fact:
target_host: "{{ hostname }}"
target_vault: "{{ vault }}"
for_role: "{{ input }}"
- name: Execution play
hosts: "{{ hostvars['localhost']['target_host'] }}"
gather_facts: no
vars_files:
- "vault/{{ hostvars['localhost']['target_vault'] }}.yml"
tasks:
- include_role:
name: target_role
vars:
param: "{{ hostvars['localhost']['for_role'] }}"这一安排几个月来一直没有问题。但是,我们的环境已经改变了,现在我需要一个时间戳并将它传递给角色和其他变量,所以我做了以下更改(用注释表示):
- name: Staging play
hosts: localhost
gather_facts: yes # Changed from 'no' to 'yes'
vars_prompt:
- name: hostname
prompt: "Enter hostname or group"
private: no
- name: vault
prompt: "Enter vault name"
private: no
- name: input
prompt: "Enter input for role"
private: no
tasks:
- set_fact:
target_host: "{{ hostname }}"
target_vault: "{{ vault }}"
for_role: "{{ input }}"
current_time: "{{ ansible_date_time.iso8601 }}" # Added fact for current time
- name: Execution play
hosts: "{{ hostvars['localhost']['target_host'] }}"
gather_facts: no
vars_files:
- "vault/{{ hostvars['localhost']['target_vault'] }}.yml"
tasks:
- include_role:
name: target_role
vars:
param: "{{ hostvars['localhost']['for_role'] }}"
timestamp: "{{ hostvars['localhost']['current_time'] # Passed current_time to
Execution Play via hostvars现在,当我执行时,我得到了vault主机变量在执行播放中未定义的错误。经过一些实验之后,我发现在分期播放中设置gather_facts: yes是触发问题的原因。
但是,我需要启用gather_facts才能使用ansible_time_date。我已经通过debug验证了事实是否被正确地记录下来,并且可以由hostvars在暂存播放中调用;只是在下面的执行播放中不能调用。经过几个小时的研究,我找不到任何理由来解释为什么在分期播放中收集事实会影响执行播放的hostvars,也找不到如何修复它的任何想法。
最后,我所需要的只是传递给包含的角色的当前时间。任何能够想出在这个用例中实际工作的解决方案的人都会赢得本月最佳员工奖。如果你能用gather_facts解释最初的问题的话,你就会得到额外的分数。
谢谢!
发布于 2022-08-23 18:10:37
所以,我不得不重新发明轮子,但想出了一个更清洁的解决方案。我只是为角色本身中的时间戳创建了一个默认值,并在适当的点添加了一个日期/时间设置调用,条件是该变量没有存在存在的值。
- name: Gather date and time.
setup:
gather_subset: date_time
when: timestamp is undefined and ansible_date_time is undefined我能够将gather_facts设置为no,但我仍然不知道为什么将其设置为yes首先会破坏任何东西。如果对此有任何见解,将不胜感激。
发布于 2022-08-24 05:56:17
..。如果你能用
gather_facts解释一下最初的问题.如果对此有任何见解,将不胜感激。
这是由可变优先引起的,也是因为Ansible不为变量“覆盖或设置新值”。因此,这将取决于它们何时何地被定义。
您可以使用以下示例进行测试
---
- hosts: localhost
become: false
gather_facts: false
tasks:
- name: Show Gathered Facts
debug:
msg: "{{ hostvars['localhost'].ansible_facts }}" # will be {} only
- name: Gather date and time only
setup:
gather_subset:
- 'date_time'
- '!min'
- name: Show Gathered Facts
debug:
msg: "{{ ansible_facts }}" # from hostvars['localhost'] again和“尝试打破它”,通过添加
- name: Set Fact
set_fact:
ansible_date_time:
date: '1970-01-01'
- name: Show Facts
debug:
msg: "{{ hostvars['localhost'] }}"请注意,对于用例,您应该使用
gather_subset:
- 'date_time'
- '!min'因为你只对ansible_date_time感兴趣。见“不确定设置”的确切列表是什么?。
也要注意缓存事实,因为“当使用set_facts__的可缓存选项创建时,变量在播放中具有较高的优先级,但是当它们来自缓存时,它们与主机事实的优先级相同。”
https://stackoverflow.com/questions/73463325
复制相似问题