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

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...