How to put the result of an echo command into a mutable variable

I have $ MY_VAR set to some value on the remote host and I want to request it from the playbook (put that value in a variable), this is what I see:

   - 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 that echo doesn't expand when called from non-essential

+3


source to share


1 answer


The problem is you are using a module command

. Here's what the documentation says :

This command will be executed on all selected nodes. I will not be processed through the shell, so type variables $HOME

and operations such as "<", ">", "|" and "&". won't work (use a module shell

if you need these functions).



So use shell

instead command

.

+4


source







All Articles