0

Question: Why the "until" conditional doesn't wait until the end of a multiple commands execution so that the "results" array is built and a specific element of the results list can be evaluated in the until condition?

Example: I've got a multi-item command that I want to repeat until a certain condition is met.

#Mount /dev/mapper/image-glance --> Abort execution if cannot mount it"          
  - name: mount /dev/mapper/image-glance
    command: "{{ item }}"
    with_items:
    - sh -c "mount /dev/mapper/image-glance"
    - sh -c "sleep 40"
    - sh -c "findmnt /dev/mapper/image-glance"
    register: mount_findmnt
    retries: 3
    delay: 1
    until: '"/var/lib/glance" in mount_findmnt.results[2].stdout'

I'm interested only in the stdout of the third command

findmnt /dev/mapper/image-glance

For this reason I specified in the "until" conditional --> results[2]

mount_findmnt.results[2].stdout

Nevertheless it seems like the "until" conditional is checked as soon as the first command

mount /dev/mapper/image-glance

is executed and not at the end of the execution of all 3 commands.

For this reason the playbook fails with the following:

FAILED! => {"failed": true, "msg": "The conditional check '\"/dev/mapper/image-glance\" in mount_findmnt.results[2].stdout' failed. The error was: error while evaluating conditional (\"/dev/mapper/image-glance\" in mount_findmnt.results[2].stdout): 'dict object' has no attribute 'results'"}

as at this point results[2] wouldnt be present.

If I comment the "until" conditional and I print "mount_findmnt" I can see the list "results" and results[2] would be present as the building of list results is not stopped by the until conditional.

StefTN
  • 310
  • 3
  • 8

1 Answers1

0

A workariund to this is to write all 3 commands on the same line and check for stdout without specifying list results.

  - name: mount /dev/mapper/image-glance
    command: "{{ item }}"
    with_items:
    - sh -c "mount /dev/mapper/image-glance; sleep 4; findmnt /dev/mapper/image-glance"
    register: mount_findmnt
    retries: 3
    delay: 1
    until: '"/var/lib/glance" in mount_findmnt.stdout'

The problem with this is that sdtout and stderr of the three commands are combined.

    "mount_findmnt": {
    "changed": true, 
    "msg": "All items completed", 
    "results": [
        {
         ...
            "stderr": "mount: /dev/mapper/image-glance is already mounted or /var/lib/glance busy\n       /dev/mapper/image-glance is already mounted on /var/lib/glance", 
            "stdout": "TARGET          SOURCE                   FSTYPE OPTIONS\n/var/lib/glance /dev/mapper/image-glance xfs    rw,relatime,attr2,inode64,noquota", 

where stderr is taken from the first command and sdtout is from the last command

StefTN
  • 310
  • 3
  • 8