Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to put the result of an echo command into an ansible variable

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

like image 791
Max L. Avatar asked Dec 17 '14 18:12

Max L.


1 Answers

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.

like image 106
tedder42 Avatar answered Nov 01 '22 11:11

tedder42