如何对变量使用通配符

How to use wildcard with variable

我想根据主机列出文件 name.But 问题是我无法使用带变量的通配符 properly.Can 有人建议我这样做。

    ---
    - 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

如果您的问题是为什么 * 不展开?,那么:

command 模块:

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 模块:

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.

因此,如果您需要任何 shell 技巧,例如通配符扩展或访问环境变量,请使用 shell 模块。