ANSIBLE How to relative dict items to task results at loop

ansibleopenvz

Need to register results from loop (task #1) and check when command.results stderr is not empty(task #2) relatively to loop items, so I can use this condition at new tasks.

The problem is that:

when condition from task #2 doesn't work cause I can't understand how to divide command.results relatively to loop items.

Is there solution to work around?

---
- name: "CHECK IF EXIST OPENVZ CONTAINER"
  hosts: "projectname-OPENVZHOSTS"
  gather_facts: yes
  remote_user: root
  vars:
    openvz_check:              "yes"
    openvz_instances:
      opwnvz_instance_1:  
        vmid:                  "119"
      opwnvz_instance_2:  
        vmid:                  "118"

  tasks:

  - name: "Ping my hosts"
    ping:


  - name: "1. Register exist containers"
    shell: echo $(vzlist -a {{ item.value.vmid }})
    register: command
    when: openvz_check == "yes"
    loop: "{{ lookup('dict', openvz_instances) }}"
    ignore_errors: yes

  - name: "2. Create variable from command"
    debug: msg="{{ command.results | map(attribute='stderr_lines') | list  }}"
    when: 
      - openvz_check == "yes" 
      - command.results | map(attribute='stderr') | list != "" 
    loop: "{{ lookup('dict', openvz_instances) }}"
    ignore_errors: yes

  - debug: msg="{{ command.results | map(attribute='stderr_lines') |list  }}"

Debug of command.results | map(attribute='stderr') | list:

ok: [projectname-OPENVZHOST-S1] => {
    "msg": [
        [], 
        []
    ]
}

Please get advice or solution as you see. Or what I need to research.

Regards

PS: have an idea to create folder and then check if it exist or not exist, but have no idea how to relation dict to results

Best Answer

You want to use the length filter.

- command.results | map(attribute='stderr') | list | length != 0

Or even better, just access stderr directly.

- command.stderr != ""

Full playbook I used is here:

---

- name: test playbook
  hosts: all
  vars:
    my_list_var: []
  tasks:
    - name: debug
      debug:
        msg: "list is empty"
      when:
        - my_list_var | list | length == 0
    - name: ls command
      command: "ls -l /tmp"
      register: result
    - name: debug 2
      debug:
        msg: "no errors"
      when: result.stderr == ""