2

would you please help me with this problem: I have a playbook with multiple tasks and each tasks contains loop and register for the output of the task. Last task is going to use lineinfile to create a csv report based on the previous registers. something like below:

- name: information
  module:
     xxxx: xxxx
     xxxx: xxxxx
  loop:
  -  xxxx
  -  xxxx
  register: task1_info
- name: information
  module:
     xxxx: xxxx
     xxxx: xxxxx
  loop:
  -  xxxx
  -  xxxx
  register: task2_info
- name: information
  lineinfile:
     path: xxxx
     line: "{{ item.tags.Name }}, {{ item.lastName }}"
  loop:
  -  task1_info.results
  -  task2_info.results

if i use only one register at the end it is working, but not loop through all registers. the other option is to write a task after each register which I don't think reasonable!!

1 Answer 1

2

I understand your use case that you like to append one list to an other or to merge two lists.

To do so you could use an approach like

---
- hosts: localhost
  become: false
  gather_facts: false

  vars:

    LIST_1:
      - 1
      - 2
      - 3

    LIST_2:
      - A
      - B
      - C

  tasks:

  - name: Info
    debug:
      msg: "{{ item }}"
    loop: "{{ LIST_1 + LIST_2 }}"
    loop_control:
      extended: true
      label: "{{ansible_loop.index0 }}"

resulting into an output of

TASK [Info] ******************
ok: [localhost] => (item=0) =>
  msg: 1
ok: [localhost] => (item=1) =>
  msg: 2
ok: [localhost] => (item=2) =>
  msg: 3
ok: [localhost] => (item=3) =>
  msg: A
ok: [localhost] => (item=4) =>
  msg: B
ok: [localhost] => (item=5) =>
  msg: C

Credits to

Further Q&A

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.