Ansible: How to get the interface name of the host machine where ansible connect from?

ansible get list of network interfaces
ansible get hostname of remote host
ansible get host ip from inventory
ansible-connection: network_cli example
ansible get ip address of another host
ansible get ip address of all hosts
ansible_default_ipv4' is undefined

I would like to determine the interface ipv6 address of the host, where ansible connects from and use it as variable in my playbook. I know how to get the ipv6 address from the facts:

{{ ansible_facts['ens192']['ipv6'][0]['address'] }}

My only problem is that the interface might be something else but ens192, because I have hosts with multiple interfaces. I always need that interface's address where ansible connect to host, which might be ensX

If you could provide me a solution for IPv4, it is good enough for me. :) Please note that default_ipv4_address does not work for me, because the default interface is not always where the ansible connection is coming.


I think it will be good if I could get the IPv6 address where ansible connects to and get it's variable somehow. So if ansible connects to host1.something.local and it has an IPv6 address: 2001:1:2:3::4 get that IPv6 address as variable and use it in playbook.

It looks like you've got a solution that works, but for what it's worth you could also just query your routing tables for that information. The ip route get command will show you both the interface name and source address used to connect to another host. For example:

$ ip route get 2607:f8b0:4006:800::200e
2607:f8b0:4006:800::200e from :: via 2001:470:1f06:46f::1 dev he-ipv6 proto static src 2001:470:1f06:46f::2 metric 675 pref medium

From the above output, we see that a connection to 2607:f8b0:4006:800::200e will use interface he-ipv6 and source address 2001:470:1f06:46f::2.

You could use this in a playbook like this:

- hosts: targethost
    - name: get local address
      delegate_to: localhost
      command: >-
        ip route get {{ hostvars['targethost'].ansible_default_ipv6.address }}
      register: route

    - set_fact:
        local_address: "{{ dict(route_parts|zip(route_parts[1:])).src }}"
        local_interface: "{{ dict(route_parts|zip(route_parts[1:])).dev }}"
        route_parts: "{{ route.stdout.splitlines()[0].split()[1:] }}"

    - debug:
          - "local address: {{ local_address }}"
          - "local interface: {{ local_interface }}"

This should work with both ipv4 and ipv6 addresses.

Ansible Network Examples — Ansible Documentation, For more information on ansible_ssh_ options see Connecting to hosts: Certain network platforms, such as Arista EOS and Cisco IOS, have the Ansible facts modules gather system information 'facts' that are available to the rest of your playbook. name: Run Vyos command vyos_command: commands: show interface� So let us learn how ansible get ip address of the remote host. Method1: Get the IP used by Ansible master to connect. In this method, we are going to use the IP address used by Ansible master to connect to the Remote Host. Sometimes, we would use the public IP to connect to the remote host in such cases we would want to use this method.

Okey, I think I found it:

{{ hostvars[inventory_hostname]['ansible_env'].SSH_CONNECTION.split(' ')[2] }}

This will get the address where ansible connects to. If IPv6 is preferred, then it will be an IPv6 address. Otherwise it will be an IPv4 address.

net_interface – Manage Interface on network devices — Ansible , Interface link status. enabled. -. Configure interface link status. mtu. -. Maximum size of transmit packet. name. - / required. Name of the Interface. purge. -. Default � By default, Ansible represents what machines it manages using a very simple INI file that puts all of your managed machines in groups of your own choosing. To add new machines, there is no additional SSL signing server involved, so there's never any hassle deciding why a particular machine didn’t get linked up due to obscure NTP or DNS issues.

1.This is the most simple way to get the default ip address which the default route points to

"{{ ansible_default_ipv6.address }}"

As for variable ansible_default_ipv6, you can get more infomation.

such as interface name, gateway, mac address and so on.

2.another workaround method

You can get all ipv6 address of the host from this variable

"{{ ansible_all_ipv6_addresses }}"

And according to the ip address, you can distinguish the ip address which is in same 'network segment' of ansible host

Tips: replace ipv6 with ipv4, also works fine

ansible get ip address of current host or target, How to get the remote target server IP address in ansible playbook using network interface name eth0, enp0s3 etc and SSH connection information are AWS EC2 machines. as this would return the private IP of the host. By default Ansible uses SFTP to transfer files. If the machine or device you want to manage does not support SFTP, you can switch to SCP mode in Configuring Ansible. The files are placed in a temporary directory and executed from there. If you need privilege escalation (sudo and similar) to run a command, pass the become flags:

How to enumerate network interfaces in Ansible, On our network we connect the lowest numbered interface to the LAN and the highest to name: define traditional ethernet facts set_fact: ansible_eth: "{% set ansible_eth current machine and builds a list of the hostvars[ inventory_hostname]['ansible_' + Some hints to get an interface when we know some information: ansible_connection: Ansible uses the ansible-connection setting to determine how to connect to a remote device. When working with Ansible Networking, set this to network_cli so Ansible treats the remote node as a network device with a limited execution environment.

Automating Network Devices with Ansible, Ansible is a great automation tool for system and network engineers, with 3 hosts: site_a 4 connection: local 5 6 tasks: 7 - name: Get Interface� An Ansible pattern can refer to a single host, an IP address, an inventory group, a set of groups, or all hosts in your inventory. Patterns are highly flexible - you can exclude or require subsets of hosts, use wildcards or regular expressions, and more. Ansible executes on all inventory hosts included in the pattern.

avinetworks/ansible-role-network_interface: Ansible Role , All the servers should have their network interfaces configured and routed updated. - hosts: all roles: - role: network. This role can also optionally add network� Once Powershell has been upgraded to at least version 3.0, the final step is for the WinRM service to be configured so that Ansible can connect to it. There are two main components of the WinRM service that governs how Ansible can interface with the Windows host: the listener and the service configuration settings.

  • A bit complicated, but I accept the solution because it is the exact answer for my question. Could be usefull later.
  • This will not work. I did a try at the frist time, but ansible_default_ipvx_address will bring me up that netowrk's address, where the default route is set to. This isn't always the address where ansible connects to. (think the situation where is a local network and ansible connects to there)