Assuming an ansible collection with the following galaxy.yml
file:
namespace: my-company
name: test
version: "1.0.0"
... is there a way from a task in my collection to find the 1.0.0
in the above? Obviously, preferably, without manually parsing galaxy.yml
myself.
In the magic variables I found the variable ansible_collection_name
which tells me the name of my collection, but not its version.
Am I missing something, or did the ansible developers just not provide that possibility?
If not for a collection, is there an option to do this for old-style roles? That works too.
You can use the lookup plugin community.general.collection_version
(ships with the community.general
collection)
install
ansible-galaxy collection install community.general
Playbook example:
- name: Demo - Get Ansible collection version via lookup
hosts: localhost
gather_facts: false
connection: local
collections:
- community.general # ensures the lookup is available
vars:
collection_fqcn: "my-company.test" # change to your collection
tasks:
- name: Read version of a specific collection
set_fact:
collection_version: >-
{{ lookup('community.general.collection_version', collection_fqcn) }}
- name: Show version
debug:
msg: "Collection {{ collection_fqcn }} version: {{ collection_version }}"
Also, when you’re inside a role within a collection, you can get the current collection’s version like this
{{ lookup('community.general.collection_version', ansible_collection_name) }}