Avalon
Avalon

Reputation: 1070

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.

Upvotes: 2

Views: 2062

Answers (2)

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68299

Use map filter:

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

Upvotes: 2

Stephen Granger
Stephen Granger

Reputation: 76

You may be able to use the map filter to extract the variables you are after, http://docs.ansible.com/ansible/playbooks_filters.html#extracting-values-from-containers

There is an example of it being used here, http://docs.ansible.com/ansible/ec2_vpc_subnet_facts_module.html

yum_list.results[0].invocation.module_args.state

should give you the value for state.

Upvotes: 2

Related Questions