如何根据dict设置多个事实



类似于:在ansible 中使用动态密钥名称设置事实

我有一个返回字典的自定义插件。我想将dict设置为主机上的多个事实,但设置的事实并不像预期的那样起作用。在Ansible中,从模板表达式设置多个事实的正确方法是什么?

这不起作用。

# Should set "name", "uuid"
- set_fact: "{{ plugin_returns_a_dict(inventory_hostname) }}"
# Evaluates to false
- assert:
that: name is defined

这确实有效。

# Should set "name", "uuid"
- with_dict: "{{ plugin_returns_a_dict(inventory_hostname) }}" 
set_fact: { "{{ item.key }}": "{{ item.value }}" }
# Evaluates to true
- assert:
that: name is defined

您尝试过使用vars吗?

您基本上可以构建这样的json结构:

vars:
user:
name: "{{ first }}.{{ second }},"
date: "{{ day }}.{{ month }}"

这可以与许多易失模块一起使用

- name: add line to a the end of the file
lineinfile:
dest: "{{ destination }}/{{ file }}.json"
line: "{{ user|to_json }}"
insertafter: EOF
vars:
user:
name: "{{ first }}.{{ second }},"
date: "{{ day }}.{{ month }}"

最新更新