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.
You can combine the stat module with local_action and save the
results. Then use those results in the conditional.
Brian_Coca
(Brian Coca)
November 6, 2014, 8:37pm
3
or just run with ignore_errors: true, it will fail when the template source dooesn’t exist
“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.
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?