Output from "fail" too verbose

142 views
Skip to first unread message

Uwe Sauter

unread,
Aug 24, 2018, 3:33:45 AM8/24/18
to ansible...@googlegroups.com
Hi all,

I have a list of files where I need to check existence and fail if one doesn't exist.

I'd expect the fail module to *just* output the defined message when the condition is true
but instead the complete item is printed.

Is there a way to make the output less verbose (but keep msg)?

Thanks,

Uwe

#### playbook ####
---
- hosts: localhost
connection: local
gather_facts: false
tasks:
- set_fact:
files:
- /tmp/test1
- /tmp/test2

- stat:
path: '{{ item }}'
with_items: '{{ files }}'
register: r

- fail:
msg: '{{ item["invocation"]["module_args"]["path"] }} not found'
when: not item.stat.exists
with_items: ' {{ r.results }}'
loop_control:
label: '{{ item["invocation"]["module_args"]["path"] }}'
##################

##### output #####
PLAY [localhost] **********************************************************************************

TASK [set_fact] ***********************************************************************************
ok: [localhost]

TASK [stat] ***************************************************************************************
ok: [localhost] => (item=/tmp/test1)
ok: [localhost] => (item=/tmp/test2)

TASK [fail] ***************************************************************************************
skipping: [localhost] => (item=/tmp/test1)
failed: [localhost] (item=/tmp/test2) => {"changed": false, "item": {"changed": false, "failed": false, "invocation":
{"module_args": {"checksum_algorithm": "sha1", "follow": false, "get_attributes": true, "get_checksum": true, "get_md5": null,
"get_mime": true, "path": "/tmp/test2"}}, "item": "/tmp/test2", "stat": {"exists": false}}, "msg": "/tmp/test2 not found"}
to retry, use: --limit @/tmp/test.retry

PLAY RECAP ****************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=1
##################

Kai Stian Olstad

unread,
Aug 24, 2018, 4:17:37 AM8/24/18
to ansible...@googlegroups.com
On 24.08.2018 09:33, Uwe Sauter wrote:
> I have a list of files where I need to check existence and fail if one
> doesn't exist.
>
> I'd expect the fail module to *just* output the defined message when
> the condition is true
> but instead the complete item is printed.

That's because of the with_items loop and not fail module.


> Is there a way to make the output less verbose (but keep msg)?

You could write you own callback plugin, then you can get any format you
want, or drop loop and do it with Jinja template instead.


> ---
> - hosts: localhost
> connection: local
> gather_facts: false
> tasks:
> - set_fact:
> files:
> - /tmp/test1
> - /tmp/test2
>
> - stat:
> path: '{{ item }}'
> with_items: '{{ files }}'
> register: r
>
> - fail:
> msg: '{{ item["invocation"]["module_args"]["path"] }} not
> found'
> when: not item.stat.exists
> with_items: ' {{ r.results }}'
> loop_control:
> label: '{{ item["invocation"]["module_args"]["path"] }}'

Try this

- fail:
msg: Files not found {{ ', '.join(r.results |
rejectattr('stat.exists') | map(attribute='item') | list) }}
when: r.results | rejectattr('stat.exists') |
map(attribute='item') | list | length > 0

--
Kai Stian Olstad

Uwe Sauter

unread,
Aug 24, 2018, 6:42:33 AM8/24/18
to ansible...@googlegroups.com
Thanks, that worked. Though it feels unusual and a little like cheating Ansible.

Regards,

Uwe

Am 24.08.18 um 10:17 schrieb Kai Stian Olstad:
Reply all
Reply to author
Forward
0 new messages