Nik
Nik

Reputation: 201

Ansible 'how to traverse over a list` to create a new list

I am trying to create a simple playbook task traversing over multiple dict items(v1) and create a new list var (list_var) with specific item in it, but it is not working list_var is only showing me one element, can anyone please suggest what am i missing? if i do this:

set_fact:
  list_var: "{{ v1.stdout  }}"
with_items: "{{ v1.items }}"

Values in v1 are returned by simple shell output and have values from different hosts like hostname

Upvotes: 0

Views: 172

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68034

Probably something list this?

    - command: echo "{{ item }}"
      register: v1
      with_sequence: start=0 end=3
    - set_fact:
        list_var: "{{ list_var|default([]) }} + [ {{ item }} ]"
      loop: "{{ v1.results | json_query('[].stdout') }}"
    - debug: var=list_var

Which produces this list

    TASK [debug] **********
    ok: [localhost] => {
        "list_var": [
            0, 
            1, 
            2, 
            3
        ]
    }

Upvotes: 1

Related Questions