This question already has an answer here:
-
Ansible Command module says that '|' is illegal character
2 answers
I want to list the files as per the host name.But problem is i not able to use the wildcard with variable properly.Can someone suggest me on this.
---
- hosts: local
become_user: yes
vars:
filename: /root/stuff
tasks:
- name: list files
action: command ls -lrt {{ filename }}/'*{{ansible_hostname}}'
register: listfiles
- debug: var=listfiles
If your question is why * doesn't expand?, then:
command
module:
The command module takes the command name followed by a list of space-delimited arguments. The given command will be executed on all selected nodes. It will not be processed through the shell, so variables like $HOME and operations like "<", ">", "|", and "&" will not work
shell
module:
The shell module takes the command name followed by a list of space-delimited arguments. It is almost exactly like the command module but runs the command through a shell (/bin/sh) on the remote node.
So if you need any shell tricks, like wildcard expansion or access to environment variables, use shell
module.