Skip to content

Commit cfd1850

Browse files
committed
Merge pull request #24 from dresden-weekly/develop
1.0 release
2 parents f0cd6b9 + 9ee1f8a commit cfd1850

File tree

10 files changed

+271
-102
lines changed

10 files changed

+271
-102
lines changed

Readme.adoc renamed to README.md

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
:o: pass:normal[+[{nbsp}]+]
2-
:x: pass:normal[+[✓]+]
3-
41
Ansible network interface configuration
52
=======================================
3+
[![Ansible Galaxy](https://img.shields.io/badge/Ansible%20Galaxy-dresden--weekly.network--interfaces-blue.svg)](https://galaxy.ansible.com/list#/roles/2766)
64

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

@@ -21,17 +19,18 @@ none
2119
Example Playbook
2220
----------------
2321

24-
[source,yml]
25-
----
22+
```yml
2623
- hosts: all
27-
sudo: true
28-
sudo_user: root
24+
become: true
25+
become_user: root
2926

3027
roles:
3128
- role: dresden-weekly.network-interfaces
3229
network_manage_devices: yes
30+
3331
network_interfaces:
3432
- device: eth0
33+
description: just a description for humans to understand
3534
auto: true
3635
family: inet
3736
method: static
@@ -44,28 +43,75 @@ Example Playbook
4443
- 8.8.4.4
4544
subnets:
4645
- 192.168.1.12/32
46+
4747
- device: eth1
48+
description: simple dhcp client interface
4849
auto: true
4950
family: inet
5051
method: dhcp
51-
----
52+
53+
- device: vlan123
54+
description: sample vlan interface using eth0 and tagged for VLAN 123.
55+
method: static
56+
address: 1.2.3.4
57+
netmask: 24
58+
broadcast: 1.2.3.255
59+
vlan:
60+
raw-device: eth0
61+
up:
62+
- route add default gw 1.2.3.254
63+
64+
- device: eth2
65+
description: First bonding device
66+
auto: true
67+
family: inet
68+
method: manual
69+
bond:
70+
master: bond0
71+
72+
- device: bond0
73+
description: This bonding device only has one interface
74+
allow:
75+
- hotplug
76+
family: inet
77+
method: static
78+
bond:
79+
mode: active-backup
80+
miimon: 100
81+
slaves: eth2
82+
address: 192.160.50.1
83+
netmask: 255.255.255.0
84+
dns_search: "localdomain"
85+
up:
86+
- ip route add 172.16.0.0/24 via 192.168.50.254 dev bond0
87+
```
5288
5389
Changelog
5490
---------
5591
56-
**0.2** *TODO*
92+
**1.1** (*TODO*)
93+
94+
* [ ] open for your ideas, fixes and pull requests
95+
96+
**1.0** (Ansible 2 release) 30.03.2016
5797
58-
* {o} open for your ideas, fixes and pull requests
98+
* [✓] compatible with Ansible 2.x
99+
* [✓] support all hook aliases
100+
* [✓] support for all allow stanzas
101+
* [✓] full device restart control
102+
* [✓] improved support for bonding
103+
* [✓] one config file per device
59104
60105
**0.1** (first release) 01.02.2015
61106
62-
* {x} ipv6 & ipv4 support
63-
* {x} support for multiple network devices
64-
* {x} dhcp and static configuration
65-
* {x} support for bridges
66-
* {x} additional subnets and ips
67-
* {x} custom hook scripts
68-
* {x} remove old interfaces
107+
* [✓] ipv6 & ipv4 support
108+
* [✓] support for multiple network devices
109+
* [✓] dhcp and static configuration
110+
* [✓] support for bridges
111+
* [✓] support for bonding
112+
* [✓] additional subnets and ips
113+
* [✓] custom hook scripts
114+
* [✓] remove old interfaces
69115
70116
License
71117
-------

defaults/main.yml

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,68 @@
44
# If true, all additional scripts are deleted
55
network_manage_devices: yes
66

7+
# Allow the network to restart
8+
#
9+
# This is only triggered, when the all interface file was changed
10+
network_allow_service_restart: yes
11+
12+
# How should changed interfaces be treated?
13+
#
14+
# Options:
15+
# service - restart the network service for the interface
16+
# interface - down & up the interface
17+
# nothing - do nothing
18+
# * - undefined behavior
19+
network_restart_method: service
20+
721
# List of all network interface configurations
822
#
923
# For ipv6 you want to add an additional inet6 entry
1024
network_interfaces:
1125
- device: eth0
26+
# auto & allow are only used for the first device entry
1227
auto: true # enable on boot (default)
28+
allow: [] # array of allow-[stanzas] eg. allow-hotplug
29+
1330
family: inet # network type eg. inet | inet6 (default)
1431
method: dhcp # dhcp | static (default)
1532
# examples for method 'static'
33+
#description: 'a user description'
1634
#address: 192.168.1.11
1735
#network: 192.168.1.0
1836
#netmask: 193.168.1.255
1937
#broadcast: 192.168.1.255
2038
#gateway: 192.168.1.1
39+
40+
# optional dns settings
2141
#nameservers: ['8.8.8.8']
22-
# optional bridge parameters
23-
#bridge:
42+
#dns_search: "domain.net" # appended dns search string
43+
44+
# optional additional subnets/ips
45+
#subnets: ['192.168.123.0/24', '192.168.124.11/32']
46+
47+
bridge: {} # optional bridge parameters
2448
# ports:
2549
# stp:
2650
# fd:
2751
# maxwait:
28-
# optional additional subnets/ips
29-
#subnets: ['192.168.123.0/24', '192.168.124.11/32']
52+
# waitport:
53+
54+
bond: {} # optional bonding parameters
55+
# mode:
56+
# miimon:
57+
# master:
58+
# slaves:
59+
# lacp-rate:
60+
61+
# optional vlan settings
62+
vlan: {}
63+
# raw-device: 'eth0'
64+
3065
# inline hook scripts
3166
pre-up: [] # pre-up script lines
3267
up: [] # up script lines
68+
post-up: [] # post-up script lines (alias for up)
69+
pre-down: [] # pre-down script lines (alias for down)
3370
down: [] # down script lines
3471
post-down: [] # post-down script lines

handlers/main.yml

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
11
---
2-
- name: restart
2+
- name: network all interfaces changed
33
service:
44
name: "{{ network_service }}"
55
state: restarted
6+
when: network_allow_service_restart
7+
register: network_service_restart_result
8+
9+
- name: network restart interface service
10+
# schema: network_configuration_result.results
11+
# = [ { "item": [ <device>, [<config>, ...] ], ... }, ... ]
12+
service:
13+
name: "{{ network_service }}"
14+
state: restarted
15+
arguments: "INTERFACE={{ item.item.0 }}"
16+
when: >
17+
(network_service_restart_result is undefined or not network_service_restart_result.changed)
18+
and ('service' == network_restart_method)
19+
and item.changed
20+
and item.item.1.0.auto | default(true)
21+
with_items: "{{network_configuration_result.results | default([])}}"
22+
23+
- name: network restart interface command
24+
include: 'exec_restart_command.yml'
25+
vars:
26+
device: "{{ item.item.0 }}"
27+
configs: "{{ item.item.1 }}"
28+
when: >
29+
(network_service_restart_result is undefined or not network_service_restart_result.changed)
30+
and (network_restart_method in network_interface_restart_commands)
31+
and item.changed
32+
and item.item.1.0.auto | default(true)
33+
with_items: "{{network_configuration_result.results | default([])}}"

meta/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ galaxy_info:
33
description: 'This is an Ansible role that manages network interface configuration as it is found on Debian/Ubuntu servers'
44
company: Dresden Weekly
55
license: MIT
6-
min_ansible_version: 1.8
6+
min_ansible_version: 2.0
77
platforms:
88
#- name: EL
99
# versions:

tasks/all_interfaces.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
- name: create directory
3+
file:
4+
path: "{{ network_interface_path }}"
5+
state: directory
6+
7+
- name: all interfaces
8+
template:
9+
src: all_interfaces.j2
10+
dest: "{{ network_all_interfaces_path }}"
11+
notify:
12+
- network all interfaces changed

tasks/exec_restart_command.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
- name: network restart interface command
3+
shell: >
4+
{{ network_interface_restart_commands[ network_restart_method ] }}

tasks/main.yml

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,26 @@
11
---
2-
- name: create directory
3-
file:
4-
path: "{{ network_interface_path }}"
5-
state: directory
6-
when: network_interfaces
7-
8-
- name: all interfaces
9-
template:
10-
src: all_interfaces.j2
11-
dest: "{{ network_all_interfaces_path }}"
12-
notify: restart
13-
when: network_interfaces
2+
- name: ensure basic networking tools are installed
3+
apt:
4+
pkg: "{{ item }}"
5+
state: present
6+
update_cache: yes
7+
cache_valid_time: 86400
8+
with_items:
9+
- vlan
1410

15-
- name: list network interfaces
16-
command: "find {{ network_interface_path }} -type f"
17-
changed_when: no
11+
- include: all_interfaces.yml
1812
when: network_interfaces
19-
register: network_existing_files
2013

2114
- name: configurations
2215
template:
2316
src: device.j2
24-
dest: "{{ network_interface_path }}/device-{{ item.device }}-{{ item.family | default('inet') }}"
25-
with_items: network_interfaces
17+
dest: "{{ network_interface_path }}/device-{{ item.0 }}"
18+
with_items:
19+
- "{{network_interfaces | default([]) | groupby('device') }}"
2620
register: network_configuration_result
21+
notify:
22+
- network restart interface service
23+
- network restart interface command
2724

28-
- name: configured files
29-
set_fact:
30-
network_configured_files: >
31-
[{% for item in network_configuration_result.results | default([]) -%}
32-
u"{{ item.dest | default(item.path) }}"
33-
{{ '' if loop.last else ',' }}
34-
{%- endfor %}]
35-
36-
- name: remove configurations
37-
file:
38-
dest: "{{ item }}"
39-
state: absent
40-
when: >
41-
network_manage_devices
42-
and (item not in network_configured_files)
43-
with_items: network_existing_files.stdout_lines | default([])
44-
45-
- name: restart devices
46-
service:
47-
name: "{{ network_service }}"
48-
state: restarted
49-
arguments: "INTERFACE={{ item.0 }}"
50-
when: >
51-
(0 != item.1 | selectattr('changed') | list | length)
52-
and ((0 != item.1 | selectattr('item.auto', 'undefined') | list | length)
53-
or (true in item.1 | selectattr('item.auto') | list)
54-
)
55-
with_items: network_configuration_result.results | default([]) | groupby('item.device')
25+
- include: manage_devices.yml
26+
when: network_manage_devices

tasks/manage_devices.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
- name: list existing files
2+
command: "find {{ network_interface_path }} -type f"
3+
changed_when: no
4+
when: network_interfaces
5+
register: network_existing_files
6+
7+
# depending on change the result stores dest or path
8+
- name: configured files
9+
set_fact:
10+
network_configured_files: >
11+
[{% for item in network_configuration_result.results | default([]) -%}
12+
u"{{ item.dest | default(item.path) }}"
13+
{{ '' if loop.last else ',' }}
14+
{%- endfor %}]
15+
16+
- name: remove configurations
17+
file:
18+
dest: "{{ item }}"
19+
state: absent
20+
when: item not in network_configured_files
21+
with_items: "{{network_existing_files.stdout_lines | default([])}}"

0 commit comments

Comments
 (0)