I've created a python script that uses parallel-ssh module (AKA pssh) in order to run various commands on remote nodes. One of the commands is puppet agent -t
, which outputs using ANSII color codes.
When the script is running and outputs to the terminal, the coloring works as expected. However, in the script log I see that it doesn't parse the ANSII codes and instead I get an ugly wrapper for each line, as below:
2016-01-12 20:23:30,748 INFO: [ubuntu01] ESC[1;31mWarning: Setting templatedir is deprecated. See http://links.puppetlabs.com/env-settings-deprecations
2016-01-12 20:23:30,748 INFO: [ubuntu01] (at /usr/lib/ruby/vendor_ruby/puppet/settings.rb:1139:in `issue_deprecation_warning')ESC[0m
2016-01-12 20:23:30,749 INFO: [ubuntu01] ESC[0;32mInfo: Retrieving pluginESC[0m
2016-01-12 20:23:31,984 INFO: [ubuntu01] ESC[0;32mInfo: Caching catalog for ubuntu01.puppetlabESC[0m
2016-01-12 20:23:32,014 INFO: [ubuntu01] ESC[0;32mInfo: Applying configuration version '1452623010'ESC[0m
2016-01-12 20:23:32,083 INFO: [ubuntu01] ESC[mNotice: Finished catalog run in 0.08 secondsESC[0m
2016-01-12 20:23:32,351 INFO: [ubuntu01] * agent is running
2016-01-12 20:23:32,353 INFO: [centos01] ESC[0;32mInfo: Retrieving pluginfactsESC[0m
2016-01-12 20:23:32,353 INFO: [centos01] ESC[0;32mInfo: Retrieving pluginESC[0m
2016-01-12 20:23:33,712 INFO: [centos01] ESC[0;32mInfo: Caching catalog for centos01.puppetlabESC[0m
2016-01-12 20:23:33,838 INFO: [centos01] ESC[0;32mInfo: Applying configuration version '1452623010'ESC[0m
2016-01-12 20:23:34,101 INFO: [centos01] ESC[mNotice: Finished catalog run in 0.27 secondsESC[0m
2016-01-12 20:23:34,421 INFO: [centos01] puppet (pid 2069) is running...
This is very frustrating, since it makes the log less readable.
I've tried to modify the logger config with re.compile(r'\x1b[^m]*m')
method which I've found in this thread, like this:
import logging
import re
ansi_escape = re.compile(r'\x1b[^m]*m')
message = ansi_escape.sub('', '%(message)s')
def set_logger(log_file):
"""Define the logger.."""
try:
logging.basicConfig(filename=log_file, level=logging.INFO,
format='%(asctime)s %(levelname)s: ' + message)
logger = logging.getLogger(__name__)
return logger
except IOError:
print "ERROR: No permissions to access the log file! Please run the script as root user (sudo will also do the trick)..\n"
exit(2)
The script runs properly, however no changes and the log still looks messy with all these ANSII codes. I assume that there might be another place in which I can set a separate handler for pssh logger, but I wasn't able to find it.
Any help would be very much appreciated!