0

Similar to: Set fact with dynamic key name in ansible

I have a custom plugin that returns a dictionary. I'd like to set the dict as multiple facts on a host but set-fact doesn't act as expected. Whats the correct way to set multiple facts from a template expression in Ansible?

This does not work.

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

This does work.

# 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
Aage Torleif
  • 1,907
  • 1
  • 20
  • 37

1 Answers1

0

have you tried to use vars ?

You can basically build a json structure like this:

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

this can be used with many ansible modules

- 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 }}"
eduprado
  • 81
  • 1
  • 4