network snippets

Network Interface Aliasing in Linux

Tagged network, ifconfig, alias, linux  Languages bash

Sometimes you need to connect to two different networks from the same physical network card, this is when network interface aliasing comes in handy:

sudo ifconfig eth0:home 192.168.0.1
sudo ifconfig eth0:work 10.0.0.1

You now have two virtual NICs and can access both network segments.

See Network Interface Aliasing in Linux for more information.

Ansible: How to find the IP address of a specific network interface when there are multiple network interfaces

Tagged ansible, ip address, network  Languages yaml

Here’s how to find the IP address of a specific network interface that matches a given CIDR:

- hosts: all
  gather_facts: false
  tasks:
    - set_fact:
        prod_ip_addr: "{{ item }}"
      when: "item | ipaddr('192.168.10.0/24')"
      with_items: "{{ ansible_all_ipv4_addresses }}"
    - debug: var=prod_ip_addr

This is useful for example when you have a separate management network interface.

It’s almost as easy as trying to explain what the script is doing in plain English.