How to discern, whether Ansible runs in color mode?

31 Views Asked by At

I'm implementing a role, which calls an external application (using shell-module). That application can color its output to make it easier on human eyes.

I'd like to enable -- or disable -- that coloring depending on whether Ansible's own coloring of output is enabled, either because its stdout is a TTY, or because force_color is set.

But how do I detect this? There is no ansible_force_color, for example...

1

There are 1 best solutions below

1
Alexander Pletnev On

You can use the config lookup to access Ansible configuration:

# playbook.yaml
---
- name: Check the colors
  connection: local
  gather_facts: false
  tasks:
    - name: Check if ANSIBLE_FORCE_COLOR setting is set
      debug:
        msg: "{{ lookup('config', 'ANSIBLE_FORCE_COLOR') | bool }}"