storing standard output to variable

I want to be able to run a command such as cat or grep following and store the standard output into a variable i can use further down

I tried to do the following but its wrong

for example

  • name: Get info
    shell: cat test.php p | grep -v “*” | grep “‘database’ =>”| cut -d “>” -f 2 | cut -d “'” -f 2
    register: so_ouput

  • name: write to ffile
    action: command echo {{so_output}} > test.file

What is the right way of doing this?

I suggest you do a

  • name: examine shell output

debug:
var: so_output

and run with -v to see what you have stored in so_output variable. From memory you’ll have stdout and stdout_lines attributes in so_output and possibly the same for stderr.

Depending on what you are trying to achieve you might want to use either template module to write the contents of so_output to a file, or at a push you can use the ‘content’ parameter of the copy module to write (simple values) to a file.

Best advice is to look at the debug module output then you can see what you have stored and work out how work with it.

Hope this helps,

Jon

ok i got it now i was missing the .stdout, because ansible register collects a bunch of other stuff , not just the standard output , so you mnust specify it, Thanks J

for example

  • name: Get info
    shell: cat test.php p | grep -v “*” | grep “‘database’ =>”| cut -d “>” -f 2 | cut -d “'” -f 2
    register: so_ouput

  • name: write to ffile
    action: command echo {{so_output.stdout}} > test.file

This will not work since command doesn't support redirect.
You must user shell or do it the ansible way.

- name: Write file.
   copy:
     content: '{{ so_output.stdout }}'
     dest: test.file
   delegate_to: localhost