Ansible中如何提取environment_ids为空的Foreman内容视图版本号
提取Foreman内容视图中未关联环境的版本号
我需要从给定的Ansible数据集item里,提取versions列表中environment_ids为空数组的条目对应的version字段值,用来通过theforeman.foreman.content_view_version模块删除Foreman里未使用的旧内容视图版本。但我之前写的Jinja2表达式{{ (item.resources| map(attribute='version') | list ) }}无法实现需求,以下是示例数据集:
item: - auto_publish: false component_ids: - 10 - 11 environments: - activation_keys: - 1 hosts: [] id: 1 label: Library name: Library permissions: readable: true - activation_keys: [] hosts: [] id: 2 label: DEV name: DEV permissions: readable: true - activation_keys: [] hosts: [] id: 3 label: PROD name: PROD permissions: readable: true generated_for: none hosts: [] id: 4 import_only: false label: system-tester last_published: 2023-07-17 14:15:24 UTC name: comp Repo (noarch) version_count: 2 versions: - environment_ids: - 1 - 2 - 3 id: 9 published: 2023-07-17 14:03:01 UTC version: '2.0' - environment_ids: - 1 - 2 - 3 id: 12 published: 2023-07-17 14:15:24 UTC version: '2.1' - auto_publish: false component_ids: - 10 - 11 environments: - activation_keys: - 1 hosts: [] id: 1 label: Library name: Library permissions: readable: true - activation_keys: [] hosts: [] id: 2 label: DEV name: DEV permissions: readable: true - activation_keys: [] hosts: [] id: 3 label: PROD name: PROD permissions: readable: true generated_for: none hosts: [] id: 4 import_only: false label: system-tester2 last_published: 2023-07-17 14:15:24 UTC name: comp Repo (noarch) version_count: 2 versions: - environment_ids: [] id: 9 published: 2023-07-17 14:03:01 UTC version: '2.0' - environment_ids: - 1 - 2 - 3 id: 12 published: 2023-07-17 14:15:24 UTC version: '2.1' - activation_keys: - id: 3 name: lx auto_publish: false component_ids: - 10 - 11 environments: - activation_keys: - 1 hosts: [] id: 1 label: Library name: Library permissions: readable: true - activation_keys: [] hosts: [] id: 2 label: DEV name: DEV permissions: readable: true - activation_keys: [] hosts: [] id: 3 label: PROD name: PROD permissions: readable: true generated_for: none hosts: [] id: 4 import_only: false label: system-tester3 last_published: 2023-07-17 14:15:24 UTC name: comp Repo (noarch) version_count: 2 versions: - environment_ids: [] id: 9 published: 2023-07-17 14:03:01 UTC version: '2.0' - environment_ids: - 1 - 2 - 3 id: 12 published: 2023-07-17 14:15:24 UTC version: '2.1'
解决方案
单条目处理表达式
如果只处理单个内容视图条目(即item是列表中的单个元素),使用以下Jinja2表达式:
{{ item.versions | selectattr('environment_ids', 'equalto', []) | map(attribute='version') | list }}
批量处理逻辑
如果要遍历整个item列表处理所有内容视图,用循环包裹筛选逻辑:
{% for cv in item %} {% set unused_versions = cv.versions | selectattr('environment_ids', 'equalto', []) | map(attribute='version') | list %} # 此处调用theforeman.foreman.content_view_version模块,传入unused_versions执行删除 {% endfor %}
表达式说明
selectattr('environment_ids', 'equalto', []):精准筛选出environment_ids为空数组的版本条目map(attribute='version'):从筛选后的条目中提取version字段值list:将结果转换为列表格式,适配Ansible模块的参数要求
示例验证
基于提供的数据集,运行结果如下:
system-tester条目:无符合条件的版本,结果为[]system-tester2/system-tester3条目:结果为['2.0']
内容的提问来源于stack exchange,提问作者Eric
相关产品推荐
相关产品推荐

