Check if there is a variable in the list in the dictionary in Ansible

I have the following tasks:

- 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}}" 

The Print records task outputs something like this:

 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." } } } } 

And I want to complete the "Record contains IP" task only when {{stag_ip}} is in {{rec.set.values}} . But my when article is definitely broken. He outputs this:

 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 '<' 

How can I "distinguish" rec.set.values from a list?

+6
source share
1 answer

The problem here is that values is a dict method. Thus, it has "priority" access to keys. To fix this, you must explicitly call get :

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

All Articles