Hi,

I'm trying to write all IPs from all hosts to fil
this is my template:

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

and this is playbook
---
- name: update /etc/hosts file dynamically
  hosts: all
  gather_facts: yes
  tasks:
    - name: update /etc/hosts
      template:
        src: templates/allIPs.j2
        dest: /tmp/allIPs


But when I run playbook and in template task I get AnsibleUndefinedVariable 
error...

fatal: [node1]: FAILED! => {"changed": false, "msg": 
"AnsibleUndefinedVariable: 'dict object' has no attribute 
'ansible_default_ipv4'"}
fatal: [node2]: FAILED! => {"changed": false, "msg": 
"AnsibleUndefinedVariable: 'dict object' has no attribute 
'ansible_default_ipv4'"}


when I run ad-hoc setup module I get what I want:

node1| SUCCESS => {
    "ansible_facts": {
        "ansible_default_ipv4": {
            "address": "172.31.116.92", 
            "alias": "ens5", 
            "broadcast": "172.31.127.255", 
            "gateway": "172.31.112.1", 
            "interface": "ens5", 
            "macaddress": "02:04:d5:9a:24:b4", 
            "mtu": 9001, 
            "netmask": "255.255.240.0", 
            "network": "172.31.112.0", 
            "type": "ether"
        },

Do you have any idea what could be a problem?

Dana ponedjeljak, 9. rujna 2019. u 11:54:33 UTC+2, korisnik 黄冠雄 napisao je:
>
> My ansible version is 2.5.
> In my playbook, one host needs to access other host's variable which I 
> defined in inventory file.
> I used {{ hostvars['groupname'].var_name }} and get an error, I also tried 
> to use {{ hostvars['groupname']['var_name'] }} which get an error also.
> So, I want to be clear how to use hostvars to access another host 
> variables defined in the inventory file?
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/080bfbf4-39c4-4212-a677-c1b1509f76c0%40googlegroups.com.

Reply via email to