1
votes

I'm trying to create a simple script to provision the machines in my Vagrant setup.

I currently have a private network set up, where each node in the Vagrant file has something like:

node.vm.network "private_network", ip: "172.16.x.x"

And in my Ansible script, I originally used {{ ansible_eth2.ipv4.address }} to pick up the ip address in my playbook tasks.

When I recently swapped machines however, the Ansible variable was undefined because the new machine doesn't have a eth2 network interface. On the new machine, the private ip address set in the Vagrant file appears to be assigned to the eth1 interface.

In an attempt to get around this issue and make my Ansible playbooks hardware agnostic, I replaced all my references to {{ ansible_eth2.ipv4.address }} with {{ ansible_default_ipv4.address }}, but that retrieves the ip address at eth0 (10.0.x.x in my case).

How can I set either the Vagrant file to always add the ip address to whatever network interface Ansible will use for {{ ansible_default_ipv4.address }}?

Or failing that, is there anything I can do with Ansible (or even raw shellscript), to retrieve the private ip address?

1
Well, what have you already tried? Because on every ansible hostfacts that I've ever seen, it includes the addresses of all interfaces, and thus it is trivial to jinja2 filter out the 10.0.x.y one to get the remaining one(s)mdaniel
Instructions on that how to filter through the network interfaces would be helpful. For now though, I've used node.vm.network "private_network", ip: machine_ip, bridge: "eth1: Ethernet" in the Vagrant file to ensure that my private ip always falls on the eth1 interface. At least that's what I think that line of code is doing...Charlie

1 Answers

0
votes

At least one way, as there are likely several:

  - debug:
      verbosity: 0
      msg: |
          {%- for nic in ansible_interfaces -%}
          {%- for addr in (vars["ansible_"+nic]["ipv4"] | map(attribute="address") | list)
              if not (addr | regex_search('10(\.\d+){3}')) -%}
          nic {{ nic }} := {{ addr }}
          {%- endfor -%}
          {%- endfor -%}