Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
Making the ANSI escape regexes configurable in the Shell class #17989
The module_utils Shell class uses a hardcoded set of regexes to filter out ansi color sequences and terminal escape codes. For using the Shell with some network appliances, this set of regexes needs to be overridden (or expanded).
This change allows a CliBase subclass to override the ansi escape regexes in the same manner that it uses to provide the prompt and errors with CLI_PROMPTS_RE and CLI_ERRORS_RE. This checks for a CLI_ANSI_RE, and if provided, uses that instead of the default ANSI_RE for sanitizing lines read from the remote shell.
The test failures seem to be a problem with the CI system:
We took a look at this among a small group in today's meeting and weren't sure if this was the right API for doing ths. We thought that perhaps overriding the split method or passing a method into the constructor rather than a regex might make more sense. But we were limited in evaluating what would be appropriate by not knowing how this is intended to be used. We have here the ability to override the Shell() constructor but nothing that's calling the constructor with overridden values. So we're not seeing the complete picture and knowing what the API should look like to accommodate that.
Could you give us some more information about how this would be called?
The background on this was that I was using ansible with a network appliance which used a series of escape codes that were subtly different from those used by say a Cisco router. So I was calling the Shell() constructor with a different list of regexes (one was the same, but I had to add about 5-6 more to get Shell() to properly parse the output of the particular appliance I was working with).
The simplest method that I could find was to add the ability to provide a different set of regexes when instantiating the Shell class, so that it could be customized for communicating with different appliances.
IIRC, overriding split() looked like it was workable, but heavy-handed since the method performed all the right steps, it just needed a larger set of regexes to deal with some quirks in how the appliance returned prompts and did pagination.
OTOH, passing in a function that returned a list of appropriate regexes could also work. That, to me, feels like a stylistic choice, but retains the overall same functionality.
I don't have access to the full source that I was using at the time (this was for a project at a previous employer), so I'm going somewhat off of memory on this.