Bootstrap FreeKB - Ansible - List packages using the yum module
Ansible - List packages using the yum module

Updated:   |  Ansible articles

If you are not familiar with modules, check out Ansible - Getting Started with Modules.

The yum module can be used to determine if a package is available and installed on the managed node (e.g. the target system).

This is similar to the yum info command.

In this example, the yum module will be used to determine if the httpd package is available and installed.

---
- hosts: all
  gather_facts: false
  remote_user: root
  tasks:
    - name: determine if a package is available and installed
      yum:
        list: wget
      register: out

    - debug:
        var: out

 

Running this playbook should return the following. In this example, the "yumstate" is "available", which means the httpd package is available and not installed.

PLAY [all]

TASK [determine if a package is available and installed] 
ok: [server1.example.com]

TASK [debug] 
ok: [server1.example.com] => {
    "msg": {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        },
        "changed": false,
        "failed": false,
        "results": [
            {
                "arch": "x86_64",
                "envra": "0:httpd-2.4.6-90.el7.centos.x86_64",
                "epoch": "0",
                "name": "httpd",
                "release": "90.el7.centos",
                "repo": "base",
                "version": "2.4.6",
                "yumstate": "available"
            }
        ]
    }
}

PLAY RECAP
server1.example.com : ok=2    changed=0    unreachable=0    failed=0

 

On the other hand, if a package is installed, the following will be displayed. Notice there are two blocks, where the yumstate of one of the blocks is "available" and the yumstate of one of the blocks is "installed".

TASK [debug]
ok: [server1.example.com] => {
    "msg": {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        },
        "changed": false,
        "failed": false,
        "results": [
            {
                "arch": "x86_64",
                "envra": "0:wget-1.14-18.el7_6.1.x86_64",
                "epoch": "0",
                "name": "wget",
                "release": "18.el7_6.1",
                "repo": "base",
                "version": "1.14",
                "yumstate": "available"
            },
            {
                "arch": "x86_64",
                "envra": "0:wget-1.14-18.el7_6.1.x86_64",
                "epoch": "0",
                "name": "wget",
                "release": "18.el7_6.1",
                "repo": "installed",
                "version": "1.14",
                "yumstate": "installed"
            }
        ]
    }
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 9ca426 in the box below so that we can be sure you are a human.