2

I have a task as below :

-name : Set-up 
 shell: abc
 register: "{{item }}_result"
 loop: {{ setu }}
-name: set of tasks
 include_tasks: abc.yml
 when "'{{ item }}_result'.rc!=0"
 loop: {{ setu }}
 

abc.yml

   - name: step1
     shell: {{ item.set }}
   - name: step2
     shell: {{ item.sertt }}

Here the first task is expected to run for all items of setu. So how do I capture the result of shell command for all items in a variable and loop the task file for all the items and also while checking that for item1 result.rc ! =0 and similarly for item2 result.rc!=0 and so on

1 Answer 1

2

A variable registered in a loop is a dictionary that always keeps the results list in the attribute results. See Registering variables with a loop. For example,

    - shell: echo {{ item }}_result
      register: out
      loop: [a, b, c]

    - debug:
        msg: "{{ item.stdout }}"
      loop: "{{ out.results }}"
      loop_control:
        label: "{{ item.cmd }}"

gives abridged

  msg: a_result
  msg: b_result
  msg: c_result

It is not possible to loop a block. See Playbook Keywords. Instead, you can loop either include_tasks or import_tasks. See Including and Importing. Put the tasks you want to iterate into a file. For example,

shell> cat abc.yml 
- debug:
    msg: step1 {{ item.stdout }}
- debug:
    msg: step2 {{ item.stdout }}

and iterate include_tasks

    - include_tasks: abc.yml
      loop: "{{ out.results }}"
      loop_control:
        label: "{{ item.cmd }}"

gives abridged

  msg: step1 a_result
  msg: step2 a_result
  msg: step1 b_result
  msg: step2 b_result
  msg: step1 c_result
  msg: step2 c_result

Example of a complete playbook for testing

- hosts: localhost

  tasks:

    - shell: echo {{ item }}_result
      register: out
      loop: [a, b, c]

    - debug:
        msg: "{{ item.stdout }}"
      loop: "{{ out.results }}"
      loop_control:
        label: "{{ item.cmd }}"

    - include_tasks: abc.yml
      loop: "{{ out.results }}"
      loop_control:
        label: "{{ item.cmd }}"

Q: Check that item result.rc !=0

A: Any rc != 0 makes the task fail. You have to ignore_errors. For example,

    - shell: "{{ item }}"
      register: out
      loop: ['true', 'false', 'true']
      ignore_errors: true

    - debug:
        msg: "{{ item.rc }}"
      loop: "{{ out.results }}"
      loop_control:
        label: "{{ item.cmd }}"

gives abridged

  msg: '0'
  msg: '1'
  msg: '0'

Now, you can conditionally iterate the tasks

    - include_tasks: abc.yml
      loop: "{{ out.results }}"
      when: item.rc != 0
      loop_control:
        label: "{{ item.cmd }}"

gives abridged

skipping: [localhost] => (item=true) 
skipping: [localhost] => (item=true) 
included: /export/scratch/tmp1/test-44/abc.yml for localhost => (item=false)

  msg: 'step1 '
  msg: 'step2 

Example of a complete playbook for testing

- hosts: localhost

  tasks:

    - shell: "{{ item }}"
      register: out
      loop: ['true', 'false', 'true']
      ignore_errors: true

    - debug:
        msg: "{{ item.rc }}"
      loop: "{{ out.results }}"
      loop_control:
        label: "{{ item.cmd }}"

    - include_tasks: abc.yml
      loop: "{{ out.results }}"
      when: item.rc != 0
      loop_control:
        label: "{{ item.cmd }}"
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your answer and detailed explanation. I just did not get 1 point - like in my case I want to check if my registered output for each item .rc!=0 so how can I do that? and also for loop I want to loop around my dictionary - setu
Any rc!=0 makes the task to fail. The attribute results is a list of whatever you loop.
Like I want to skip task every time if the result.rc is 0 and only proceed if it is not zero. For example - like I need to do mounting for 6 filesystems using LVM. setu which has filesystem name, volume group, physical_volume etc for 6 filesystems. So I want to run this loop of LVM steps for 6 times but that should only be executed when results.rc!=0 for each filesystem
I added an example. Fit it to your case.

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.