Skip to content

Commit

Permalink
Merge pull request #24 from dresden-weekly/develop
Browse files Browse the repository at this point in the history
1.0 release
  • Loading branch information
arBmind committed Mar 30, 2016
2 parents f0cd6b9 + 9ee1f8a commit cfd1850
Show file tree
Hide file tree
Showing 10 changed files with 271 additions and 102 deletions.
80 changes: 63 additions & 17 deletions Readme.adoc → README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
:o: pass:normal[+[{nbsp}]+]
:x: pass:normal[+[✓]+]

Ansible network interface configuration
=======================================
[![Ansible Galaxy](https://img.shields.io/badge/Ansible%20Galaxy-dresden--weekly.network--interfaces-blue.svg)](https://galaxy.ansible.com/list#/roles/2766)

This is an Ansible role that manages network interface configuration as it is found on Debian/Ubuntu servers

Expand All @@ -21,17 +19,18 @@ none
Example Playbook
----------------

[source,yml]
----
```yml
- hosts: all
sudo: true
sudo_user: root
become: true
become_user: root

roles:
- role: dresden-weekly.network-interfaces
network_manage_devices: yes

network_interfaces:
- device: eth0
description: just a description for humans to understand
auto: true
family: inet
method: static
Expand All @@ -44,28 +43,75 @@ Example Playbook
- 8.8.4.4
subnets:
- 192.168.1.12/32

- device: eth1
description: simple dhcp client interface
auto: true
family: inet
method: dhcp
----

- device: vlan123
description: sample vlan interface using eth0 and tagged for VLAN 123.
method: static
address: 1.2.3.4
netmask: 24
broadcast: 1.2.3.255
vlan:
raw-device: eth0
up:
- route add default gw 1.2.3.254

- device: eth2
description: First bonding device
auto: true
family: inet
method: manual
bond:
master: bond0

- device: bond0
description: This bonding device only has one interface
allow:
- hotplug
family: inet
method: static
bond:
mode: active-backup
miimon: 100
slaves: eth2
address: 192.160.50.1
netmask: 255.255.255.0
dns_search: "localdomain"
up:
- ip route add 172.16.0.0/24 via 192.168.50.254 dev bond0
```
Changelog
---------
**0.2** *TODO*
**1.1** (*TODO*)
* [ ] open for your ideas, fixes and pull requests
**1.0** (Ansible 2 release) 30.03.2016
* {o} open for your ideas, fixes and pull requests
* [✓] compatible with Ansible 2.x
* [✓] support all hook aliases
* [✓] support for all allow stanzas
* [✓] full device restart control
* [✓] improved support for bonding
* [✓] one config file per device
**0.1** (first release) 01.02.2015
* {x} ipv6 & ipv4 support
* {x} support for multiple network devices
* {x} dhcp and static configuration
* {x} support for bridges
* {x} additional subnets and ips
* {x} custom hook scripts
* {x} remove old interfaces
* [✓] ipv6 & ipv4 support
* [✓] support for multiple network devices
* [✓] dhcp and static configuration
* [✓] support for bridges
* [✓] support for bonding
* [✓] additional subnets and ips
* [✓] custom hook scripts
* [✓] remove old interfaces
License
-------
Expand Down
45 changes: 41 additions & 4 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,68 @@
# If true, all additional scripts are deleted
network_manage_devices: yes

# Allow the network to restart
#
# This is only triggered, when the all interface file was changed
network_allow_service_restart: yes

# How should changed interfaces be treated?
#
# Options:
# service - restart the network service for the interface
# interface - down & up the interface
# nothing - do nothing
# * - undefined behavior
network_restart_method: service

# List of all network interface configurations
#
# For ipv6 you want to add an additional inet6 entry
network_interfaces:
- device: eth0
# auto & allow are only used for the first device entry
auto: true # enable on boot (default)
allow: [] # array of allow-[stanzas] eg. allow-hotplug

family: inet # network type eg. inet | inet6 (default)
method: dhcp # dhcp | static (default)
# examples for method 'static'
#description: 'a user description'
#address: 192.168.1.11
#network: 192.168.1.0
#netmask: 193.168.1.255
#broadcast: 192.168.1.255
#gateway: 192.168.1.1

# optional dns settings
#nameservers: ['8.8.8.8']
# optional bridge parameters
#bridge:
#dns_search: "domain.net" # appended dns search string

# optional additional subnets/ips
#subnets: ['192.168.123.0/24', '192.168.124.11/32']

bridge: {} # optional bridge parameters
# ports:
# stp:
# fd:
# maxwait:
# optional additional subnets/ips
#subnets: ['192.168.123.0/24', '192.168.124.11/32']
# waitport:

bond: {} # optional bonding parameters
# mode:
# miimon:
# master:
# slaves:
# lacp-rate:

# optional vlan settings
vlan: {}
# raw-device: 'eth0'

# inline hook scripts
pre-up: [] # pre-up script lines
up: [] # up script lines
post-up: [] # post-up script lines (alias for up)
pre-down: [] # pre-down script lines (alias for down)
down: [] # down script lines
post-down: [] # post-down script lines
30 changes: 29 additions & 1 deletion handlers/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
---
- name: restart
- name: network all interfaces changed
service:
name: "{{ network_service }}"
state: restarted
when: network_allow_service_restart
register: network_service_restart_result

- name: network restart interface service
# schema: network_configuration_result.results
# = [ { "item": [ <device>, [<config>, ...] ], ... }, ... ]
service:
name: "{{ network_service }}"
state: restarted
arguments: "INTERFACE={{ item.item.0 }}"
when: >
(network_service_restart_result is undefined or not network_service_restart_result.changed)
and ('service' == network_restart_method)
and item.changed
and item.item.1.0.auto | default(true)
with_items: "{{network_configuration_result.results | default([])}}"

- name: network restart interface command
include: 'exec_restart_command.yml'
vars:
device: "{{ item.item.0 }}"
configs: "{{ item.item.1 }}"
when: >
(network_service_restart_result is undefined or not network_service_restart_result.changed)
and (network_restart_method in network_interface_restart_commands)
and item.changed
and item.item.1.0.auto | default(true)
with_items: "{{network_configuration_result.results | default([])}}"
2 changes: 1 addition & 1 deletion meta/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ galaxy_info:
description: 'This is an Ansible role that manages network interface configuration as it is found on Debian/Ubuntu servers'
company: Dresden Weekly
license: MIT
min_ansible_version: 1.8
min_ansible_version: 2.0
platforms:
#- name: EL
# versions:
Expand Down
12 changes: 12 additions & 0 deletions tasks/all_interfaces.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
- name: create directory
file:
path: "{{ network_interface_path }}"
state: directory

- name: all interfaces
template:
src: all_interfaces.j2
dest: "{{ network_all_interfaces_path }}"
notify:
- network all interfaces changed
4 changes: 4 additions & 0 deletions tasks/exec_restart_command.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
- name: network restart interface command
shell: >
{{ network_interface_restart_commands[ network_restart_method ] }}
63 changes: 17 additions & 46 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,55 +1,26 @@
---
- name: create directory
file:
path: "{{ network_interface_path }}"
state: directory
when: network_interfaces

- name: all interfaces
template:
src: all_interfaces.j2
dest: "{{ network_all_interfaces_path }}"
notify: restart
when: network_interfaces
- name: ensure basic networking tools are installed
apt:
pkg: "{{ item }}"
state: present
update_cache: yes
cache_valid_time: 86400
with_items:
- vlan

- name: list network interfaces
command: "find {{ network_interface_path }} -type f"
changed_when: no
- include: all_interfaces.yml
when: network_interfaces
register: network_existing_files

- name: configurations
template:
src: device.j2
dest: "{{ network_interface_path }}/device-{{ item.device }}-{{ item.family | default('inet') }}"
with_items: network_interfaces
dest: "{{ network_interface_path }}/device-{{ item.0 }}"
with_items:
- "{{network_interfaces | default([]) | groupby('device') }}"
register: network_configuration_result
notify:
- network restart interface service
- network restart interface command

- name: configured files
set_fact:
network_configured_files: >
[{% for item in network_configuration_result.results | default([]) -%}
u"{{ item.dest | default(item.path) }}"
{{ '' if loop.last else ',' }}
{%- endfor %}]
- name: remove configurations
file:
dest: "{{ item }}"
state: absent
when: >
network_manage_devices
and (item not in network_configured_files)
with_items: network_existing_files.stdout_lines | default([])

- name: restart devices
service:
name: "{{ network_service }}"
state: restarted
arguments: "INTERFACE={{ item.0 }}"
when: >
(0 != item.1 | selectattr('changed') | list | length)
and ((0 != item.1 | selectattr('item.auto', 'undefined') | list | length)
or (true in item.1 | selectattr('item.auto') | list)
)
with_items: network_configuration_result.results | default([]) | groupby('item.device')
- include: manage_devices.yml
when: network_manage_devices
21 changes: 21 additions & 0 deletions tasks/manage_devices.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
- name: list existing files
command: "find {{ network_interface_path }} -type f"
changed_when: no
when: network_interfaces
register: network_existing_files

# depending on change the result stores dest or path
- name: configured files
set_fact:
network_configured_files: >
[{% for item in network_configuration_result.results | default([]) -%}
u"{{ item.dest | default(item.path) }}"
{{ '' if loop.last else ',' }}
{%- endfor %}]
- name: remove configurations
file:
dest: "{{ item }}"
state: absent
when: item not in network_configured_files
with_items: "{{network_existing_files.stdout_lines | default([])}}"
Loading

0 comments on commit cfd1850

Please sign in to comment.