如何使用带变量的通配符 [英] How to use wildcard with variable
问题描述
我想按主机名列出文件.但是问题是我不能正确使用带变量的通配符.有人可以建议我这样做吗.
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屋!