Accessing inventory host variable in Ansible playbook
Asked Answered
G

6

79

In Ansible 2.1, I have a role being called by a playbook that needs access to a host file variable. Any thoughts on how to access it?

I am trying to access the ansible_ssh_host in the test1 section of the following inventory host file:

[test1]
test-1 ansible_ssh_host=abc.def.ghi.jkl ansible_ssh_port=1212

[test2]
test2-1 ansible_ssh_host=abc.def.ghi.mno ansible_ssh_port=1212

[test3]
test3-1 ansible_ssh_host=abc.def.ghi.pqr ansible_ssh_port=1212
test3-2 ansible_ssh_host=abc.def.ghi.stu ansible_ssh_port=1212

[all:children]
test1
test2
test3

I have tried accessing the role in the following fashions:

{{ hostvars.ansible_ssh_host }} 

and

{{ hostvars.test1.ansible_ssh_host }}

I get this error:

fatal: [localhost]: FAILED! => {"failed": true, "msg": "'ansible.vars.hostvars.HostVars object' has no attribute 'ansible'"}
Gerik answered 13/10, 2016 at 17:56 Comment(0)
S
89

You are on the right track about hostvars.
This magic variable is used to access information about other hosts.

hostvars is a hash with inventory hostnames as keys.
To access fields of each host, use hostvars['test-1'], hostvars['test2-1'], etc.

ansible_ssh_host is deprecated in favor of ansible_host since 2.0.
So you should first remove "_ssh" from inventory hosts arguments (i.e. to become "ansible_user", "ansible_host", and "ansible_port"), then in your role call it with:

{{ hostvars['your_host_group'].ansible_host }}
Subdiaconate answered 13/10, 2016 at 19:33 Comment(4)
spaciba- I tried that syntax but i think it was not working because of a typo on my sideGerik
You can access the ansible_host of the local machine by doing hostvars[inventory_hostname].ansible_hostGrandiose
There's no need to use hostvars at all, I'm able to use ansible_host variable directlyTorbart
That link has now moved to docs.ansible.com/ansible/latest/user_guide/…Iambic
I
35
[host_group]
host-1 ansible_ssh_host=192.168.0.21 node_name=foo
host-2 ansible_ssh_host=192.168.0.22 node_name=bar

[host_group:vars]
custom_var=asdasdasd

You can access host group vars using:

{{ hostvars['host_group'].custom_var }}

If you need a specific value from specific host, you can use:

{{ hostvars[groups['host_group'][0]].node_name }}
Idel answered 23/3, 2018 at 13:4 Comment(0)
S
30

You should be able to use the variable name directly

ansible_ssh_host

Or you can go through hostvars without having to specify the host literally by using the magic variable inventory_hostname

hostvars[inventory_hostname].ansible_ssh_host
Sherwin answered 12/12, 2017 at 4:6 Comment(0)
B
6

I struggled with this, too. My specific setup is: Your host.ini (with the modern names):

[test3]
test3-1 ansible_host=abc.def.ghi.pqr ansible_port=1212
test3-2 ansible_host=abc.def.ghi.stu ansible_port=1212

plus a play fill_file.yml

---
- remote_user: ec2-user
  hosts: test3
  tasks:
   - name: fill file
     template:
       src: file.j2
       dest: filled_file.txt

plus a template file.j2 , like

{% for host in groups['test3'] %}
   {{ hostvars[host].ansible_host }}
{% endfor %}

This worked for me, the result is

abc.def.ghi.pqr
abc.def.ghi.stu

I have to admit it's ansible 2.7, not 2.1. The template is a variation of an example in https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html.

The accepted answer didn't work in my setup. With a template

{{ hostvars['test3'].ansible_host }}

my play fails with "AnsibleUndefinedVariable: \"hostvars['test3']\" is undefined" .

Remark: I tried some variations, but failed, occasionally with "ansible.vars.hostvars.HostVars object has no element "; Some of this might be explained by what they say. in https://github.com/ansible/ansible/issues/13343#issuecomment-160992631

hostvars emulates a dictionary [...]. hostvars is also lazily loaded

Blackstone answered 21/3, 2019 at 17:15 Comment(1)
my play fails with "AnsibleUndefinedVariable: \"hostvars['test3']\" is undefined" this is because you don't have a host test3. it will work with {{ hostvars['test3-1'].ansible_host }} or {{ hostvars['test3-2'].ansible_host }}Runge
D
2

I've found also a nice and simple way to address hostsvars right on one of Ansible's Github issues

Looks like you can do this as well:

 - debug:
    msg: "{{ ansible_ssh_host }}"
Devindevina answered 22/11, 2018 at 9:50 Comment(1)
This has been deprecated to become "ansible_host"Extreme
D
2

Thanks a lot this note was very useful for me! Was able to send the variable defined under /group_var/vars in the ansible playbook as indicated below.

tasks:
- name: check service account password expiry
- command:

sh /home/monit/get_ldap_attr.sh {{ item }} {{ LDAP_AUTH_USR }}
Dorsad answered 16/5, 2019 at 7:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.