1

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.

1
  • Update the question with the template templ.j2. Commented Apr 26, 2020 at 20:56

1 Answer 1

0

Q1: "I can't find a way to get"

/etc/init.d/manage1
/etc/init.d/manage2

A:

Use item.value.script

- name: Create files from templates
  template:
    src: "templ.j2"
    dest: "/etc/init.d/{{ item.value.script }}"
  loop: "{{ NODES|dict2items }}"
  when: "{{ 'script' in item.value }}"
1
  • Indeed, it works, seems I tried 20 others options but not suggested by you. Thanks
    – hubizx
    Commented Apr 27, 2020 at 11:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .