# How to sort ansible output

**URL:** <https://forum.ansible.com/t/how-to-sort-ansible-output/27263>\
**Category:** Ansible Project\
**Created:** [April 12, 2018, 7:42pm UTC](https://forum.ansible.com/t/how-to-sort-ansible-output/27263 "2018-04-12T19:42:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![The\_Wanderer](https://avatars.discourse-cdn.com/v4/letter/t/43a26b/32.png) [@The\_Wanderer](https://forum.ansible.com/u/The_Wanderer)\
**Post date:** [April 12, 2018, 7:42pm UTC](https://forum.ansible.com/t/how-to-sort-ansible-output/27263/1 "2018-04-12T19:42:16Z")

</div>

Hi all,

I have a couple of servers that are managed via ansible. On all of them, I have got a script - free.sh - that when run, outputs just the free memory, like - 5GB. This is not the free system RAM, but a value that’s obtained by complex calculations done via the script. I can call the script via ansible in this manner:

ansible -bK -a “/usr/bin/free.sh” apache-servers

(where apache-servers is the host-group on which I want to run the script)

And here’s the limitation that I’m facing - Ansible just returns results in the order the job completes. But, I want the result to be sorted according to the script’s output. Is this possible?

---

<div class="post-metadata">

**Author:** ![Biswadip\_Dutta](https://avatars.discourse-cdn.com/v4/letter/b/54ee81/32.png) [@Biswadip\_Dutta](https://forum.ansible.com/u/Biswadip_Dutta)\
**Post date:** [April 13, 2018, 6:19am UTC](https://forum.ansible.com/t/how-to-sort-ansible-output/27263/2 "2018-04-13T06:19:44Z")

</div>

I’d suggest you do the sorting part later. I mean after you get the Ansible results and then sort it.

---

<div class="post-metadata">

**Author:** ![The\_Wanderer](https://avatars.discourse-cdn.com/v4/letter/t/43a26b/32.png) [@The\_Wanderer](https://forum.ansible.com/u/The_Wanderer)\
**Post date:** [April 13, 2018, 7:00am UTC](https://forum.ansible.com/t/how-to-sort-ansible-output/27263/3 "2018-04-13T07:00:09Z")

</div>

I know, but how?

---

<div class="post-metadata">

**Author:** ![Biswadip\_Dutta](https://avatars.discourse-cdn.com/v4/letter/b/54ee81/32.png) [@Biswadip\_Dutta](https://forum.ansible.com/u/Biswadip_Dutta)\
**Post date:** [April 13, 2018, 8:53am UTC](https://forum.ansible.com/t/how-to-sort-ansible-output/27263/4 "2018-04-13T08:53:11Z")

</div>

Oh okay. I think I misunderstood your query. What I was suggesting is that you can write another script to sort the output based on your needs.  
I don’t think Ansible has such functionality. If there’s one then I will also like to know.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [April 14, 2018, 2:49am UTC](https://forum.ansible.com/t/how-to-sort-ansible-output/27263/5 "2018-04-14T02:49:39Z")

</div>

ansible -m script -a 'free.sh' |sort?

you can also use --tree to save a file with the output of each server

or you can just write your own callback to handle the output.
