# Get output of command that includes many quotes?

**URL:** <https://forum.ansible.com/t/get-output-of-command-that-includes-many-quotes/18195>\
**Category:** Ansible Project\
**Created:** [May 8, 2015, 9:21pm UTC](https://forum.ansible.com/t/get-output-of-command-that-includes-many-quotes/18195 "2015-05-08T21:21:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Oliver](https://avatars.discourse-cdn.com/v4/letter/j/97f17d/32.png) [@John\_Oliver](https://forum.ansible.com/u/John_Oliver)\
**Post date:** [May 8, 2015, 9:21pm UTC](https://forum.ansible.com/t/get-output-of-command-that-includes-many-quotes/18195/1 "2015-05-08T21:21:16Z")

</div>

I want to register a variable with my java version. I can get that with:

java -version 2\>&1 | head -1 | awk ‘BEGIN { FS = “"” } ; { print $2 }’

That gives me the result I want: 1.8.0\_45

But I cannot make this happen in ansible ☹ I’m going crazy with quotes and escapes. I’ve tried:

- name: Get java version  
shell: \>

java -version 2\>&1 | head -1 | awk ‘BEGIN { FS = “"” } ; { print $2 }’  
register: java\_ver

and:

- name: Get java version  
command: “{{ item }}”  
with\_items:
- java -version 2\>&1 | head -1 | awk ‘BEGIN { FS = “"” } ; { print $2 }’  
register: java\_ver

as well as trying various combinations of quotes and escape characters. I just can’t get the right recipe.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [May 8, 2015, 9:44pm UTC](https://forum.ansible.com/t/get-output-of-command-that-includes-many-quotes/18195/2 "2015-05-08T21:44:36Z")

</div>

Because I would rather use jinja2 than piping through a bunch of shell commands:

- name: Run java -version  
command: java -version  
register: java\_version\_out  
changed\_when: false

- name: Set java\_version var  
set\_fact: java\_version=“{{ java\_version\_out.stderr.splitlines()[0]|regex\_replace(‘[^\d.]+’, ‘’) }}”

- debug: var=java\_version

---

<div class="post-metadata">

**Author:** ![John\_Oliver](https://avatars.discourse-cdn.com/v4/letter/j/97f17d/32.png) [@John\_Oliver](https://forum.ansible.com/u/John_Oliver)\
**Post date:** [May 8, 2015, 10:03pm UTC](https://forum.ansible.com/t/get-output-of-command-that-includes-many-quotes/18195/3 "2015-05-08T22:03:24Z")

</div>

Nice! However, your regex is also killing the underscore, and I need that. I’m starting to monkey around with this, but I just don’t know jinja, and I bet you’ll have the right magic sauce before I can puzzle it out 🙂

Thanks!

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [May 8, 2015, 10:04pm UTC](https://forum.ansible.com/t/get-output-of-command-that-includes-many-quotes/18195/4 "2015-05-08T22:04:26Z")

</div>

You can add the underscore to the list of allowed characters in the regex\_replace:

regex\_replace(‘[^\d\_.]+’, ‘’)
