1

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

---
 - 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

2
  • 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.
    – 13dimitar
    Commented Feb 19, 2019 at 11:28
  • 1
    @13dimitar you should put that ad-hoc play into an answer :-)
    – simbo1905
    Commented Feb 19, 2019 at 17:49

3 Answers 3

0

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"
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.

0

The "Ansible approach" 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"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.