gpt4 book ai didi

jinja2 - 检查变量是否在 Ansible 字典中的列表中

转载 作者:行者123 更新时间:2023-12-04 15:10:01 25 4
gpt4 key购买 nike

我有以下任务:

- name: Retrieve records from Route53
route53:
command: get
zone: "{{ stag_zone }}"
record: "{{ stag_record }}"
type: A
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
register: rec

- name: Print records
debug: var=rec

- name: Record contains IP
debug: msg="{{rec}} contains {{stag_ip}}"
when: "'{{stag_ip}}' in {{rec.set.values}}"
Print records任务输出如下:
ok: [XXX.XXX.XXX.XXX] => {
"var": {
"rec": {
"changed": false,
"invocation": {
"module_args": "",
"module_name": "route53"
},
"set": {
"alias": false,
"record": "YYY.XXX.ZZZ.",
"ttl": "300",
"type": "A",
"value": "AAA.AAA.AAA.AAA,BBB.BBB.BBB.BBB",
"values": [
"AAA.AAA.AAA.AAA",
"BBB.BBB.BBB.BBB"
],
"zone": "XXX.ZZZ."
}
}
}
}

而我只想在 {{stag_ip}} 时才执行“记录包含 IP”任务在 {{rec.set.values}} .但是我的 when条款肯定被打破了。它输出这个:
fatal: [XXX.XXX.XXX.XXX] => Failed to template {% if 'QQQ.QQQ.QQQ.QQQ' in <built-in method values of dict object at 0x7fb66f54e6e0> %} True {% else %} False {% endif %}: template error while templating string: unexpected '<'

我如何“转换” rec.set.values到列表?

最佳答案

这里的问题是因为 valuesdict方法。因此它比访问 key 具有“优先级”。要解决此问题,必须明确调用 get :

when: stag_ip in rec.set.get('values')

关于jinja2 - 检查变量是否在 Ansible 字典中的列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31734199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com