Answers: <p><strong>Ansible approach.</strong>
  Due to the possibility of multiple versions of the same package being installed, the package dictionary value is a list. To resolve this, simply take the first element of the list.</p>
  <pre><code>  tasks:
      - name: Gather the package facts
        ansible.builtin.package_facts:
          manager: auto
  
      - name: Check whether a package is installed
        ansible.builtin.debug:
          msg: &quot;{{ ansible_facts.packages['google-chrome-stable'][0].version }}&quot;
        when: &quot;'google-chrome-stable' in ansible_facts.packages&quot;
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2023-09-06T10:04:52.617Z
Answers: 2023-09-06T10:04:52.617Z
Answers: Rufetto
Answers: <p>The <a href="https://docs.ansible.com/ansible/latest/collections/ansible/builtin/package_facts_module.html" rel="nofollow noreferrer">&quot;Ansible approach&quot;</a> would look like this:</p>
  <pre><code>- name: Check whether a package called snapd is installed
    ansible.builtin.debug:
      msg: &quot;{{ ansible_facts.packages['snapd'].version }} version of snapd is installed!&quot;
    when: &quot;'snapd' in ansible_facts.packages&quot;
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2023-06-07T12:41:29.560Z
Answers: 2023-06-07T12:41:29.560Z
Answers: YoshiMbele
Answers: <p>A simple way would be to do: </p>
  
  <pre><code>ansible -i &lt;inventory&gt; &lt;target group, lets' say some CentOS systems&gt; -m shell -a "rpm -qa | grep &lt;package name&gt;"
  </code></pre>
  
  <p>That would display a list consisting of server name and package version.</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-02-20T08:08:24.997Z
Answers: 2019-02-20T08:08:24.997Z
Answers: 13dimitar
Body: <p>i need to check more than 100 servers for the snapd package, but I don't like the output at all. I searched all day for different options without luck. Attached is the output of ansible-playbook. Any hints guys?  <a href="https://i.sstatic.net/eV6WZ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/eV6WZ.png" alt="enter image description here" /></a></p>
  <pre><code>---
   - hosts: centos
     become: true
     tasks:
      - name: Check Hostname
        command: /usr/bin/hostname
  
      - name: Check for package if is installed
        yum:
          list: snapd
        register: result
  </code></pre>
  <p>For checking the package version I tried with the following playbook but Ansible doesn't like the syntax:</p>
  <pre><code>---
   - hosts: test2
     become: true
     tasks:
      - name: Check Hostname
        command: /usr/bin/hostname
  
      - name: Check for package if is installed
        yum:
          list: snapd
        register: package_name_version
  
      - name: set package version
        set_fact:
          package_name_version: &quot;{{ package_name_version.results|selectattr('yumstate','equalto','installed')|map(attribute='version')|list|first }}&quot;
  </code></pre>
  <p>Thanks,
  Juls</p>
  
Comments: CC BY-SA 4.0
Comments: 2019-02-19T11:28:26.907Z
Comments: I simple way would be to do `ansible -i <inventory> <target group, lets' say some CentOS systems> -m shell -a "rpm -qa | grep <package name>"`; that would display a list consisting of server name and package version.
Comments: 13dimitar
Comments: CC BY-SA 4.0
Comments: 2019-02-19T17:49:56.820Z
Comments: @13dimitar you should put that ad-hoc play into an answer :-)
Comments: simbo1905
Tags: <ansible>
Title: How can I check for an installed packaged version on multiple linux hosts?
UserDisplayName: Sissi_00_20
ThreadText: # How can I check for an installed packaged version on multiple linux hosts?
  
  (Asked by: username_0 on 2019-02-18)
  
  i need to check more than 100 servers for the snapd package, but I don't like the output at all. I searched all day for different options without luck. Attached is the output of ansible-playbook. Any hints guys? [![enter image description here](https://i.sstatic.net/eV6WZ.png)](https://i.sstatic.net/eV6WZ.png)
  
  ```
  ---
   - hosts: centos
     become: true
     tasks:
      - name: Check Hostname
        command: /usr/bin/hostname
  
      - name: Check for package if is installed
        yum:
          list: snapd
        register: result
  
  ```
  
  For checking the package version I tried with the following playbook but Ansible doesn't like the syntax:
  
  ```
  ---
   - hosts: test2
     become: true
     tasks:
      - name: Check Hostname
        command: /usr/bin/hostname
  
      - name: Check for package if is installed
        yum:
          list: snapd
        register: package_name_version
  
      - name: set package version
        set_fact:
          package_name_version: "{{ package_name_version.results|selectattr('yumstate','equalto','installed')|map(attribute='version')|list|first }}"
  
  ```
  
  Thanks,
  Juls
  
  **Tags:** ansible
  
  > **Comments:**
  > **username_1**: I simple way would be to do `ansible -i <inventory> <target group, lets' say some CentOS systems> -m shell -a "rpm -qa | grep <package name>"`; that would display a list consisting of server name and package version.  
  > **username_2**: @username_1 you should put that ad-hoc play into an answer :-)  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_3 on 2023-09-06. Score: 0 | Accepted answer
  
  **Ansible approach.**
  Due to the possibility of multiple versions of the same package being installed, the package dictionary value is a list. To resolve this, simply take the first element of the list.
  
  ```
    tasks:
      - name: Gather the package facts
        ansible.builtin.package_facts:
          manager: auto
  
      - name: Check whether a package is installed
        ansible.builtin.debug:
          msg: "{{ ansible_facts.packages['google-chrome-stable'][0].version }}"
        when: "'google-chrome-stable' in ansible_facts.packages"
  
  ```
  
  
  ---
  
  ### Answer by username_4 on 2023-06-07. Score: 0
  
  The ["Ansible approach"](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/package_facts_module.html) would look like this:
  
  ```
  - name: Check whether a package called snapd is installed
    ansible.builtin.debug:
      msg: "{{ ansible_facts.packages['snapd'].version }} version of snapd is installed!"
    when: "'snapd' in ansible_facts.packages"
  
  ```
  
  
  ---
  
  ### Answer by username_1 on 2019-02-20. Score: 0
  
  A simple way would be to do:
  
  ```
  ansible -i <inventory> <target group, lets' say some CentOS systems> -m shell -a "rpm -qa | grep <package name>"
  
  ```
  
  That would display a list consisting of server name and package version.
  
  
  ---
  
  