Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

Ansible hostvars has no attribute 'ansible_nodename'

We have several playbooks that use hostvars[item]['ansible_nodename'] where item is a host alias. It usually works. But sometimes does not, giving the following error:

'dict object' has no attribute 'ansible_nodename` 

I printed the contents of hostvars and did not see this attribute there.

I could not find the documentation of hostvars to be able to see what we can safely use instead ansible_nodename. So, the questions are:

  • Can I safely replace ansible_nodename with ansible_host?
  • Where can I find a description of the contents of hostvars and algorithm of its creation?
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

But sometimes does not, giving the following error...

This can be because facts for those hosts are not gathered at that moment in time.

You may want to add empty play to gather facts for all hosts at the very top of your playbook:

- name: gather facts
  hosts: all

- name: balancer configuration
  hosts: balancer
  tasks:
    - name: generate configs
      template:
        src: conf.j2
        dest: "/conf/{{ hostvars[item]['ansible_nodename'] }}.conf"
      with_hostnames: nodes

Without 'gather facts' play, configuration task for balancer will fail because ansible_nodename is not gathered.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...