# storing standard output to variable

**URL:** <https://forum.ansible.com/t/storing-standard-output-to-variable/24985>\
**Category:** Ansible Project\
**Created:** [July 14, 2017, 10:58am UTC](https://forum.ansible.com/t/storing-standard-output-to-variable/24985 "2017-07-14T10:58:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stelios](https://avatars.discourse-cdn.com/v4/letter/s/8dc957/32.png) [@stelios](https://forum.ansible.com/u/stelios)\
**Post date:** [July 14, 2017, 10:58am UTC](https://forum.ansible.com/t/storing-standard-output-to-variable/24985/1 "2017-07-14T10:58:40Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [July 14, 2017, 11:50am UTC](https://forum.ansible.com/t/storing-standard-output-to-variable/24985/2 "2017-07-14T11:50:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stelios](https://avatars.discourse-cdn.com/v4/letter/s/8dc957/32.png) [@stelios](https://forum.ansible.com/u/stelios)\
**Post date:** [July 14, 2017, 12:00pm UTC](https://forum.ansible.com/t/storing-standard-output-to-variable/24985/3 "2017-07-14T12:00:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 14, 2017, 2:15pm UTC](https://forum.ansible.com/t/storing-standard-output-to-variable/24985/4 "2017-07-14T14:15:10Z")

</div>

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

- name: Write file.  
&nbsp;&nbsp;&nbsp;copy:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content: '{{ so\_output.stdout }}'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: test.file  
&nbsp;&nbsp;&nbsp;delegate\_to: localhost
