如何使用带变量的通配符 [英] How to use wildcard with variable

查看:227
本文介绍了如何使用带变量的通配符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按主机名列出文件.但是问题是我不能正确使用带变量的通配符.有人可以建议我这样做吗.

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

命令模块采用命令名称,后跟由空格分隔的参数列表.给定的命令将在所有选定的节点上执行.它不会通过外壳进行处理,因此$ HOME之类的变量以及<",>," |和"&之类的操作将无法正常工作

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

shell模块采用命令名称,后跟由空格分隔的参数列表.几乎与命令模块完全一样,但是通过远程节点上的外壳程序(/bin/sh)运行命令.

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

So if you need any shell tricks, like wildcard expansion or access to environment variables, use shell module.

这篇关于如何使用带变量的通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆