英文:
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) 列表以供进一步处理。
为了获取一个干净的列表输出:
- 根据 Is it possible to remove error messages from Bash? 删除 Python 警告消息
- 根据 Remove all lines before a match with
sed
删除ansible-galaxy
的标题 - 根据 How do I trim leading and trailing whitespace from each line of some output? 删除输出中的尾随空白
- 根据 Replace spaces ... 用逗号替换空白
进一步的文档:
关于您的评论:
> ... 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
- Drop Python warning messages according Is it possible to remove error messages from Bash?
- Drop
ansible-galaxy
header from output according Remove all lines before a match withsed
- Remove trailing whitespaces from output according How do I trim leading and trailing whitespace from each line of some output?
- Replace whitespaces with comma according Replace spaces ...
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论