Ansible check if key/value pair exists in list of dictionaries

Viewed 9428

I am trying to check if a certain key/value pair exists in a list of dictionaries in Ansible.

I found this question, however I am not sure if the syntax differs from python to ansible (I have never seen an if statement in ansible!) Check if value already exists within list of dictionaries?

I have already tried the when condition:

  when: '"value" not in list'

however I have not had any luck with that.

For example, list looks something like:

list: [
   {
   "key1" : "value1",
   "key2" : "value2",
   "key3" : "value3"
   },
   {
   "key1" : "value4",
   "key2" : "value5",
   "key3" : "value6"
   },
   and so on

And I am trying to find out, for example, whether the pair "key2":"value5" exists within any of the dictionaries in the list. Hopefully there is a way to do this that just gives true if the pair exists, false if not?

Any tips would be greatly appreciated! Thanks.

1 Answers
Related