How to get just the version of the software when using ansible_facts.packages["zabbix-agent"]
For some reason (probably because of more versions of the same package might be installed), the value of the package dictionary is a list. A simple solution is to take the first element
- set_fact:
za_ver: "{{ ansible_facts.packages['zabbix-agent'][0].version }}"
when: "'zabbix-agent' in ansible_facts.packages"
To take into the account the possibility of more versions installed, use map filter
- set_fact:
za_ver: "{{ ansible_facts.packages['zabbix-agent']|
map(attribute='version')|
list }}"
when: "'zabbix-agent' in ansible_facts.packages"
Below is the equivalent with json_query filter
- set_fact:
za_ver: "{{ ansible_facts.packages['zabbix-agent']|
json_query('[].version') }}"
when: "'zabbix-agent' in ansible_facts.packages"