Check if all the values of a yml matches a string else throw error and exit in ansible

Hi,

I have two files file1.yml and file2.yml which I am passing from the deployment level.

I want to verify that all the values of the file1 matches to a string else exits with error message, same with the 2nd file.

file1.yml (value field is same)

Not pretty but it should get the job done.

   - include_vars:
       file: file1.yml
       name: file1

   - set_fact:
       file1values: "{{ file1values | default() + [item.1] }}"
     with_items:
       - "{{ file1 | dictsort }}"

   - assert:
         that: file1values | unique | length == 1

Thanks a lot Kai for the response. Here I don’t want to include the vars, just want to check if the file exists and it contains the string.

Is there any alternate? I meant without using the include_vars option.

The length comparison works well.

But when comparing the string it fails.

  • assert:

that: “file1values | unique | length == 1”

that: file1values.get(‘values’) | unique == ‘drop’ - Tried with get values.

You can always use shell

   - shell: cut -f2 -d":" /path/to/file1.yml | uniq | wc -l
     register: r

   - assert:
       that: r.stdout == "1"

file1values is a list so the get method wont work.
unique return a list of unique values of a list, ["a", "a", "b"] becomes ["a", "b"] and ["a", "a", "a"] becomes ["a"] so you can't compare that to a string.

Thanks a lot for the help, I will take it forward from here to add some more conditional checks.