'Ansible how to acces results dictionary

I am opening a playbook to update patches in centos, but first I need to validate that there are updated packages and in case the tasks are not stopped.

I am using the yum plugin to validate if there are updates but when trying to fail the task with a debug in case there are no updates it does not allow me to validate and always gives skyp.

Playbook.

---
- hosts: "{{ hosts}}"
  become: true
  tasks:
  - name: check updates
    yum:
      list: updates
      update_cache: true
    register: salida

  - name: show yum
    debug:
      msg: "{{ salida }}"
    when: '"yumstate" in salida.results'
...


Output

TASK [Gathering Facts] *********************************************************************************************************************
ok: []

TASK [check updates] **********************************************************************************************************************
ok: []

TASK [show yum] ************************************************************************************************************
skipping: []

Output var.

"msg": [
    {
        "arch": "x86_64",
        "envra": "0:",
        "epoch": "0",
        "name": "",
        "release": "3770.el7",
        "repo": "",
        "version": "20.0.0",
        "yumstate": "available"
    },
    {
        "arch": "x86_64",
        "envra": "0:",
        "epoch": "0",
        "name": "syslog-ng-premium-edition-compact",
        "release": "1.rhel7",
        "repo": "",
        "version": "7.0.29",
        "yumstate": "available"
    }
]

}



Solution 1:[1]

For example, you can count the number of available packages and execute a task if there are any

    - debug:
        msg: "There are {{ pkg_available }} packages available."
      vars:
        pkg_available: "{{ salida.results|
                           selectattr('yumstate', 'eq', 'available')|
                           length }}"
      when: pkg_available|int > 0

This works fine also in the --check mode.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vladimir Botka