Mazzy
Mazzy

Reputation: 14189

set_fact in a role called as a loop

What I'm trying to do is to invoke a role with include_role multiple times with with_items. Something like this:

- include_role:
    name: my_role
  with_items: "{{ list }}"
  loop_control:
    loop_var: struct

then inside the role I have to save the output of a command. However since this is a loop how could I save the output without it being overwritten? I think I'm approaching wrongly to the problem

Upvotes: 0

Views: 348

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68034

An option would be to append the output to a list. For example tasks of a role

- command: date
  register: myoutput
- set_fact:
    log: "{{ log + [myoutput.stdout] }}"
- debug:
    msg: "{{ log }}"

and the play.yml

- hosts:
    - localhost
  vars:
    log: []
    list: [1,2]
  tasks:
    - include_role: name=role
      loop: "{{ list }}"
      loop_control:
        loop_var: struct

.

# ansible-playbook play.yml | grep -A 3 msg
    "msg": [
        "Sat Sep 22 19:52:38 CEST 2018"
    ]
}
--
    "msg": [
        "Sat Sep 22 19:52:38 CEST 2018", 
        "Sat Sep 22 19:52:40 CEST 2018"
    ]

Upvotes: 2

Related Questions