How to use wildcard with variable [duplicate]

社会主义新天地 提交于 2020-01-07 03:46:11

问题


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:


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.



来源:https://stackoverflow.com/questions/38543220/how-to-use-wildcard-with-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!