Sean Iffland
Sean Iffland

Reputation: 29

Ansible facts in Dictionary for Jinja Template

I am trying to template out building servers and cannot get the jinja syntax correct. I have a var dictionary (assume "ansible_hostname" is "server" gathered from facts):

server:
  ip: 111.111.111.111

I am trying to call the variable in my template:

"{{ hostvars['ansible_hostname'].['ip'] }}"

I just cannot seem to figure out what is needed. A poke in the right direction would be great.

Sean

Upvotes: 0

Views: 350

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68144

Use inventory_hostname and remove the dot when brackets [] are used.

    "{{ hostvars[inventory_hostname]['ip'] }}"

For example, this inventory and playbook

shell> cat host
all:
  hosts:
    server:
      ip: 111.111.111.111

shell> cat pb.yml
- hosts: server
  tasks:
    - debug:
        msg: "{{ hostvars[inventory_hostname]['ip'] }}"

give (abridged)

shell> ansible-playbook pb.yml

    "msg": "111.111.111.111"

Upvotes: 1

Related Questions