
官方文档中是不建议使用shell=True的,因为这可能导致shell injection安全问题,但是有些情况下用shell模块就很方便,比如我要批量删除一些文件,
ansible -i inventory all -m command -a "rm -f /etc/yum.repos.d/CentOS.repo" -U root -s -f 50 -kK
因为你的命令行中包含了通配符号,通配符必须要有在shell环境中才能被识别出,不然,它只能删除CentOS.repo这一个文件。 《linux就该这么学》
所以你需要执行以下命令才能成功
ansible -i inventory all -m shell -a "rm -f /etc/yum.repos.d/CentOS.repo" -U root -s -f 50 -kK
而这两个命令所生成的可执行脚本的区别就一行
<MODULE_ARGS = 'rm -f /etc/yum.repos.d/CentOS.repo'
command xxx,执行linux shell自带的命令xxx。这些命令通常在/lib/bin下面,如ls,grep,等等。可以避免linux查找指令的位置。可选参数pVv,p表示指定查找路径的命令,V表示打印详细信息,v表示打印信息。例如:command ls,与直接执行ls结果相同。
command ls | grep bbb 与直接执行ls | grep bbb相同。
这个命令,在bash1.x 版本时有用。现在已经用处不大了。目前的bash是4.x版本。
详细信息可以看man bash,对command 的解释:
command [-pVv] command [arg ...]
Run command with args suppressing the normal shell function lookup. Only builtin commands or commands found in the PATH are executed.
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)