I would like to insert an IP address in to a J2 template which is used by an Ansible playbook. That IP adress is not the address of the host which is being provisioned, but the IP of the host from which the provisioning is done. Everything I have found so far covers using variables/facts related to the hosts being provisioned.
In other words: the IP I’d like to insert is the one in ['ansible_default_ipv4']['address']
when executing ansible -m setup 127.0.0.1
.
I think that I could use a local playbook to write a dynamically generated template file containing the IP, but I was hoping that this might be possible “the Ansible way”.
You can force Ansible to fetch facts about the control host by running the
setup
module locally by using either alocal_action
ordelegate_to
. You can then either register that output and parse it or simply useset_fact
to give it a useful name to use later in your template.An example play might look something like:
And then use the
ansible_control_host_address
variable in your template as normal:This is how I solved the same problem (Ansible 2.7):
Seems to work like a charm. :-)
Just use this: