# ansible parse text string from stdout

**URL:** <https://forum.ansible.com/t/ansible-parse-text-string-from-stdout/29169>\
**Category:** Ansible Project\
**Created:** [November 30, 2018, 9:39pm UTC](https://forum.ansible.com/t/ansible-parse-text-string-from-stdout/29169 "2018-11-30T21:39:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saravanan](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@Saravanan](https://forum.ansible.com/u/Saravanan)\
**Post date:** [November 30, 2018, 9:39pm UTC](https://forum.ansible.com/t/ansible-parse-text-string-from-stdout/29169/1 "2018-11-30T21:39:46Z")

</div>

Hi Ansible Experts,  
Need help to capture the JBOSS EAP JVM uptime value through Ansible playbook. From the below task stdout need to parse the uptime value which is in numeric field to a variable.

{  
“outcome” =\> “success”,  
“result” =\> 3613082543L  
}

Can you please suggest the right string parsing method to achieve this? I was unable to use Unix commands like grep/ awk/ sed to filter this numeric value.

---

<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:** [December 1, 2018, 12:35am UTC](https://forum.ansible.com/t/ansible-parse-text-string-from-stdout/29169/2 "2018-12-01T00:35:00Z")

</div>

> Hi Ansible Experts,  
> Need help to capture the JBOSS EAP JVM uptime value through Ansible  
> playbook. From the below task stdout need to parse the uptime value which  
> is in numeric field to a variable.
> 
> {  
> &nbsp;&nbsp;&nbsp;&nbsp;"outcome" =\> "success",  
> &nbsp;&nbsp;&nbsp;&nbsp;"result" =\> 3613082543L  
> }
> 
> Can you please suggest the right string parsing method to achieve this? I  
> was unable to use Unix commands like grep/ awk/ sed to filter this numeric  
> value.

You have not provided the task, but if you are using the shell module just add  
&nbsp;&nbsp;\> awk '/result/ { print $3 }'  
and you stdout will only contain that value.

---

<div class="post-metadata">

**Author:** ![Saravanan](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@Saravanan](https://forum.ansible.com/u/Saravanan)\
**Post date:** [December 1, 2018, 2:11am UTC](https://forum.ansible.com/t/ansible-parse-text-string-from-stdout/29169/3 "2018-12-01T02:11:52Z")

</div>

Thanks for your reply Kai.  
I am using the below task using command module to get the uptime value of JBOSS EAP server instance. However still I am receiving error as mentioned below.

`

```auto
- name: Check the JBOSS Server Uptime Domain mode
  command: "{{ CLI_SCRIPT }} --connect controller={{ item.hostname }}:{{ item.port }} --user={{ jbossuser }} --password={{ jbosspass }} /host={{ item.host }}-{{ item.server }}/core-service=platform-mbean/type=runtime:read-attribute(name=uptime)| awk '/result/ { print $3 }'"
  register: uptime
  delegate_to: remoteserver
  ignore_errors: yes

```

`

Error message:  
`  
fatal: [127.0.0.1 → remoteserver]: FAILED! =\> {“changed”: true, “cmd”: [“/opt/jboss/jboss-eap-7.2.0/bin/jboss-cli.sh”, “–connect”, “controller=remoteserver:11011”, “–user=someuser”, “–password=sometext”, “/host=jbosshost-server/core-service=platform-mbean/type=runtime:read-attribute(name=uptime)|”, “awk”, “/result/ { print $3 }”], “delta”: “0:00:09.420046”, “end”: “2018-11-30 20:05:18.798064”, “msg”: “non-zero return code”, “rc”: 1, “start”: “2018-11-30 20:05:09.378018”, “stderr”: “Duplicate argument ‘–command’/‘–commands’.”, “stderr\_lines”: [“Duplicate argument ‘–command’/‘–commands’.”], “stdout”: “”, “stdout\_lines”: }

`

I believe we can use Ansible regex, to capture this value.

---

<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:** [December 1, 2018, 10:13am UTC](https://forum.ansible.com/t/ansible-parse-text-string-from-stdout/29169/4 "2018-12-01T10:13:57Z")

</div>

> Thanks for your reply Kai.  
> I am using the below task using command module to get the uptime value of  
> JBOSS EAP server instance. However still I am receiving error as mentioned  
> below.
> 
> - name: Check the JBOSS Server Uptime Domain mode  
> &nbsp;&nbsp;command: "{{ CLI\_SCRIPT }} --connect controller={{ item.hostname }}:{{ item.port }} --user={{ jbossuser }} --password={{ jbosspass }} /host={{ item.host }}-{{ item.server }}/core-service=platform-mbean/type=runtime:read-attribute(name=uptime)| awk '/result/ { print $3 }'"  
> &nbsp;&nbsp;register: uptime  
> &nbsp;&nbsp;delegate\_to: remoteserver  
> &nbsp;&nbsp;ignore\_errors: yes

Command module doesn't support pipe or redirect only shell module does that.  
So just replace command: with shell: and it this should work.

> I believe we can use Ansible regex, to capture this value.

To my knowledge (or lack there of) this is actually cumbersome to achieve in Ansible, just using shell module and add awk is way easier.

---

<div class="post-metadata">

**Author:** ![Saravanan](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@Saravanan](https://forum.ansible.com/u/Saravanan)\
**Post date:** [December 2, 2018, 6:08pm UTC](https://forum.ansible.com/t/ansible-parse-text-string-from-stdout/29169/5 "2018-12-02T18:08:54Z")

</div>

Unable to use shell module to query the JBOSS uptime with CLI script in Ansible. Can you please help to rectify the error?

`

- name: Check the JBOSS Server Uptime Domain mode  
shell: “{{ CLI\_SCRIPT }} --connect controller={{ item.hostname }}:{{ item.port }} --user={{ myusername }} --password={{ mypassword }} /host={{ item.host }}-{{ item.server }}/core-service=platform-mbean/type=runtime:read-attribute(name=uptime)| awk ‘/result/ { print $3 }’”  
register: uptime  
delegate\_to: remoteserver  
ignore\_errors: yes

`

Error message:

fatal: [127.0.0.1 → remoteserver]: FAILED! =\> {“changed”: true, “cmd”: “/opt/jboss/jboss-eap-7.2.0/bin/jboss-cli.sh --connect controller=jbossserver:11011 --user=myusername --password=mypassword /host=jbosshost-server/core-service=platform-mbean/type=runtime:read-attribute(name=uptime) | awk ‘/result/ { print $3 }’”, “delta”: “0:00:00.278269”, “end”: “2018-12-02 12:03:04.863388”, “msg”: “non-zero return code”, “rc”: 2, “start”: “2018-12-02 12:03:04.585119”, “stderr”: “/bin/sh: syntax error at line 1: `(' unexpected", "stderr_lines": ["/bin/sh: syntax error at line 1: `(’ unexpected”], “stdout”: “”, “stdout\_lines”: }

---

<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:** [December 2, 2018, 6:18pm UTC](https://forum.ansible.com/t/ansible-parse-text-string-from-stdout/29169/6 "2018-12-02T18:18:26Z")

</div>

It doesn't like the parenthesise so try to escape them both with a backslash.

---

<div class="post-metadata">

**Author:** ![Saravanan](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@Saravanan](https://forum.ansible.com/u/Saravanan)\
**Post date:** [December 2, 2018, 6:54pm UTC](https://forum.ansible.com/t/ansible-parse-text-string-from-stdout/29169/7 "2018-12-02T18:54:37Z")

</div>

Thank you for the assistance. Added with two backslash to escape the parenthesis.

`

- name: Check the JBOSS Server Uptime Domain mode

shell: “{{ CLI\_SCRIPT }} --connect controller={{ item.hostname }}:{{ item.port }} --user={{ myusername }} --password={{ mypassword }} /host={{ item.host }}-{{ item.server }}/core-service=platform-mbean/type=runtime:read-attribute\(name=uptime\)| awk ‘/result/ { print $3 }’”

register: uptime  
delegate\_to: remoteserver  
ignore\_errors: yes

`
