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

**URL:** <https://forum.ansible.com/t/check-if-all-the-values-of-a-yml-matches-a-string-else-throw-error-and-exit-in-ansible/27568>\
**Category:** Ansible Project\
**Created:** [May 20, 2018, 5:48am UTC](https://forum.ansible.com/t/check-if-all-the-values-of-a-yml-matches-a-string-else-throw-error-and-exit-in-ansible/27568 "2018-05-20T05:48:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![BIJAY\_PARIDA](https://avatars.discourse-cdn.com/v4/letter/b/7feea3/32.png) [@BIJAY\_PARIDA](https://forum.ansible.com/u/BIJAY_PARIDA)\
**Post date:** [May 20, 2018, 5:48am UTC](https://forum.ansible.com/t/check-if-all-the-values-of-a-yml-matches-a-string-else-throw-error-and-exit-in-ansible/27568/1 "2018-05-20T05:48:13Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 20, 2018, 8:28am UTC](https://forum.ansible.com/t/check-if-all-the-values-of-a-yml-matches-a-string-else-throw-error-and-exit-in-ansible/27568/2 "2018-05-20T08:28:20Z")

</div>

Not pretty but it should get the job done.

&nbsp;&nbsp;&nbsp;- include\_vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file: file1.yml  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: file1

&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file1values: "{{ file1values | default() + [item.1] }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ file1 | dictsort }}"

&nbsp;&nbsp;&nbsp;- assert:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;that: file1values | unique | length == 1

---

<div class="post-metadata">

**Author:** ![BIJAY\_PARIDA](https://avatars.discourse-cdn.com/v4/letter/b/7feea3/32.png) [@BIJAY\_PARIDA](https://forum.ansible.com/u/BIJAY_PARIDA)\
**Post date:** [May 20, 2018, 9:30am UTC](https://forum.ansible.com/t/check-if-all-the-values-of-a-yml-matches-a-string-else-throw-error-and-exit-in-ansible/27568/3 "2018-05-20T09:30:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![BIJAY\_PARIDA](https://avatars.discourse-cdn.com/v4/letter/b/7feea3/32.png) [@BIJAY\_PARIDA](https://forum.ansible.com/u/BIJAY_PARIDA)\
**Post date:** [May 20, 2018, 10:12am UTC](https://forum.ansible.com/t/check-if-all-the-values-of-a-yml-matches-a-string-else-throw-error-and-exit-in-ansible/27568/4 "2018-05-20T10:12:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 20, 2018, 10:20am UTC](https://forum.ansible.com/t/check-if-all-the-values-of-a-yml-matches-a-string-else-throw-error-and-exit-in-ansible/27568/5 "2018-05-20T10:20:09Z")

</div>

You can always use shell

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

&nbsp;&nbsp;&nbsp;- assert:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;that: r.stdout == "1"

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 20, 2018, 10:29am UTC](https://forum.ansible.com/t/check-if-all-the-values-of-a-yml-matches-a-string-else-throw-error-and-exit-in-ansible/27568/6 "2018-05-20T10:29:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![BIJAY\_PARIDA](https://avatars.discourse-cdn.com/v4/letter/b/7feea3/32.png) [@BIJAY\_PARIDA](https://forum.ansible.com/u/BIJAY_PARIDA)\
**Post date:** [May 20, 2018, 10:34am UTC](https://forum.ansible.com/t/check-if-all-the-values-of-a-yml-matches-a-string-else-throw-error-and-exit-in-ansible/27568/7 "2018-05-20T10:34:23Z")

</div>

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