How to use wildcard with variable [duplicate]

2019-03-03 17:12发布

This question already has an answer here:

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

1条回答
Emotional °昔
2楼-- · 2019-03-03 17:41

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.

查看更多
登录 后发表回答