1

(New to ansible) I'm running ansible all -m setup on a handful of hosts to produce JSON output for our inventory system. Some of our ansible remotes are in different subnets (DMZs for instance). I'd like to include the LAN IP associated with these hosts in the ansible JSON output. How can I do this?

UPDATE: To clarify, I would like the 192.168.1.1 ip address to be in the fact output somehow. The ansible_all_ipv4_addresses field and ansible_default_ipv4 in the output contains 10.10.10.1 which isn't a helpful IP address to people looking at the inventory from a LAN perspective.

192.168.1.0/24       192.168.1.0/24
    LAN ----------------FIREWALL
                         |     |
                         eth0  eth1
                         |     |
                         |     +-------DMZ1---------+
                         |                         |
       host1.org.net   > + eth0:1=192.168.1.1      + 10.10.10.1  > host1.org.net
                         |                         |
       host2.org.net   > + eth0:2=192.168.1.2      + 10.10.10.2  > host2.org.net
Server Fault
  • 3,714
  • 12
  • 54
  • 89
  • They should already be in those facts if they're local interfaces.... Are you looking for LOM IPs?? – Jacob Evans Mar 08 '18 at 20:44
  • Right, the IP addresses are already in the facts. – Michael Hampton Mar 08 '18 at 20:53
  • I don't think I'm being clear about what I'm needing or I've completely missed something obvious. I've added a diagram and will go look through the JSON output again. Perhaps my grep was on a line break or something. – Server Fault Mar 08 '18 at 21:06
  • Ansible uses `ip -4 route get 8.8.8.8` to determine the default IPv4 address. This is reasonable on a properly configured host. As for your other interface, make sure you haven't configured them [ancient style with aliases](https://wiki.debian.org/NetworkConfiguration#Multiple_IP_addresses_on_one_Interface). If you have, switch your config to the modern method. – Michael Hampton Mar 08 '18 at 21:17
  • Thanks for the explanation, but I think ansible is working fine. The management host (where ansible is launched from) is on the LAN. I'd like to know if there's a way to include a fact from that end of the connection, not from the endpoint. The endpoint, in my case, has no way to find out what it's IP would be in the LAN DNS view. – Server Fault Mar 08 '18 at 21:29
  • The setup module returns information gathered from the managed system. If you want to set additional facts or modify them, you would probably need to create a playbook. In a playbook you should be able to go up with a way to gather facts, and then add more based on information from your inventory. Another option might be to set custom facts on the machines. – Zoredache Mar 10 '18 at 00:09
  • @zoredache will a "command: " from a playbook execute on the controller, or on the remote? If I can get the host IP on the controller, then use a playbook "command" to dump the output into /etc/ansible/facts.d/lan_ip then that may work. I would just need to "get_facts" after placing the file. Is that what you had in mind? I've tried a test run simplying doing "command: ifconfig > /tmp/lan_ip" and I get an error. I need to read up more on playbooks – Server Fault Mar 11 '18 at 19:04

1 Answers1

0

One solution seems to be to run a playbook to populate a file in /etc/ansible/facts.d/whatever.fact using the "{{inventory_hostname}}" variable. When setup runs, it will include the whatever.fact in the JSON output. It's here in case it helps anyone else: How can I log inventory_hostname to a file on the remote using a Playbook?

Server Fault
  • 3,714
  • 12
  • 54
  • 89