Get output of command that includes many quotes?

47 views
Skip to first unread message

John Oliver

unread,
May 8, 2015, 5:21:16 PM5/8/15
to ansible...@googlegroups.com
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.

Matt Martz

unread,
May 8, 2015, 5:45:21 PM5/8/15
to ansible...@googlegroups.com
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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/30e7016d-9285-424a-adb7-2fca96e87044%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net

John Oliver

unread,
May 8, 2015, 6:03:25 PM5/8/15
to ansible...@googlegroups.com
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!

Matt Martz

unread,
May 8, 2015, 6:05:11 PM5/8/15
to ansible...@googlegroups.com
You can add the underscore to the list of allowed characters in the regex_replace:

regex_replace('[^\d_.]+', '')

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages