通配符在 ansible shell 命令中不起作用

通配符在 ansible shell 命令中不起作用

通过 powershell 运行 ansible 模块时,shell 命令不会扩展通配符。

如果我删除通配符并指定版本号,它将运行“rabbitmq_server-3.9.3”。

关于如何使用通配符来实现这个功能有什么建议吗?

Yaml 代码:

  - name: Check if RabbitMQ is already installed
    shell: "\"/cygdrive/c/Program\ Files/RabbitMQ\ Server/rabbitmq_server-*/sbin/rabbitmq-service.bat\" status"
    ignore_errors: true
    register: RabbitMQ_install_status

错误:

‘C:\cygwin\bin\bash.exe -c ansible-playbook 测试.yml’:

TASK [Check if RabbitMQ is already installed] **************************************************************************fatal: [localhost]: FAILED! => {"changed": true, "cmd": "\"/cygdrive/c/Program Files/RabbitMQ Server/rabbitmq_server-*/sbin/rabbitmq-service.bat\" status", "delta": "0:00:00.042184", "end": "2021-08-16 13:43:52.066236", "msg": "non-zero return code", "rc": 127, "start": "2021-08-16 13:43:52.024052", "stderr": "/bin/sh: /cygdrive/c/Program Files/RabbitMQ Server/rabbitmq_server-*/sbin/rabbitmq-service.bat: No such file or directory", "stderr_lines": ["/bin/sh: /cygdrive/c/Program Files/RabbitMQ Server/rabbitmq_server-*/sbin/rabbitmq-service.bat: No such file or directory"], "stdout": "", "stdout_lines": []}
...ignoring

答案1

由于您已将通配符放在引号中,因此 shell 不会扩展通配符。单引号或双引号中均不会进行通配符扩展。

\"/cygdrive/c/Program\ Files/RabbitMQ\ Server/rabbitmq_server-*/sbin/rabbitmq-service.bat\"

假设您这样做是为了避免路径中的空格。

尝试仅引用需要引用的路径部分。

\"/cygdrive/c/Program\ Files/RabbitMQ\ Server\"/rabbitmq_server-*/sbin/rabbitmq-service.bat
##                                           ##

答案2

你需要.*

“/cygdrive/c/Program\Files/RabbitMQ\Server/rabbitmq_server-.*/sbin/rabbitmq-service.bat”

相关内容