Avalon Avalon - 2 months ago 29
Linux Question

Display only certain dictionary values from yum list in ansible playbook

I created a playbook that will check to see if a package is installed on a RHEL based server using the "yum" Ansible module and then display the results.

My task is as follows;

roles/linux/yumcheck/tasks/main.yml

- name: Check for installed packages
yum: list={{ item.package }}

with_items:
- { package: 'package1' }
- { package: 'package2' }
- { package: 'package3' }

register: yumlist
- debug: var=yumlist


The playbook correctly outputs the results in the following format;

"results": [
{
"_ansible_item_result": true,
"_ansible_no_log": false,
"changed": false,
"invocation": {
"module_args": {
"conf_file": null,
"disable_gpg_check": false,
"disablerepo": null,
"enablerepo": null,
"exclude": null,
"install_repoquery": true,
"list": "python",
"name": null,
"state": "installed",
"update_cache": false,
"validate_certs": true
},
"module_name": "yum"
},
"item": {
"package": "python"
},


The yum list module will also display "available" packages. When running this playbook with a package name (such as python) that as multiple available packages, the output can become very lengthy. My goal is for the playbook to only output certain dictionary values (I believe they are referred to as dictionary values).

How can I format the results to only show pertitent information such as the "state" (installed or not) and the name of the package?

I tried displaying the information in the same manner you would display facts about a host using brackets or curly braces, for example {{ results.state}} but Ansible complained about the variable being undefined. I have not been able to find similar examples of this question online. My goal is to simply and cleanly display whether or not a list of packages are installed without all the extra bloaty information.

Thanks.

Answer

Use map filter:

- debug: msg="{{ yum_list.results | map(attribute='invocation.module_args.state') | list }}"
Comments