Search code examples
ansiblejinja2

How to get ansible playbook terminal output to show ansible_host jinja var from inventory file instead of literal jinja syntax


Will do my best to explain my situation. Trying to figure out how to get the terminal output of a jinja expression for a var I have set when running the playbook at the command line. Specifically, getting the return line for changed:... to show the actual value instead of the literal jinja syntax that I have for ansible_host='{{ inventory_hostname }}.{{ host_domain }}':

Inventory file fragment (ce_campus_rtr_inventory_singular):

[all:vars]
ansible_connection=network_cli
host_domain=wfm.pvt
ansible_host='{{ inventory_hostname }}.{{ host_domain }}'

[CEASF]
CEASF-RTR

I am using Cisco ios config module and the ansible-playbook output snippet comes out like this:

changed: [CEASF-RTR -> {{ inventory_hostname }}.{{ host_domain }}] => (item=cowfm-tower-core) => {

But I need it to actually output the correct hostname and domain name, such as CEASF-RTR.wfm.pvt for example, because it makes debugging harder when it does not.

When running the playbook in verbose mode, I can see the correct var value outputted:

<CEASF-RTR.wfm.pvt> EXEC /bin/sh -c....

Same when I debug at the command line:

user$ ansible -i ce_campus_rtr_inventory_singular all -m debug -a "var=hostvars[inventory_hostname].ansible_host"

CEASF-RTR | SUCCESS => {
    "hostvars[inventory_hostname].ansible_host": "CEASF-RTR.wfm.pvt"
}

Do I have to define ansible_host per each host in my inventory file? Or is there some arg I need to pass when running ansible-playbook command to process the jinja expression in the inventory file so that the output is this:

changed: [CEASF-RTR -> CEASF-RTR.wfm.pvt] => (item=cowfm-tower-core) => {

instead of this..

changed: [CEASF-RTR -> {{ inventory_hostname }}.{{ host_domain }}] => (item=cowfm-tower-core) => {

Can I modify ansible return data going to stdout during execution perhaps? Looks like I would have to do something with a custom callback plugin.


Solution

  • I discovered that the default terminal output is set by the stdout callback plugin, specifically in my case, default.py.

    Turns out, you can develop your own to tailor the output for what you want to see: Develop custom callback plugin