如何将echo命令的结果放入ansible变量 [英] how to put the result of an echo command into an ansible variable

查看:683
本文介绍了如何将echo命令的结果放入ansible变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将$ MY_VAR设置为远程主机上的某个值,我想从剧本中查询它 (将其值放入ansible变量中),这就是我所看到的:

I have $MY_VAR set to some value on the remote host, and I want to query it from a playbook (put it's value in an ansible variable), here's what I am seeing :


   - name: put shell var into ansible var
     command: echo $MY_VAR
     register: my_var

   - debug: var=my_var


ok: [192.168.78.10] => {
    "my_var": {
        "changed": true, 
        "cmd": [
            "echo", 
            "$my_var"
        ], 
        "delta": "0:00:00.002284", 
        "end": "2014-12-17 18:10:01.097217", 
        "invocation": {
            "module_args": "echo $my_var", 
            "module_name": "command"
        }, 
        "rc": 0, 
        "start": "2014-12-17 18:10:01.094933", 
        "stderr": "", 
        "stdout": "$my_var", 
        "stdout_lines": [
            "$my_var"
        ]
    }
}

注意:

如果我将命令更改为:

note:

If I change the command to :

 command: pwd

然后我得到了预期的结果:

then I get the expected result :

"my_var": {
  "stdout": "/home/vagrant", 
  "stdout_lines": [
      "/home/vagrant"  
  ]
}

从ansible调用时好像回声不会扩展

It seems as if echo does not expand when called from ansible

推荐答案

问题是您正在使用command模块. 文档内容如下:

The problem is that you are using the command module. Here's what the documentation says:

给定的命令将在所有选定的节点上执行.它不会 可以通过外壳进行处理,因此像$HOME这样的变量和操作 如<",>","|"和&"将不起作用(如果您使用shell模块, 需要这些功能.)

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 (use the shell module if you need these features).

因此,请使用shell而不是command.

这篇关于如何将echo命令的结果放入ansible变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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