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

14 views
Skip to first unread message

BIJAY PARIDA

unread,
May 20, 2018, 1:48:13 AM5/20/18
to Ansible Project
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)
=======================

"item1" : "value"
"item2" : "value"
"item3" : "value"


file2.yml (value2 field is same)
=======================

"item1" : "value2"
"item2" : "value2"
"item3" : "value2"

Both value1 and value2 are different and I want to check the respective files and their values.

And assert the condition file1.values | uniqe == value1 

Thanks
Bijay

Kai Stian Olstad

unread,
May 20, 2018, 4:28:31 AM5/20/18
to ansible...@googlegroups.com
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

--
Kai Stian Olstad

BIJAY PARIDA

unread,
May 20, 2018, 5:30:51 AM5/20/18
to Ansible Project
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.

--
Bijay

BIJAY PARIDA

unread,
May 20, 2018, 6:12:23 AM5/20/18
to Ansible Project
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.


--
Bijay

Kai Stian Olstad

unread,
May 20, 2018, 6:20:25 AM5/20/18
to ansible...@googlegroups.com
On 20.05.2018 11:30, BIJAY PARIDA wrote:
> 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.

You can always use shell

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

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

--
Kai Stian Olstad

Kai Stian Olstad

unread,
May 20, 2018, 6:29:26 AM5/20/18
to ansible...@googlegroups.com
On 20.05.2018 12:12, BIJAY PARIDA wrote:
> 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.

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.


--
Kai Stian Olstad

BIJAY PARIDA

unread,
May 20, 2018, 6:34:23 AM5/20/18
to Ansible Project
Thanks a lot for the help, I will take it forward from here to add some more conditional checks.

--
Bijay
Reply all
Reply to author
Forward
0 new messages