0

I have problem and i don't did i even start correctly.

i have hosts file that looks like

all:
  children:
    application1:
      children:
        application1-webserver:
          hosts:
            host1.domain.net:
            host2.domain.net:
        application1-database:
          hosts:
            dbhost1.domain.net:
    application2:
      children:
        application2-webserver:
          hosts:
            host3.domain.net:
            host4.domain.net:
        application1-database:
          hosts:
            dbhost2.domain.net:
    app-servers:
      hosts:
            host1.domain.net:
            host2.domain.net:
            host3.domain.net:
            host4.domain.net:

I have created template file. I know it is not pg_hba.conf, but it is no mater now, if i get IPs out it will be easy

{% for i in groups['app-servers'] %}
{{ hostvars[i]['ansible_default_ipv4_address'] }}
{% endfor %}

So maybe for time to time i have to run this script to create "new" enviroment, and i don't want to change IPs from app-servers manually. What i want is to get IP from FQDN.

Need this so i can limit access to db from network, just to those servers.

Thanks for help.

  • 2
    I'm not sure I understand what the problem is. Are you getting an error when you try to use that template? At first glance it looks okay. – larsks Nov 13 '19 at 21:26
  • Yes i'm getting error fatal: `FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'ansible_default_ipv4_address'"}` Running this against one test server i have. I have network access to all servers – dzecevic Nov 14 '19 at 07:27

2 Answers2

0

The variable holding the global info is ansible_default_ipv4. It is only available if you have gathered facts on your host (make sure you did not use gather_facts: false on your play).

It's a hash containing several keys among which address. As an example (obfuscated), this is what I get on my localhost:

$ ansible localhost -m setup -a gather_subset=network -a filter=*default_ipv4*
localhost | SUCCESS => {
    "ansible_facts": {
        "ansible_default_ipv4": {
            "address": "x.y.z.a",
            "alias": "interface",
            "broadcast": "x.y.z.255",
            "gateway": "x.y.z..1",
            "interface": "interface",
            "macaddress": "xx:xx:xx:xx:xx:xx",
            "mtu": 1500,
            "netmask": "255.255.255.0",
            "network": "x.y.z..0",
            "type": "ether"
        }
    },
    "changed": false
}

So the name of the var you are looking for in your template is ansible_default_ip4.address

Your full fixed template:

{% for i in groups['app-servers'] %}
{{ hostvars[i].ansible_default_ipv4.address }}
{% endfor %}

Possible alternative notations (mixing dot and array notation):

{{ hostvars[i]['ansible_default_ipv4']['address'] }}
{{ hostvars[i].ansible_default_ipv4['address'] }}
etc.
Zeitounator
  • 38,476
  • 7
  • 53
  • 66
0

So i got it. It is little complicated.

Playbook looks like this:

---
- hosts: app-servers
  tasks:
    - ping:

- hosts: dbhost1.domain.net
  tasks:
    - include_role:
        name: test-for
...

Hosts file like above:

all:
  children:
    application1:
      children:
        application1-webserver:
          hosts:
            host1.domain.net:
            host2.domain.net:
        application1-database:
          hosts:
            dbhost1.domain.net:
    application2:
      children:
        application2-webserver:
          hosts:
            host3.domain.net:
            host4.domain.net:
        application1-database:
          hosts:
            dbhost2.domain.net:
    app-servers:
      hosts:
            host1.domain.net:
            host2.domain.net:
            host3.domain.net:
            host4.domain.net:

Only after i ping hosts i get IPs i need

Then in template:

{% for i in groups['app-servers'] %}
{{ hostvars[i].ansible_default_ipv4.address }}
{% endfor %}

And it works.

Thanks