Skip to main content
  1. Posts/

Get colorful ansible output in Jenkins

·228 words·2 mins·

Working with ansible is enjoyable, but it’s a little bland when you use it with Jenkins. Jenkins doesn’t spawn a TTY and that causes ansible to skip over the code that outputs status lines with colors. The fix is relatively straightforward.

First, install the AnsiColor Plugin on your Jenkins node.

Once that’s done, edit your Jenkins job so that you export ANSIBLE_FORCE_COLOR=true before running ansible:

export ANSIBLE_FORCE_COLOR=true
ansible-playbook -i hosts site.yml

If your ansible playbook requires sudo to run properly on your local host, be sure to use the -E option with sudo so that your environment variables are preserved when your job runs. For example:

export ANSIBLE_FORCE_COLOR=true
sudo -E ansible-playbook -i hosts site.yml

HOLD UP: As Sam Sharpe reminded me, the better way to handle environment variables with sudo is to add them to env_keep in your sudoers file (use visudo to edit it):

Defaults        env_reset
Defaults        env_keep += "ANSIBLE_FORCE_COLOR"

Adding it to env_keep is a more secure method and you won’t need the -E any longer on the command line.

While you’re on the configuration page for your Jenkins job, look for Color ANSI Console Output under the Build Environment section. Enable it and ensure xterm is selected in the drop-down box.

Save your new configuration and run your job again. You should have some awesome colors in your console output when your ansible job runs.