验证 Ansible 的集合依赖在执行开始之前已安装。

huangapple go评论63阅读模式
英文:

Verify that Ansible's Collection dependencies are installed before 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?

答案1

得分: 1

我在播放开始时添加了一个验证任务:

pre_tasks:
  - name: 验证依赖集合是否已安装
    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
英文:

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

答案2

得分: 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 ' ' ','

为了防止使用 loop 并且为了获取所有已安装的 Ansible 集合以供进一步处理,一个最小的示例剧本如下:

---
- 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') }}"

将创建一个逗号分隔值 (CSV) 列表以供进一步处理。

为了获取一个干净的列表输出:

进一步的文档:

关于您的评论:

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

是的,根据一个人尝试实现的目标,可能需要进行更多的测试和/或处理。以下是一个快速示例:

  - 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

感谢以下链接:

如何继续下一步?

过去有一个类似的问题,涉及到 What Ansible command to list or verify installed modules — whether built-in or add-on?,其中有一个很好的解决方案,可以 创建一个已安装集合和模块的字典

英文:

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.

huangapple
  • 本文由 发表于 2023年5月28日 13:20:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76350055.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定