ansible-for-devops icon indicating copy to clipboard operation
ansible-for-devops copied to clipboard

block doesn't like loops/with_

Open TedMil opened this issue 3 years ago • 1 comments

After reading on page 120: "If you want to perform a series of tasks with one set of task parameters (e.g. with_- items, when, or become) applied, blocks are quite handy.", I tried this code:

   - block: 
      - name: create Logical Volume
        community.general.lvol:
          vg: "{{ vgscan}}"
          lv: "{{ item.key }}"
          size: "{{ item.value }}"
      - name: format the Logical Volume
        community.general.filesystem:
          dev: "/dev/mapper/{{ vgscan }}-{{ item.key }} "
          fstype: xfs
          opt: "-L {{ item.key }}"
    when: "{{ item.key }} not in {{ lvscan.stdout }}"
    #loop: "{{ gluster_vol_size|dict2items }}"
    with_dictionary: "{{ gluster_vol_size }}"

and got back: ERROR! 'loop' is not a valid attribute for a Block

Digging in the documentation I found:

"All tasks in a block inherit directives applied at the block level. Most of what you can apply to a single task (with the exception of loops) can be applied at the block level, so blocks make it much easier to set data or directives common to the tasks."

Now I have to figure out how to do my loop.

Ted Miller

TedMil avatar Apr 17 '22 21:04 TedMil

I don't mention in that section that loops are supported (and the example only shows examples of when applied to multiple tasks)—are you suggesting the section might need extra clarification?

Regarding this feature, see: https://github.com/ansible/ansible/issues/13262

geerlingguy avatar Apr 18 '22 14:04 geerlingguy