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

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

问题描述

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

<前>- 名称:将 shell var 放入 ansible var命令:echo $MY_VAR注册:my_var- 调试:var=my_var

<前>好的:[192.168.78.10] => {my_var":{改变":真的,命令":[回声",$my_var"],"delta": "0:00:00.002284","end": "2014-12-17 18:10:01.097217",调用":{"module_args": "echo $my_var",模块名称":命令"},rc":0,"开始": "2014-12-17 18:10:01.094933","stderr": "","stdout": "$my_var",标准输出线":[$my_var"]}}

注意:

如果我将命令更改为:

 命令:pwd

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

"my_var": {"stdout": "/home/vagrant",标准输出线":[/家/流浪者"]}

当从 ansible 调用时,echo 似乎没有扩展

解决方案

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

<块引用>

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

所以,使用 shell 而不是 command.

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"  
  ]
}

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

解决方案

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

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).

So, use shell instead of command.

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

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