Hi, I have the similar issue.

I have template file and I'm trying to write down IPs of all hosts to file 
on some node
So my template is:

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

{% endfor %} 

and playabook:
---
- 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 I get:
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'"}

also when I run ad-hoc setup I got this dict and its values:

   "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "172.31.116.92"
        ], 
        "ansible_all_ipv6_addresses": [
            "2a05:d018:85:e101:135a:c53d:f1e5:4c6", 
            "fe80::4:d5ff:fe9a:24b4"
        ], 
        "ansible_apparmor": {
            "status": "disabled"
        }, 
        "ansible_architecture": "x86_64", 
        "ansible_bios_date": "10/16/2017", 
        "ansible_bios_version": "1.0", 
        "ansible_cmdline": {
            "BOOT_IMAGE": "/boot/vmlinuz-3.10.0-957.12.1.el7.x86_64", 
            "LANG": "en_US.UTF-8", 
            "console": "ttyS0", 
            "crashkernel": "auto", 
            "ro": true, 
            "root": "UUID=0f790447-ebef-4ca0-b229-d0aa1985d57f", 
            "vconsole.font": "latarcyrheb-sun16", 
            "vconsole.keymap": "us"
        }, 
        "ansible_date_time": {
            "date": "2019-09-11", 
            "day": "11", 
            "epoch": "1568190515", 
            "hour": "08", 
            "iso8601": "2019-09-11T08:28:35Z", 
            "iso8601_basic": "20190911T082835217763", 
            "iso8601_basic_short": "20190911T082835", 
            "iso8601_micro": "2019-09-11T08:28:35.217841Z", 
            "minute": "28", 
            "month": "09", 
            "second": "35", 
            "time": "08:28:35", 
            "tz": "UTC", 
            "tz_offset": "+0000", 
            "weekday": "Wednesday", 
            "weekday_number": "3", 
            "weeknumber": "36", 
            "year": "2019"
        }, 
        "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"

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/0fe64b4a-fe0a-4695-95fa-5c76135879d3%40googlegroups.com.

Reply via email to