-1

I am attempting to copy multiple template files to my unix machine. Issue is, I have multiple systems, so the templates are variablized based off the group_var file, app.yml. User parses the system_name to the ansible playbook which then gathers the details from app.yml. The templates then pick those variables. Then the templates should be copied to the destination. However, with my current code, only 1 of the two templates are copied over.

app.yml:

system1:
    src: 'template/file1.j2'
    dst: '/app/user/file1'
    src: 'template/file2.j2'
    dst: '/app/user/file2'

system2:
    src: 'template/file1.j2'
    dst: '/app/user/file1'
    src: 'template/file2.j2'
    dst: '/app/user/file2'

main.yml

- name: copy mutiple templates to destination with dictionary
  template: src={{ item.src }} dst={{ item.dst }}
  when: "hostgroup='user_input'"
  with_items: '{{ system1 }}'

With the above, only 1 file is copied and not both. Please can someone point me in the right direction? I have used the following also as reference; https://riptutorial.com/ansible/example/22079/with-items---predefined-dictionary

test1 1
  • 47
  • 7

1 Answers1

1

Your mistake is that you defined them as a dictionary, so YAML will understand it as an array.
You should format them as a list.
The example you mentioned has the right format.
I kept system2 as it is for debugging purposes:

system1:
  - src: 'template/file1.j2'
    dst: '/app/user/file1'
  - src: 'template/file2.j2'
    dst: '/app/user/file2'

system2:
    src: 'template/file1.j2'
    dst: '/app/user/file1'
    src: 'template/file2.j2'
    dst: '/app/user/file2'

Here is a debug task with type_debug which can recognise the variable type:

  - debug:
      msg: "system1: {{ system1 | type_debug }} / system2: {{ system2 | type_debug }}"

Result:

TASK [debug] *******************************************************************************
ok: [localhost] => {
    "msg": "system1: list / system2: dict"
}

An item of a list is introduced by a dash - in YAML.

Khaled
  • 775
  • 1
  • 5
  • 19