How to get cleaner output from Ansible jobs

ansible

I'm taking time this week to get up to speed on Ansible.

Despite having:

$ ansible-config dump | grep -i ^ans.*color
ANSIBLE_FORCE_COLOR(default) = False
ANSIBLE_NOCOLOR(env: ANSIBLE_NOCOLOR) = True

I get ANSI-ish junk in my output:

...
resizewin: timeout reading from terminal
...
Shared connection to host.example.com closed.

^[[45;160R       25.22 real         2.64 user         1.49 sys
$ 5;160R5;160R5;160R5;160R

Also, is there any way to get just the stdout of the ansible job, without the Ansible artifacts

 host.example.com | CHANGED | rc=0 >>

and

Shared connection to host.example.com closed.

I can see that output has value in some situations, but in other situations I'd like to be able to capture just the stdout. Presently, I'm resorting to head and tail tricks to strip the first line and the last two lines of the output, which works, but is tiresome.

$ ansible-config dump --only-changed
ANSIBLE_NOCOLOR(/usr/local/etc/ansible/ansible.cfg) = True
DEFAULT_PRIVATE_KEY_FILE(/usr/local/etc/ansible/ansible.cfg) = /home/jim/.ssh/my-ansible-id_rsa

Best Answer

Ad-hoc ansible output sometimes includes a dict of the return values, and warnings go to stderr. But the first line looks like something intended for human consumption not a machine. Plus you only get to run one task.

If you are parsing the results of a task at all, write a playbook and run it with ansible-playbook. It may be short, that's fine.

- name: Do a thing
  register: result
  command: /bin/uptime

- name: Print result dict
  debug:
    var: result

Jinja expressions are available. Print out only the variable you care about (perhaps result.stdout), render a template, run another command based on stdout, or do any number of things with the result.