0

Given the variable structure

syncjobs:
  - filename: output1.bash
    content: data1
  - filename: output2.bash
    content: data2

I want to loop over it creating the files with ansible.builtin.template using the field filename for the dest. I can achieve this with

- name: Create scripts
  ansible.builtin.template:
    src: template.bash.j2
    dest: /opt/bin/{{ item.filename }}
    owner: root
    mode: "u+x,o-r"
    backup: true
  with_items: "{{ syncjobs }}"

however I can't figure out how to address the specific content field from the syncjobs dict in the template file. Of course I can access the variable syncjobs, but I need to address the content field from the related dictionary for every specific iteration.

I tried to set an additional fact like current_filename to item.filename inside an ansible.builtin.block, but unfortunately loops does not seem to be supported for blocks.

1 Answers1

1

Exactly the same way as in the playbook. There is the same {{ item }} variable in the template, so you can refer to {{ item.content }} in the template file. (Actually, all the playbook variables are available in the template, just the same set that exist at that specific place of the playbook where the ansible.builtin.template was called.)

Nikita Kipriyanov
  • 8,033
  • 1
  • 21
  • 39