1

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?

U880D
  • 8,601
  • 6
  • 24
  • 40
lonix
  • 14,255
  • 23
  • 85
  • 176

2 Answers2

1

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
lonix
  • 14,255
  • 23
  • 85
  • 176
1

Unfortunately the command ansible-galaxy has no parameter to get clean output or to format it. So one need to do post-processing on the output.

 ansible-galaxy collection list 2>/dev/null | sed '0,/^-.*$/d;s/[ \t]*$//' | tr -s ' ' ','

To prevent using loop and in order to get all installed Ansible Collections for further processing, a minimal example playbook

---
- hosts: localhost
  become: false
  gather_facts: false

  tasks:

  - name: Generate Collection Facts
    shell:
      cmd: >-
        echo "Collection,Version";
        ansible-galaxy collection list 2>/dev/null
        | sed '0,/^-.*$/d'
        | sed 's/[ \t]*$//'
        | tr -s ' ' ','
    register: collection_facts

  - name: Show Collection Facts
    debug:
      msg: "{{ collection_facts.stdout | community.general.from_csv(dialect='unix') }}"

will create a list of Comma Separated Values (CSV) for further processing.

To get a clean list output

Further Documentation


Regarding your comment

... it reports "what's installed". But if I want to know "what's missing" I need to perform more processing on that CSV output?

Right, depending on what one try to achieve more tests can and/or need to be done. A quick example

  - name: Register Collection Facts
    set_fact:
      collection_facts: "{{ collection_facts.stdout | community.general.from_csv(dialect='unix') }}"

  - name: Show Collection Facts
    debug:
      msg: "{{ collection_facts }}"

  - name: Show if 'community.general' is installed
    debug:
      msg: "Is installed"
    when: collection_facts | selectattr(search_key,'equalto',search_val) | list | count > 0
    vars:
      search_key: Collection
      search_val: community.general

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.

U880D
  • 8,601
  • 6
  • 24
  • 40
  • 1
    Thanks. This is very nice - it reports "what's installed". But if I want to know "what's missing" I need to perform more processing on that csv output? – lonix May 28 '23 at 09:32
  • Right, that's the case but would be very simple like. I am going to provide an example. ... just one moment. – U880D May 28 '23 at 09:34
  • Reading this again, it looks like a standalone play, just for verification. That is actually potentially quite useful. I can imagine putting all kinds of pre-flight tests / verifications in there. – lonix May 28 '23 at 09:35
  • Right, it is currently a standalone play just to provide a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example), for learning purpose, for debugging to get familiar with the data structures. One need to adopt the approach for further use in his own case. – U880D May 28 '23 at 10:16
  • 1
    My answer below was what I needed... just a quick and effective fail-safe. But I'm marking your answer anyway because it's so comprehensive. Thanks for putting so much effort into it. – lonix May 28 '23 at 10:19