create remote file only if local template exists

1,614 views
Skip to first unread message

Jordi Funollet

unread,
Nov 6, 2014, 3:08:29 PM11/6/14
to ansible...@googlegroups.com
I'm trying to have a template uploaded only if the template exists
*locally*. My use case is having a bunch of items with common parts
configured on the same role. Something like

- template: src=nginx/{{ item }}.j2 dest={{ nginx_sites_dir }}/{{
item }}
with_items: apps
when: file_exists|nginx/{{ item }}.j2

- template: src=uwsgi/{{ item }}.j2 dest={{ uwsgi_dir }}/{{ item }}
with_items: apps
when: file_exists|uwsgi/{{ item }}.j2

So for every 'app' it would configure Nginx only if a template exists on
'templates/nginx/' for this specific app, and likewise for uwsgi.

Is there some conditional that could be used here? I could probably live
with 'ignore_errors: yes' but this could mask other unintended problems.

--
Jordi Funollet Pujol
http://www.linkedin.com/in/jordifunollet

Michael Peters

unread,
Nov 6, 2014, 3:15:10 PM11/6/14
to ansible...@googlegroups.com
You can combine the stat module with local_action and save the
results. Then use those results in the conditional.
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/1415304501.3580326.187961429.610B9E15%40webmail.messagingengine.com.
> For more options, visit https://groups.google.com/d/optout.

Brian Coca

unread,
Nov 6, 2014, 3:37:37 PM11/6/14
to ansible...@googlegroups.com
or just run with ignore_errors: true, it will fail when the template source dooesn't exist


For more options, visit https://groups.google.com/d/optout.



--
Brian Coca

Michael DeHaan

unread,
Nov 7, 2014, 9:24:48 AM11/7/14
to ansible...@googlegroups.com
"or just run with ignore_errors: true, it will fail when the template source dooesn't exist"

That's not always true with action plugins.  Usually true of modules that return failed error codes.

The better way to do this would be to use the "stat" module with "local_action", check for existance, and stick a "when" on the template item.



Jordi Funollet

unread,
Nov 10, 2014, 5:12:08 AM11/10/14
to ansible...@googlegroups.com
I see how the 'stat + when' pattern would work for a single item, but
not when there's a list of templates that must be checked. This code:

- local_action: stat path=nginx/{{ item }}.j2
register: nginx_sites_exist
with_items: apps

would register a var like

{
"nginx_sites_exist": {
"changed": false,
"msg": "All items completed",
"results": [
{
"changed": false,
"invocation": {
"module_args": "path=nginx/cc-insert.j2",
"module_name": "stat"
},
"item": "cc-insert",
"stat": {
"exists": false
}
},
{
"changed": false,
"invocation": {
"module_args": "path=nginx/ftpbridge.j2",
"module_name": "stat"
},
"item": "ftpbridge",
"stat": {
"exists": false
}
}
]
}
}

How could this be passed to a 'when' statement?
Reply all
Reply to author
Forward
0 new messages