Filtering ssh stdout for compatibility

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

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

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

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