# Filtering ssh stdout for compatibility

**URL:** <https://forum.ansible.com/t/filtering-ssh-stdout-for-compatibility/21073>\
**Category:** Ansible Developer\
**Created:** [April 5, 2016, 7:34pm UTC](https://forum.ansible.com/t/filtering-ssh-stdout-for-compatibility/21073 "2016-04-05T19:34:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kenneth\_Bell](https://avatars.discourse-cdn.com/v4/letter/k/6f9a4e/32.png) [@Kenneth\_Bell](https://forum.ansible.com/u/Kenneth_Bell)\
**Post date:** [April 5, 2016, 7:34pm UTC](https://forum.ansible.com/t/filtering-ssh-stdout-for-compatibility/21073/1 "2016-04-05T19:34:39Z")

</div>

Hi,

We’ve got a system that puts extraneous output in stdout when accessed via SSH, which breaks Ansible. I’ve prepared a patch (below) that tries to work-around this in a generic way, in case there are other systems out there that do something similar.

The patch adds a new parameter ssh\_stdout\_filter, which is a regex. If set, it removes everything that matches the regex from the stdout stream. Typical usage would be something like this:

add\_host:  
hostname: “my\_host”  
ansible\_ssh\_user: “user”

ansible\_ssh\_host: “1.1.1.1”

ansible\_ssh\_stdout\_filter: " Done\r\n"

groups: “A, B”

I’d appreciate feedback on the patch, and whether this is something that would be welcomed as a pull request.

Thanks,

Ken

From 6c43527a528c2e9cb8ff968128df9dcfff129a53 Mon Sep 17 00:00:00 2001

---

<div class="post-metadata">

**Author:** ![Florian\_Apolloner](https://avatars.discourse-cdn.com/v4/letter/f/dfb087/32.png) [@Florian\_Apolloner](https://forum.ansible.com/u/Florian_Apolloner)\
**Post date:** [April 5, 2016, 10:07pm UTC](https://forum.ansible.com/t/filtering-ssh-stdout-for-compatibility/21073/2 "2016-04-05T22:07:47Z")

</div>

Using re.sub sounds very dangerous – or can you gurantee that the “valid” part of stdout will never contain your filter string?

---

<div class="post-metadata">

**Author:** ![Kenneth\_Bell](https://avatars.discourse-cdn.com/v4/letter/k/6f9a4e/32.png) [@Kenneth\_Bell](https://forum.ansible.com/u/Kenneth_Bell)\
**Post date:** [April 5, 2016, 11:03pm UTC](https://forum.ansible.com/t/filtering-ssh-stdout-for-compatibility/21073/3 "2016-04-05T23:03:36Z")

</div>

Thanks for taking a look.

I think it comes down to the nature of the ‘extraneous’ data that the host injects into stdout.

If that extraneous data happens to be the same as some expected output, that could be a problem. It’ll come down to the person authoring the ansible.cfg / playbooks / dynamic inventory script to create the most specific regular expression they can.

The host I’m dealing with happens to inject ‘Done\r\n’ twice in the stdout output stream, so for my needs a basic string-match would be sufficient. The regex allows for the case where there might be some variance to the extraneous data.

If ansible\_ssh\_stdout\_filter isn’t specified, the code will fall-through without modifying stdout - so no negative impact on backwards compatibility, unless ansible\_ssh\_stdout\_filter is specified. If ansible\_ssh\_stdout\_filter is specified, then it’s because ansible is failing to work with a host, in which case this change might just be enough to let ansible work.

Ken

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [April 6, 2016, 5:58am UTC](https://forum.ansible.com/t/filtering-ssh-stdout-for-compatibility/21073/4 "2016-04-06T05:58:43Z")

</div>

I think maybe it would be better to create a specialised ssh connection plugin for this device with the unusual behaviour, rather than adding regex logic to the ssh plugin.  
You’d need to set a connection type against the host but it would leave ssh untouched.

Just a thought,

Jon
