Does command module honor wildcards?

204 views
Skip to first unread message

Josef Fortier

unread,
Jul 25, 2017, 1:18:34 PM7/25/17
to Ansible Project
I attempted a command to rm host_keys to regenerate

- name: delete host keys
  command: rm /etc/ssh/*_key*

This issues a warning, and does not actually delete the files
I added a -f flag to rm, thinking that maybe some shell condition caused the command module to return early, but that had no effect.
Ansible gives a warning about rm, suggesting I use the file module, but that seems problematic, adding a lot of logic to a simple task.
Regardless, I'd not expect it to veto but merely suggest.

I was able to get it working with the shell module.

Some questions:

1) Why is this? I'd guess the rm/file module warning overrode in some fashion, but maybe wildcards (this seems wrong on the face of it, but I'm at a loss)

2) Is there a file module way to bulk remove on glob/wildcard that's elegant?
I'm trying fairly hard to avoid lot's of register calls, as they make the logic flow much harder to follow.

Kai Stian Olstad

unread,
Jul 25, 2017, 2:40:13 PM7/25/17
to ansible...@googlegroups.com
On 25. juli 2017 19:18, Josef Fortier wrote:
> I attempted a command to rm host_keys to regenerate
>
> - name: delete host keys
> command: rm /etc/ssh/*_key*
>
> This issues a warning, and does not actually delete the files
> I added a -f flag to rm, thinking that maybe some shell condition caused
> the command module to return early, but that had no effect.
> Ansible gives a warning about rm, suggesting I use the file module, but
> that seems problematic, adding a lot of logic to a simple task.
> Regardless, I'd not expect it to veto but merely suggest.

It is just a informational message, you can set "warn: False" on the
task and the messages won't show.
It's also possible to turn of this informational warning message in
ansible.cfg.


> I was able to get it working with the shell module.

Command module does not support globs so you would need to use shell
with globs.


> 2) Is there a file module way to bulk remove on glob/wildcard that's
> elegant?
> I'm trying fairly hard to avoid lot's of register calls, as they make the
> logic flow much harder to follow.

To do is the more Ansible way, you need two task.

First one with the find module, that support globs or regexp, register
the output in a variable, the use the file module to delete the list the
find module returned.

--
Kai Stian Olstad

Josef Fortier

unread,
Jul 26, 2017, 11:51:56 AM7/26/17
to Ansible Project, ansible-pr...@olstad.com

Command module does not support globs so you would need to use shell
with globs.

Ahh... Thanks.

FWIW, Looking at the command module docs I do see this telling note:
>> "It will not be processed through the shell"
But I'd only get the lack of glob support from that sentence implicitly; the explicit references are to job control and redirection modifiers.

To do is the more Ansible way, you need two task.
First one with the find module, that support globs or regexp

Thanks :-) I'm sure I'll end up using that regex support down the road.
Reply all
Reply to author
Forward
0 new messages