[英]Jinja2 templating - Filter specific items - Ansible
我想知道如何将 ansible 中的两个列表与 jinja2 模板进行比较,然后得到列表之间的差异。
假设我们有两个列表:customers 和 exclude_customers
customers:
- name: A
git: true
version: 7.2.8
- name: B
git: true
- name: components
git: true
- name: Editor
git: true
exclude_customers:
- name: components
- name: Editor
在此示例中,我希望得到值为 A 和 B 的结果。
{% for customer in customers| difference(['exclude_customers']) | map (attribute='name') %}
{{ customer.name }}
{% endfor %}
但我得到了这个错误:
AnsibleUndefinedVariable: 'ansible.parsing.yaml.objects.AnsibleUnicode object' has no attribute 'name'
谢谢
如果我正确理解目标,这应该有效,
---
- hosts: localhost
gather_facts: False
vars:
customers:
- name: A
git: true
version: 7.2.8
- name: B
git: true
- name: components
git: true
- name: Editor
git: true
exclude_customers:
- name: components
- name: Editor
git: true
tasks:
- debug:
msg: "{{ customers |map (attribute='name')|list |difference(exclude_customers|map (attribute='name')|list) }}"
将返回:
PLAY [localhost] *************************************************************************************************************************************************************************************************************************
TASK [debug] *****************************************************************************************************************************************************************************************************************************
ok: [localhost] =>
msg:
- A
- B
PLAY RECAP *******************************************************************************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.