如何根据字典设置多个事实?

时间:2020-03-30 16:44:24

标签: ansible ansible-facts

类似于:Set fact with dynamic key name in 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

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用vars

您基本上可以构建如下的json结构:

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

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

- 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 }}"