Ansible - template file from variable and include only content of that variable

Viewed 16

For lack of a better title, I have this dummy variable, this task and this j2 template:

# variable
my_list:
  - filename: something.conf
    content: hello
  - filename: else.conf
    content: asdf
# conf.j2
{% for item in my_list %}
{{ item.content }}
{% endfor %}
# task
- name: set config
  template:
    src: conf.j2
    dest: "/tmp/{{ item.filename }}"
  loop: "{{ my_list }}"

This results in having 2 files on /tmp, something.conf and else.conf, having the same content.

Q: How can I configure Ansible so that per filename, it only 'pastes/templates' the content for that filename. Note that the items in my_list will grow.

Expected output on target vm:

# cat /tmp/something.conf
hello
# cat /tmp/else.conf
asdf

Do I have to create an if clause in the template, or edit the loop in a certain way?

0 Answers
Related