# Hard to automate reading output from one cmd and using as input for another - best practices?

**URL:** <https://forum.ansible.com/t/hard-to-automate-reading-output-from-one-cmd-and-using-as-input-for-another-best-practices/14532>\
**Category:** Ansible Project\
**Created:** [February 10, 2014, 4:02pm UTC](https://forum.ansible.com/t/hard-to-automate-reading-output-from-one-cmd-and-using-as-input-for-another-best-practices/14532 "2014-02-10T16:02:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcus\_Philip](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@Marcus\_Philip](https://forum.ansible.com/u/Marcus_Philip)\
**Post date:** [February 10, 2014, 4:02pm UTC](https://forum.ansible.com/t/hard-to-automate-reading-output-from-one-cmd-and-using-as-input-for-another-best-practices/14532/1 "2014-02-10T16:02:51Z")

</div>

I’ve been using Ansible for more than a year now and it’s great. I mainly use it in playbook mode, and it’s mainly called by the build server as part of a pipeline.

My concern is this: Several times I have gotten stuck when there’s a need for using the output from one command as input to another. This makes it hard to automate flows. I find the Ansible built-in and Jinja filter too limited. With shell, I’m used to working with line-oriented output and piping one cmd to the next. I find that hard to do in Ansible.

Am I the only one with this problem?  
Am I missing something?  
What are the best practices?  
I don’t know Python - is this what’s limiting me?  
Can you share your experiences/solutions?

I looked at some threads on this forum and I found some people with similar problems but no general solution. Is it correct that some (specialized) modules return the output as structured data? Could this be added to the basic modules?

**Here’s an example:**  
The first task calls a shell on target host that calls the Tomcat manager on localhost which lists the web apps. A output line looks like this:  
/MyWebApp:running:0:MyWebApp

After that I would like to create a list of the _running_ webapps and use this in the following command.

- name: Get running services

local\_action: shell getRunningServices {{ inventory\_hostname }}

register: validate\_int\_result

- name: Echo running services

local\_action: command echo “Service - {{ item }}”

with\_items: validate\_int\_result.stdout\_lines

when: “‘:running:’ in item”

Here the second task only identifies the relevant lines. _How do I do what I need to do, i.e. **collect the running services into a list variable?** _

---

<div class="post-metadata">

**Author:** ![James\_Martin2](https://avatars.discourse-cdn.com/v4/letter/j/b9e5f3/32.png) [@James\_Martin2](https://forum.ansible.com/u/James_Martin2)\
**Post date:** [February 10, 2014, 4:10pm UTC](https://forum.ansible.com/t/hard-to-automate-reading-output-from-one-cmd-and-using-as-input-for-another-best-practices/14532/2 "2014-02-10T16:10:46Z")

</div>

You could write a simple module to gather that information for you and present it as facts.

---

<div class="post-metadata">

**Author:** ![David\_Adams](https://avatars.discourse-cdn.com/v4/letter/d/df788c/32.png) [@David\_Adams](https://forum.ansible.com/u/David_Adams)\
**Post date:** [February 10, 2014, 5:41pm UTC](https://forum.ansible.com/t/hard-to-automate-reading-output-from-one-cmd-and-using-as-input-for-another-best-practices/14532/3 "2014-02-10T17:41:49Z")

</div>

Using the “shell” module you can do all the piping you want to get it down to exactly the list you one, one item per line, and then use result.stdout\_lines as your list. In your example:

- name: Get running services  
shell: “getRunningServices {{ inventory\_hostname }} |grep :running: |cut -d: -f4”  
register: service\_result

- name: List running services  
debug: msg=“{{ item }} is running”  
with\_items: service\_result.stdout\_lines

-dave
