Does command module honor wildcards?

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.

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.

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 :slight_smile: I’m sure I’ll end up using that regex support down the road.