Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

jinja2 - ansible jinja template hostvars can not get ansible_host

I am trying to iterate over hostvars in an ansible playbook and it appears to be working just fine to several hostvars, except the one I need: ansible_host

If I try:

{% for host in groups['all'] %}
  server {{ host }} {{ hostvars[host] }}  
{% endfor %}

It outputs this for each server: server server2 {'ansible_verbosity': 0,, '... 'ansible_check_mode': False, 'ansible_run_tags': [u'all'], 'ansible_skip_tags': [], u'ansible_host': u'10.192.11.17', 'ansible_version': {'major': 2, 'full': '2.9.11', 'string': '2.9.11', 'minor': 9, 'revision': 11}}

Which holds all vars.

Now If I try

{% for host in groups['all'] %}
  server {{ host }} {{ hostvars[host]['verbosity'] }}  
{% endfor %}

it will get me the ansible_verbosity just fine. But I can not find a way to get the ansible_host

{% for host in groups['all'] %}
  server {{ host }} {{ hostvars[host]['ansible_host'] }}  
{% endfor %}

I think the problem maybe related to the fact ansible_host is a unicode var ( u'ansible_host' ). I say that because the other 2 unicode vars fails with the same problem while all the others work just fine.

Any idea how to get that var?

question from:https://stackoverflow.com/questions/65910468/ansible-jinja-template-hostvars-can-not-get-ansible-host

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Q: "hostvars can not get ansible_host"

A: ansible_host is not included in hostvars if it is not explicitly declared. ansible_host is a Connection variable. It is used, for example, as a parameter of the ssh connection plugin (see ansible-doc -t connection ssh). It is needed if inventory_hostname does not resolve. Very probably, any of your hosts don't have an ansible_host explicitly set. This is causing the problem. If ansible_host is not explicitly declared its value is set to inventory_hostname.

For example

shell > grep test_11 hosts
test_11
- hosts: test_11
  tasks:
    - debug:
        var: hostvars[inventory_hostname]['ansible_host']
    - debug:
        var: ansible_host

gives

TASK [debug] *************************************************************
ok: [test_11] => 
  hostvars[inventory_hostname]['ansible_host']: VARIABLE IS NOT DEFINED!

TASK [debug] *************************************************************
ok: [test_11] => 
  ansible_host: test_11

If you declare ansible_host, for example, in the inventory

shell > grep test_11 hosts
test_11 ansible_host=10.1.0.61

the same playbook gives

TASK [debug] *************************************************************
ok: [test_11] => 
  hostvars[inventory_hostname]['ansible_host']: 10.1.0.61

TASK [debug] *************************************************************
ok: [test_11] => 
  ansible_host: 10.1.0.61

ansible_host: The ip/name of the target host to use instead of inventory_hostname.


A quick solution to the problem might be defaulting to inventory_hostname, assuming ansible_host is not needed, i.e. inventory_hostname resolves. For example

{% for host in groups['all'] %}
  server {{ host }} {{ hostvars[host]['ansible_host']|default(host) }}
{% endfor %}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...