Sample Header Ad - 728x90

Ansible - template from dictionary item

1 vote
1 answer
1362 views
Ansible - template from dictionary item 1'st: Trying to write task which creates files from template
NODES:
  node1:
    server: host1
    script: manage1
  node2:
    server: host2
    script: manage2
  node3:
    server: host3

- name: Create files from templates
  template:
    src: "templ.j2"
    dest: "/etc/init.d/{{item.key}}"
  loop: "{{NODES|dict2items}}"
  when: "{{ 'script' in item.value }}"
Above creates files:
/etc/init.d/node1
/etc/init.d/node2
I can't find a way to get:
/etc/init.d/manage1
/etc/init.d/manage2
2'nd question: during looking for above changed dictionary into list of hashes, ( I'd rather stay with dictionaries ):
NODES:
  - node1:
    server: host1
    script: manage1
  - node2:
    server: host2
    script: manage2
for above list simple file creation works correctly:
- name: Create files
  file:
    path: "/etc/init.d/{{item.script}}"
    state: touch
  with_items: "{{ NODES }}"
but analogical file creation from template does not:
- name: Create files template
  file:
    dest: "/etc/init.d/{{item.script}}"
    src: templ.j2
  with_items: "{{ NODES }}"
I'm starting with Ansible, and is not easy to understand especially second case.
Asked by hubizx (11 rep)
Apr 26, 2020, 05:00 PM
Last activity: Jul 30, 2020, 07:22 PM