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

时间:2015-07-30 21:16:26

标签: jinja2 ansible

我有以下任务:

"Date"

- 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 '<' ”投射到列表中?

1 个答案:

答案 0 :(得分:5)

此处的问题是因为valuesdict方法。所以它有&#34;优先&#34;过度访问密钥。要解决此问题,必须明确调用get

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