I use various Ansible Collections. When the Controller Node does not have them installed (e.g. because I installed ansible-core instead of ansible) then the play breaks halfway.
Is there a way to verify such dependencies before the play begins?
I use various Ansible Collections. When the Controller Node does not have them installed (e.g. because I installed ansible-core instead of ansible) then the play breaks halfway.
Is there a way to verify such dependencies before the play begins?
On
I added a verification task at the beginning of the play:
pre_tasks:
- name: verify dependent collections are installed
delegate_to: localhost
ansible.builtin.shell: ansible-galaxy collection list | grep "{{item}}" | wc -l
register: result
failed_when: result.stdout != "1"
changed_when: false
loop:
- community.general
- amazon.aws
- kubernetes.core
Unfortunately the command
ansible-galaxyhas no parameter to get clean output or to format it. So one need to do post-processing on the output.To prevent using
loopand in order to get all installed Ansible Collections for further processing, a minimal example playbookwill create a list of Comma Separated Values (CSV) for further processing.
To get a clean list output
ansible-galaxyheader from output according Remove all lines before a match withsedFurther Documentation
community.general/plugins/filter /from_csv.pyRegarding your comment
Right, depending on what one try to achieve more tests can and/or need to be done. A quick example
Thanks To
How to proceed further?
There was a similar question in the past about What Ansible command to list or verify installed modules — whether built-in or add-on? which has a good solution to create a dictionary of installed collections and modules.