pluginsansiblecallback

Ansible callback plugin prints metacharacters when playbook trace is redirected to file


I'm writing an Ansible playbook with an ad-hoc df -h command:

---
- name: execute df -h
  hosts: all
  gather_facts: false

  tasks:
    - name: Execute df -h
      command: df -h
      register: dfh_output

    - name: Output df -h
      debug:
        var: dfh_output.stdout

Obviously the outputs when the playbook is running, the trace is not very pretty, human-readable and most important it's not idented like the df -h command is idented.
So following Formatting stdout in a debug task of Ansible, I used the stdout_callback.

My problem now is that when I redirect it to a file:
$ ansible-playbook -i inventory.yml playbook_dfh.yml > test.log
The test.log have all the metacharacters for the identation printed out:

TASK [Output df -h] *********************************

^[[0;32mok: [MYSERVER] => ^[[0m
^[[0;32m  dfh_output.stdout: |-^[[0m
^[[0;32m
            Filesystem                                  Size  Used Avail Use% 
^[[0;32m    devtmpfs                                    126G     0  126G   0% 
^[[0;32m    tmpfs                                       126G     0  126G   0% 

I've thought about sed ing, but it's going to be annoying to do this every time. How can the file be formatted like it is for the playbook execution trace?

My best bet was to directly modify the Python code for ansible.plugin.callback but I don't know where the code is located (I've tried /usr/bin, /, etc... and I literally can't find it).
I've tried a few other stuff, from the ansible.builtin.default callback but nothing seems to work.

My concern is that I may be running out of solutions that would be implementable with Ansible/YAML. Maybe it's a dead end? Any ideas?


Solution

  • These meta-characters are used to color the output. You can suppress this with configuration, either in ansible.cfg:

    [default]
    nocolor = True
    

    Or by setting the environment variable: export ANSIBLE_NOCOLOR=True.