The stat module does not work with wildcards, so the first task will not do what you expect. Most Ansible modules do not support *
, ?
, etc. wildcards in their parameters unless explicitly documented that they do. The reason for this is that wildcard expansion is typically handled by your login shell (bash, zsh, etc), so unless the application explicitly supports it then it won't recognize them.
Here's an easy way to verify this:
tasks:
- stat: path=/etc/*.conf
register: foo
- debug: var=foo
- stat: path=/etc/resolv.conf
register: bar
- debug: var=bar
The output of this is:
ok: [localhost] => {
"var": {
"foo": {
"changed": false,
"invocation": {
"module_args": "path=/etc/*.conf",
"module_complex_args": {},
"module_name": "stat"
},
"stat": {
"exists": false
}
}
}
}
TASK: [stat path=/etc/resolv.conf] ********************************************
ok: [localhost]
TASK: [debug var=bar] *********************************************************
ok: [localhost] => {
"var": {
"bar": {
"changed": false,
"invocation": {
"module_args": "path=/etc/resolv.conf",
"module_complex_args": {},
"module_name": "stat"
},
"stat": {
"atime": 1446665095.0724516,
"checksum": "fd75f8cc67e4879fa546cbbd901b211bcb7e1b5e",
"ctime": 1446840004.4182615,
"dev": 51713,
"exists": true,
"gid": 0,
"gr_name": "root",
"inode": 146096,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"md5": "a56ca5f7379429d3b358ce922b28039b",
"mode": "0644",
"mtime": 1446840004.4182615,
"nlink": 1,
"path": "/etc/resolv.conf",
"pw_name": "root",
"rgrp": true,
"roth": true,
"rusr": true,
"size": 94,
"uid": 0,
"wgrp": false,
"woth": false,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
}
}
}
}
Note that when you specify a single file it returns the results for that file, but when you specify a wildcard it basically returns nothing.
As @udondan implied in his answer, you can just do something like this:
- shell: rm -rf /opt/app/jboss/configuration/*.web
Since rm
will silently complete without error if there are 0 matches.
If you really want to get fancy you could use the find module to locate all the files that match your pattern and then invoke the rm
command (or better yet use the file module and set state=absent
) using a with_items loop to loop over what find
returns.
mv
,cp
andrm
*.web
only if files exists. I have validatedmv and cp *web
and it was failed and aborting with no such file or directory...Just noticedrm
is silently completing without error.