I just want to update this old discussion to point out that there is now a package module that makes this more straightforward
- name: get the rpm or apt package facts
package_facts:
manager: "auto"
- name: show apache2 version
debug: var=ansible_facts.packages.apache2[0].version
Answer from fred on Stack OverflowWhat is the best way to have ansible check if cabon black package had been installed and if not run to few steps. Install dependencies instal lcabon black and activate it. Finaly star and enable service. This for linux os
ansible - How can I check for an installed packaged version on multiple linux hosts? - DevOps Stack Exchange
Check if a package (RPM) is installed in a certain version
Access ansible facts variable?
yum - how to verify that OS packages are removed or not installed in ansible - Stack Overflow
I just want to update this old discussion to point out that there is now a package module that makes this more straightforward
- name: get the rpm or apt package facts
package_facts:
manager: "auto"
- name: show apache2 version
debug: var=ansible_facts.packages.apache2[0].version
I think more native ansible way would be:
- name: get package version
yum:
list: package_name
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 }}"
ansible_facts.packages is a dictionary. There are no keys in that dictionary named OB2* nor OB2 since no package has this exact name.
If you want to get all keys which names start with the string "OB2", one way is to filter out all others.
- Transform you dict to a key/value list with the
dict2itemsfilter - Use the
selectattrfilter and apply thematchtest to find the relevant entries. - Since the
keyname is also contained in the parameternameinside each element in thevaluelist , you can retain only thevalueusing themap(attribute='someattr')filter - Last, flatten the list to get one single list containing all packages version
Here is a playbook illustrating the concept. For the example, I used as a prefix "zlib". Just change it back to whatever suits your needs
---
- hosts: localhost
gather_facts: false
vars:
package_prefix: "zlib"
filtered_packages: "{{ ansible_facts.packages | dict2items
| selectattr('key', 'match', package_prefix)
| map(attribute='value') | flatten }}"
tasks:
- name: gather package facts
ansible.builtin.package_facts:
- name: debug the raw variable
debug:
var: filtered_packages
- name: count relevant packages
vars:
pkg_num: "{{ filtered_packages | count }}"
debug:
msg: "There are {{ pkg_num }} packages
which name starts with {{ package_prefix }}"
- name: show some info about relevant packages
debug:
msg: "Package named {{ item.name }} is in category {{ item.category }}
and has version {{ item.version }}"
loop: "{{ filtered_packages }}"
Which gives on my Ubuntu local machine:
PLAY [localhost] *****************************************************************************************************************
TASK [gather package facts] ******************************************************************************************************
ok: [localhost]
TASK [debug the raw variable] ****************************************************************************************************
ok: [localhost] => {
"filtered_packages": [
{
"arch": "amd64",
"category": "libs",
"name": "zlib1g",
"origin": "Ubuntu",
"source": "apt",
"version": "1:1.2.11.dfsg-2ubuntu1.3"
},
{
"arch": "amd64",
"category": "libdevel",
"name": "zlib1g-dev",
"origin": "Ubuntu",
"source": "apt",
"version": "1:1.2.11.dfsg-2ubuntu1.3"
}
]
}
TASK [count relevant packages] ***************************************************************************************************
ok: [localhost] => {
"msg": "There are 2 packages which name starts with zlib"
}
TASK [show some info about relevant packages] ************************************************************************************
ok: [localhost] => (item={'name': 'zlib1g', 'version': '1:1.2.11.dfsg-2ubuntu1.3', 'arch': 'amd64', 'category': 'libs', 'origin': 'Ubuntu', 'source': 'apt'}) => {
"msg": "Package named zlib1g is in category libs and has version 1:1.2.11.dfsg-2ubuntu1.3"
}
ok: [localhost] => (item={'name': 'zlib1g-dev', 'version': '1:1.2.11.dfsg-2ubuntu1.3', 'arch': 'amd64', 'category': 'libdevel', 'origin': 'Ubuntu', 'source': 'apt'}) => {
"msg": "Package named zlib1g-dev is in category libdevel and has version 1:1.2.11.dfsg-2ubuntu1.3"
}
PLAY RECAP ***********************************************************************************************************************
localhost : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
You have to filter the keys, a direct address is not possible, because you don't know the exact name.
- name: Gather the package facts
ansible.builtin.package_facts:
- name: Filter package names
set_fact:
filtered_package_names: "{{ ansible_facts.packages | list
| map('regex_search', '^vim.*') | select('string') | list }}"
- name: Print filtered packages
debug:
var: filtered_package_names
- name: Print package details from all filtered packages
debug:
msg: "{{ ansible_facts.packages[item] }}"
with_items: "{{ filtered_package_names }}"
With list a list of the keys is created, then you can filter this list with regex_search, afterwards the list is reduced to the filter result.
== Edit begin
There is a smarter filter method. Instead of using map(regex_search) / select(string), you could use directly select(match), so the filtering would look like:
- name: Filter package names
set_fact:
filtered_package_names: "{{ ansible_facts.packages | list
| select('match', '^vim.*') | list }}"
== Edit end
The result is a list of package names that match your regex.
If you need more information about one of the packages, you can then use ansible_facts.packages[_your_item] to get the rest of the information.
Example output of the above tasks:
TASK [Gather the package facts] ****************************************************************************************
ok: [localhost]
TASK [Filter package names] ********************************************************************************************
ok: [localhost]
TASK [Print filtered packages] *****************************************************************************************
ok: [localhost] => {
"filtered_package_names": [
"vim",
"vim-common",
"vim-runtime",
"vim-tiny"
]
}
TASK [Print package details] *******************************************************************************************
ok: [localhost] => (item=vim) => {
"msg": [
{
"arch": "amd64",
"category": "editors",
"name": "vim",
"origin": "Ubuntu",
"source": "apt",
"version": "2:8.1.2269-1ubuntu5.7"
}
]
}
ok: [localhost] => (item=vim-common) => {
"msg": [
{
"arch": "all",
"category": "editors",
"name": "vim-common",
"origin": "Ubuntu",
"source": "apt",
"version": "2:8.1.2269-1ubuntu5.7"
}
]
}
ok: [localhost] => (item=vim-runtime) => {
"msg": [
{
"arch": "all",
"category": "editors",
"name": "vim-runtime",
"origin": "Ubuntu",
"source": "apt",
"version": "2:8.1.2269-1ubuntu5.7"
}
]
}
ok: [localhost] => (item=vim-tiny) => {
"msg": [
{
"arch": "amd64",
"category": "editors",
"name": "vim-tiny",
"origin": "Ubuntu",
"source": "apt",
"version": "2:8.1.2269-1ubuntu5.7"
}
]
}
Are you looking to know about all dpkg commands with options? Have a read from the below link.
15 dpkg commands to Manage Debian based Linux Servers
To List all Installed Packages
Here less is a simple text reader used to scroll through the list of packages in a new buffer that opens in the existing terminal window. The list will not be mixed with other terminal commands and output. Hit q to return to the terminal prompt. See man less for more info.
dpkg -l | less
To check whether a package is installed or not:
dpkg -l {package_name}
dpkg -l vlc
To check if the package is installed or not (for example, vlc). If installed, launch the package:
dpkg -l | grep vlc
Show the location where the package is installed. The -S (capital S) stands for "search"
sudo dpkg -S {package_name}
sudo dpkg -S skype
To use Grep to search:
dpkg -l | grep {keywords}
dpkg -l | grep pdf
apt -qq list PACKAGE can also be used for checking whether the PACKAGE is installed.
If installed it'll print something like (with [installed] at the end of the line):
$ apt -qq list awscli
awscli/stable,now 1.4.2-1 all [installed]
If not installed the output will be:
$ apt -qq list awscli
awscli/stable 1.4.2-1 all