# Check if a package (RPM) is installed in a certain version

**URL:** <https://forum.ansible.com/t/check-if-a-package-rpm-is-installed-in-a-certain-version/40371>\
**Category:** Get Help\
**Created:** [February 19, 2025, 4:20pm UTC](https://forum.ansible.com/t/check-if-a-package-rpm-is-installed-in-a-certain-version/40371 "2025-02-19T16:20:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![microlinux](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/microlinux/32/2968_2.png) [@microlinux](https://forum.ansible.com/u/microlinux)\
**Post date:** [February 19, 2025, 4:20pm UTC](https://forum.ansible.com/t/check-if-a-package-rpm-is-installed-in-a-certain-version/40371/1 "2025-02-19T16:20:55Z")

</div>

Hi,

I’m currently writing a playbook for a workstation running Rocky Linux, and I can’t figure out one detail.

I want to add a line to a file (`yum.conf`) only when the `vagrant` package is installed in version 2.4.1. I know I have to use the `package_facts` module to do this. I’ve spent a couple hours banging my head on the keyboard trying out various syntaxes, but nothing works. So here’s in plain words what I have to do:

**If** the vagrant package is installed **and** its version is 2.4.1 **then** add the line `exclude=vagrant` to the `/etc/yum.conf` file.

Any suggestions ?

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [February 19, 2025, 4:31pm UTC](https://forum.ansible.com/t/check-if-a-package-rpm-is-installed-in-a-certain-version/40371/2 "2025-02-19T16:31:46Z")

</div>

I could suggest how to do this for Debian / Ubuntu… what does the `package_facts` output look like on a RedHat based system?

---

<div class="post-metadata">

**Author:** ![hugonz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hugonz/32/503_2.png) [@hugonz](https://forum.ansible.com/u/hugonz)\
**Post date:** [February 19, 2025, 7:23pm UTC](https://forum.ansible.com/t/check-if-a-package-rpm-is-installed-in-a-certain-version/40371/3 "2025-02-19T19:23:08Z")

</div>

This is the output of package\_facts:

```auto
localhost | SUCCESS => {
    "ansible_facts": {
        "packages": {
            "aardvark-dns": [
                {
                    "arch": "x86_64",
                    "epoch": 2,
                    "name": "aardvark-dns",
                    "release": "1.el9_5",
                    "source": "rpm",
                    "version": "1.12.2"
                }
            ],
            "acl": [
                {
                    "arch": "x86_64",
                    "epoch": null,
                    "name": "acl",
                    "release": "4.el9",
                    "source": "rpm",

```

So I’ m guessing you just have to do:

```auto
- name: Insert line in yum.conf
  ansible.builtin.lineinfile:
    line: exclude=vagrant
    path: /etc/yum.conf
  when: ansible_facts.packages.vagrant.release == '2.4.1' 

type or paste code here

```

---

<div class="post-metadata">

**Author:** ![microlinux](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/microlinux/32/2968_2.png) [@microlinux](https://forum.ansible.com/u/microlinux)\
**Post date:** [February 19, 2025, 7:50pm UTC](https://forum.ansible.com/t/check-if-a-package-rpm-is-installed-in-a-certain-version/40371/4 "2025-02-19T19:50:21Z")

</div>

Here’s the code I used for testing:

```auto
- name: Gather package facts
  ansible.builtin.package_facts:
    manager: rpm

- name: Print Vagrant package facts
  ansible.builtin.debug:
    var: ansible_facts.packages['vagrant']

```

And here’s the output:

```auto
TASK [apps_vagrant : Print Vagrant package facts] ******************************
ok: [localhost] => {
    "ansible_facts.packages['vagrant']": [
        {
            "arch": "x86_64",
            "epoch": 0,
            "name": "vagrant",
            "release": "1",
            "source": "rpm",
            "version": "2.4.1"
        }
    ]
}

```

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [February 20, 2025, 11:31am UTC](https://forum.ansible.com/t/check-if-a-package-rpm-is-installed-in-a-certain-version/40371/5 "2025-02-20T11:31:45Z")

</div>

@hugonz 's answer is good, just two more thoughts, older versions of Ansible would error if `ansible_facts.packages.vagrant.release` was not defined and if you wanted to check for `vagrant` version `2.4.1` or above you could also add in use of the [ansible.builtin.version test](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/version_test.html):

```yaml
- name: Insert line in yum.conf
  ansible.builtin.lineinfile:
    line: exclude=vagrant
    path: /etc/yum.conf
  when:
    - ansible_facts.packages.vagrant.release is defined
    - ansible_facts.packages.vagrant.release is ansible.builtin.version('2.4.1', '>=')"

```
