Skip to content

Commit 4a90ec1

Browse files
committed
fixed network management
1 parent 27b02d8 commit 4a90ec1

File tree

3 files changed

+57
-34
lines changed

3 files changed

+57
-34
lines changed

handlers/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
22
- name: restart
33
service:
4-
name: {{ network_service }}
4+
name: "{{ network_service }}"
55
state: restarted

tasks/main.yml

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,53 @@
33
file:
44
path: "{{ network_interface_path }}"
55
state: directory
6-
when: network_interfaces | length > 0
6+
when: network_interfaces
77

88
- name: all interfaces
99
template:
1010
src: all_interfaces.j2
1111
dest: "{{ network_all_interfaces_path }}"
1212
notify: restart
13-
when: network_interfaces | length > 0
13+
when: network_interfaces
1414

1515
- name: list network interfaces
16-
command: "find {{ network_interface_path }} -name 'device-*'"
16+
command: "find {{ network_interface_path }} -type f"
1717
changed_when: no
18+
when: network_interfaces
1819
register: network_existing_files
1920

2021
- name: configurations
2122
template:
2223
src: device.j2
23-
dest: "{{ network_interface_path }}/device-{{ item.device }}-{{ item.family }}"
24+
dest: "{{ network_interface_path }}/device-{{ item.device }}-{{ item.family | default('inet') }}"
2425
with_items: network_interfaces
2526
register: network_configuration_result
2627

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+
2736
- name: remove configurations
2837
file:
2938
dest: "{{ item }}"
3039
state: absent
3140
when: >
32-
network_manage_devices and
33-
item in (network_configuration_result.results | map(attribute='path'))
34-
with_items: network_existing_files
41+
network_manage_devices
42+
and (item not in network_configured_files)
43+
with_items: network_existing_files.stdout_lines | default([])
3544

3645
- name: restart devices
3746
service:
3847
name: "{{ network_service }}"
3948
state: restarted
40-
arguments: "{{ item.grouper }}"
41-
when: true in (item.list | map(attribute='changed'))
42-
with_items: network_configuration_result.results | groupby('item.device')
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')

templates/device.j2

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,47 @@
11
# {{ ansible_managed }}
2-
{% if item.auto | default(true, true) -%}
2+
3+
{% if item.auto | default(true) %}
34
auto {{ item.device }}
4-
{% endif -%}
5+
{% endif %}
56
iface {{ item.device }} {{ item.family | default('inet', true) }} {{ item.method | default('dhcp', true) }}
6-
{% for key in ['address', 'network', 'netmask', 'broadcast', 'gateway'] -%}
7-
{% if key in item -%}
7+
{% for key in ['address', 'network', 'netmask', 'broadcast', 'gateway'] %}
8+
{% if key in item %}
89
{{ key }} {{ item[key] }}
9-
{% endif -%}
10-
{% endfor -%}
10+
{% endif %}
11+
{% endfor %}
12+
13+
{%- if item.nameservers is defined %}
1114

12-
{% if item.nameservers is defined -%}
1315
dns-nameservers {{ item.nameservers | join(' ') }}
14-
{% endif -%}
16+
{% endif %}
17+
18+
{%- if item.bridge is defined -%}
1519

16-
{% if item.bridge is defined -%}
17-
{% for key in ['ports', 'stp', 'fd', 'maxwait'] -%}
20+
# bridge settings
21+
{% for key in ['ports', 'stp', 'fd', 'maxwait'] %}
1822
{% if key in item.bridge -%}
1923
bridge_{{ key }} {{ item.bridge[key] }}
20-
{% endif -%}
21-
{% endfor -%}
22-
{% endif -%}
24+
{% endif %}
25+
{% endfor %}
26+
{% endif %}
2327

24-
{% if item.subnets is defined -%}
25-
{% for subnet in item.subnets -%}
28+
{%- if item.subnets is defined %}
29+
30+
# additional subnets
31+
{% for subnet in item.subnets %}
2632
up /sbin/ip addr add {{ subnet }} dev {{ item.device }}
2733
down /sbin/ip addr del {{ subnet }} dev {{ item.device }}
28-
{% endfor -%}
29-
{% endif -%}
34+
{% endfor %}
35+
{% endif %}
36+
37+
{%- if ['pre-up', 'up', 'down', 'post-down'] | intersect(item.keys()) %}
3038

31-
{% for key in ['pre-up', 'up', 'down', 'post-down'] -%}
32-
{% if key in item -%}
33-
{% for value in item[key] -%}
39+
# hook scripts
40+
{% for key in ['pre-up', 'up', 'down', 'post-down'] %}
41+
{% if key in item %}
42+
{% for value in item[key] %}
3443
{{ key }} {{ value }}
35-
{% endfor -%}
36-
{% endif -%}
37-
{% endfor -%}
44+
{% endfor %}
45+
{% endif %}
46+
{% endfor %}
47+
{% endif %}

0 commit comments

Comments
 (0)