Save saltstack console colored output to file

Save saltstack console colored output to file

Not an expert with Salt yet, but wow, saltstack is such a great tool! Like colored output? What about saving colors to your log files? Chrome extension inside (help needed!)

Hey, before we begin, I started playing around with colors in a terminal and tough it could be cool to have a chrome extension for this. Let’s create ANSI colors chrome extension together!

Alright let’s begin

Saltstack’s output has colors for the great good, green when result is pass, pink when it’s modified and red when it fails.


Easy to see when something goes wrong. Now when you start running commands on multiple servers, salt is very handy, but what if you’d like to share the output with a teammate? Go for it, copy paste, plain white/black text or take a screenshot of your terminal? Continue reading for a solution ;)

Console colors

See Bash Prompt HOWTO: Colors for reference ;)

Here are some env variable colors

export TEXT_BLACK=$'\033'\[30m
export TEXT_RED=$'\033'\[31m
export TEXT_GREEN=$'\033'\[32m
export TEXT_YELLOW=$'\033'\[33m
export TEXT_BLUE=$'\033'\[34m
export TEXT_MAGENTA=$'\033'\[35m
export TEXT_CYAN=$'\033'\[36m
export TEXT_WHITE=$'\033'\[37m

Values are ANSI Escape Codes and the environment variables above comes by default with Oh-my-zsh :)

echo "$TEXT_CYAN colors $TEXT_GREEN are $TEXT_RED mainstream"

example for the above

Colors and files

Now, depending on your shell, writing this to a shell with > should keep these ANSI characters, so when you cat the file again, you should see it with colors.

echo "$TEXT_CYAN colors $TEXT_GREEN are $TEXT_RED mainstream" > colored_file.txt && cat colored_file.txt

In my case, colors are kept (using Oh-my-zsh with iterm2 terminal emulator) and I’m happy with it, but sometimes, it’s not the case depending on the command or the shell being used.

Read colored output from file

Depending on your shell environment, less command may not need additional parameters, but if you try to pipe a colored file to less and don’t get any colors, try with -r parameter:

cat colored_file.txt | less -r

Thanks to this answer on stackoverflow and its comments.

keep salt colored output

Haven’t talked much about salt in current post so lets begin. Writing salt output to a file doesn’t keep ANSI colors. Luckily for us, salt command has a --force-color parameter added in salt issue #4121.

Salt colored output example

Solution to keep salt output looks like this:

salt-call state.highstate -l debug --force-color > salt.log
cat salt.log

Awesome, salt colors!

What about clear text (no color)?

I you want to remove colors from output, there are a few solutions available, here’s a question asking for a regex to remove ANSI Escape codes. My favorite answer is ansi-strip-cli npm module

npm install --g strip-ansi-cli


cat colored_file.txt | strip-ansi

No more colors :)

Otherwise, you can strip output with perl -pe 's/\x1b\[[0-9;]*[mG]//g' which is perfect if you don’t want node.

cat colored_file.txt | perl -pe 's/\x1b\[[0-9;]*[mG]//g'

salt solution

So with salt, I managed to get both output using the following:

salt-call state.highstate -l debug --force-color \
| tee salt-colored.log \
| tee >(strip-ansi > salt.log)

Prefer not to bloat your system with npm?

salt-call state.highstate -l debug --force-color \
| tee salt-colored.log \
| tee >(perl -pe 's/\x1b\[[0-9;]*[mG]//g' > salt.log)

Now you can enjoy salt output in both ways :)

cat salt-colored.log
cat salt.log

What’s the tee command?

Thanks to tldr pages

tldr tee
  Read from standard input and write to standard output and files.

  - Copy standard input to each FILE, and also to standard output:
    echo "example" | tee FILE

  - Append to the given FILEs, do not overwrite:
    echo "example" | tee -a FILE

Some random stuff because why not

Cool, you’ve reached this far, get lolcat for the lolz

gem install lolcat

man ascii lolcat