An ansible role for configuring different network interfaces
Switch branches/tags
Nothing to show
Clone or download
markgoddard Merge pull request #39 from avadhanij/issues/37
Added support for IPv6 address and route configuration
Latest commit fd7fc25 Jun 19, 2018

README.md

Ansible Interfaces Role Build Status

An ansible role for configuring different network interfaces.

WARNING: This role can be dangerous to use. If you lose network connectivity to your target host by incorrectly configuring your networking, you may be unable to recover without physical access to the machine.

  • Ethernet interfaces
  • Bridge interfaces
  • Bonded interfaces
  • Network routes
  • IP routing tables
  • IP routing rules

Role Variables

The variables that can be passed to this role and a brief description about them are as follows:

# The list of route tables to be defined
interfaces_route_tables: []

# The list of ethernet interfaces to be added to the system
interfaces_ether_interfaces: []

# The list of bridge interfaces to be added to the system
interfaces_bridge_interfaces: []

# The list of bonded interfaces to be added to the system
interfaces_bond_interfaces: []

Note: The values for the list are listed in the examples below.

  1. Configure eth1 and eth2 on a host with a static IP and a dhcp IP. Also define static routes and a gateway.
- hosts: myhost
  roles:
    - role: MichaelRigart.interfaces
      interfaces_ether_interfaces:
       - device: eth1
         bootproto: static
         address: 192.168.1.150
         netmask: 255.255.255.0
         gateway: 192.168.1.1
         dnsnameservers: 192.0.2.1 192.0.2.2
         dnssearch: example.com
         mtu: 9000
         route:
          - network: 192.168.200.0
            netmask: 255.255.255.0
            gateway: 192.168.1.1
          - network: 192.168.100.0
            netmask: 255.255.255.0
            gateway: 192.168.1.1
       - device: eth2
         bootproto: dhcp
  1. Configure a bridge interface with multiple NICs added to the bridge.
- hosts: myhost
  roles:
    - role: MichaelRigart.interfaces
      interfaces_bridge_interfaces:
       -  device: br1
          type: bridge
          address: 192.168.1.150
          netmask: 255.255.255.0
          bootproto: static
          stp: "on"
          mtu: 1500
          ports: [eth1, eth2]

Note: Routes can also be added for this interface in the same way routes are added for ethernet interfaces.

  1. Configure a bond interface with an "active-backup" slave configuration.
- hosts: myhost
  roles:
    - role: MichaelRigart.interfaces
      interfaces_bond_interfaces:
        - device: bond0
          mtu: 9000
          address: 192.168.1.150
          netmask: 255.255.255.0
          bootproto: static
          bond_mode: active-backup
          bond_miimon: 100
          bond_slaves: [eth1, eth2]
          route:
          - network: 192.168.222.0
            netmask: 255.255.255.0
            gateway: 192.168.1.1
  1. Configure a bonded interface with "802.3ad" as the bonding mode and IP address obtained via DHCP.
- hosts: myhost
  roles:
    - role: MichaelRigart.interfaces
      interfaces_bond_interfaces:
        - device: bond0
          bootproto: dhcp
          bond_mode: 802.3ad
          bond_miimon: 100
          bond_downdelay: 200
          bond_updelay: 200
          bond_lacp_rate: 1
          bond_xmit_hash_policy: layer3+4
          bond_slaves: [eth1, eth2]
  1. Configure a routing table myroutetable, and an Ethernet interface eth1 with an IP routing rule that defines when to use the routing table. It also configures an IP route on the interface for the myroutetable routing table.
- hosts: myhost
  roles:
    - role: MichaelRigart.interfaces
      interfaces_route_tables:
       - name: myroutetable
         id: 42
      interfaces_ether_interfaces:
       - device: eth1
         bootproto: static
         address: 192.168.1.150
         netmask: 255.255.255.0
         dnsnameservers: 192.0.2.1 192.0.2.2
         dnssearch: example.com
         mtu: 9000
         route:
          - network: 192.168.200.0
            netmask: 255.255.255.0
            gateway: 192.168.1.1
            table: myroutetable
         rules:
          - from 192.168.200.0/24 table myroutetable
  1. Configure a hot-pluggable Wifi interface wlan0 with a static IP and a wpa_supplicant configuration. Also configure eth0 with a dhcp IP.
- hosts: myhost
  roles:
    - role: MichaelRigart.interfaces
      interfaces_ether_interfaces:
        - device: wlan0
          allowclass: allow-hotplug
          bootproto: static
          address: 192.168.1.150
          netmask: 255.255.255.0
          network: 192.168.1.0
          broadcast: 192.168.1.255
          gateway: 192.168.1.1
          dnsnameservers: 192.168.1.1
          wpaconf: /etc/wpa_supplicant/wpa_supplicant.conf
        - device: eth0
          bootproto: dhcp
  1. Configure a static IPv4 address, route along with an IPv6 address and route on an interface.
- hosts: myhost
  roles:
    - role: MichaelRigart.interfaces
      interfaces_ether_interfaces:
        - device: ens160
          bootproto: static
          address: 10.99.99.2
          netmask: 255.255.255.0
          route:
            - network: 10.99.98.0
              netmask: 255.255.255.0
              gateway: 10.99.99.1
          ip6:
            address: fd49:f9f5:ccb4:2acd::ac18:1002
            prefix: 120
            route:
              - network: 64:FF9B::/96
                gateway: fd49:f9f5:ccb4:2acd::ac18:1001
  1. All the above examples show how to configure a single host, The below example shows how to define your network configurations for all your machines.

Assume your host inventory is as follows:

/etc/ansible/hosts

[dc1]
host1
host2

Describe your network configuration for each host in host vars:

host_vars/host1

interfaces_ether_interfaces:
  - device: eth1
    bootproto: static
    address: 192.168.1.150
    netmask: 255.255.255.0
    gateway: 192.168.1.1
    route:
     - network: 192.168.200.0
       netmask: 255.255.255.0
       gateway: 192.168.1.1
interfaces_bond_interfaces:
  - device: bond0
    mtu: 9000
    bootproto: dhcp
    bond_mode: 802.3ad
    bond_miimon: 100
    bond_slaves: [eth2, eth3]

host_vars/host2

interfaces_ether_interfaces:
  - device: eth0
    bootproto: static
    address: 192.168.1.150
    netmask: 255.255.255.0
    gateway: 192.168.1.1

Create a playbook which applies this role to all hosts as shown below, and run the playbook. All the servers should have their network interfaces configured and routes updated.

- hosts: all
  roles:
    - role: MichaelRigart.interfaces

Note: Ansible needs network connectivity throughout the playbook process, you may need to have a control interface that you do not modify using this method so that Ansible has a stable connection to configure the target systems.

Example Playbook

- hosts: servers
  roles:
     - { role: MichaelRigart.interfaces, become: true }

License

GPLv3

Author Information

Michaël Rigart michael@netronix.be