Commit 400873b5 authored by Pietsch, Martin's avatar Pietsch, Martin

convert with_-loops to loop-loops

parent 9d58e69e
......@@ -6,23 +6,25 @@
- set_fact:
dhcp_ranges: >-
{% set range = {} -%}
{% if item.value.dhcp is defined -%}
{% do range.update({item.key: {}}) -%}
{% if item.value.dhcp.ipv4 is defined -%}
{% do range[item.key].update({"ipv4": {"from": item.value.dhcp.ipv4.from, "to": item.value.dhcp.ipv4.to}}) -%}
{% if _netclass.value.dhcp is defined -%}
{% do range.update({_netclass.key: {}}) -%}
{% if _netclass.value.dhcp.ipv4 is defined -%}
{% do range[_netclass.key].update({"ipv4": {"from": _netclass.value.dhcp.ipv4.from, "to": _netclass.value.dhcp.ipv4.to}}) -%}
{% else -%}
{% do range[item.key].update({"ipv4": {"from": "0.0.0.0", "to": "0.0.0.0"}}) -%}
{% do range[_netclass.key].update({"ipv4": {"from": "0.0.0.0", "to": "0.0.0.0"}}) -%}
{% endif -%}
{% if item.value.dhcp.ipv6 is defined -%}
{% do range[item.key].update({"ipv6": {"from": item.value.dhcp.ipv6.from, "to": item.value.dhcp.ipv6.to}}) -%}
{% if _netclass.value.dhcp.ipv6 is defined -%}
{% do range[_netclass.key].update({"ipv6": {"from": _netclass.value.dhcp.ipv6.from, "to": _netclass.value.dhcp.ipv6.to}}) -%}
{% else -%}
{% do range[item.key].update({"ipv6": {"from": "::0", "to": "::0"}}) -%}
{% do range[_netclass.key].update({"ipv6": {"from": "::0", "to": "::0"}}) -%}
{% endif -%}
{% else -%}
{% do range.update({item.key: {"ipv4": {"from": "0.0.0.0", "to": "0.0.0.0"}, "ipv6": {"from": "::0", "to": "::0"}}}) -%}
{% do range.update({_netclass.key: {"ipv4": {"from": "0.0.0.0", "to": "0.0.0.0"}, "ipv6": {"from": "::0", "to": "::0"}}}) -%}
{% endif -%}
{{ dhcp_ranges | default({}) | combine(range) }}
with_dict: "{{ network.classes }}"
loop: "{{ network.classes | dict2items }}"
loop_control:
loop_var: _netclass
- stat:
path: "/etc/rc.conf"
......@@ -31,20 +33,24 @@
- name: set static IPv4 network interfaces
lineinfile:
dest: "/etc/rc.conf"
line: "{{ 'ifconfig_%s=\"inet %s netmask %s\"' | format(item.key, item.value.ipv4, '%s/%s' | format(item.value.ipv4, network.classes[item.value.class].ipv4.prefix) | ipaddr('netmask')) }}"
regexp: "{{ '^ifconfig_%s=' | format(item.key) }}"
line: "{{ 'ifconfig_%s=\"inet %s netmask %s\"' | format(_netdev.key, _netdev.value.ipv4, '%s/%s' | format(_netdev.value.ipv4, network.classes[_netdev.value.class].ipv4.prefix) | ipaddr('netmask')) }}"
regexp: "{{ '^ifconfig_%s=' | format(_netdev.key) }}"
create: yes
when: ((item.value.ipv4 | default(False) | ip_in_range('%s-%s' | format(dhcp_ranges[item.value.class].ipv4.from, dhcp_ranges[item.value.class].ipv4.to))) or (item.value.static | default(True)) == True) and item.value.ipv4 is defined
with_dict: "{{ network.devices }}"
when: ((_netdev.value.ipv4 | default(False) | ip_in_range('%s-%s' | format(dhcp_ranges[_netdev.value.class].ipv4.from, dhcp_ranges[_netdev.value.class].ipv4.to))) or (_netdev.value.static | default(True)) == True) and _netdev.value.ipv4 is defined
loop: "{{ network.devices | dict2items }}"
loop_control:
loop_var: _netdev
- name: set dynamic IPv4 network interfaces
lineinfile:
dest: "/etc/rc.conf"
line: "{{ 'ifconfig_%s=\"SYNCDHCP\"' | format(item.key) }}"
regexp: "{{ '^ifconfig_%s=' | format(item.key) }}"
line: "{{ 'ifconfig_%s=\"SYNCDHCP\"' | format(_netdev.key) }}"
regexp: "{{ '^ifconfig_%s=' | format(_netdev.key) }}"
create: yes
when: ((item.value.ipv4 | default(False) | ip_in_range('%s-%s' | format(dhcp_ranges[item.value.class].ipv4.from, dhcp_ranges[item.value.class].ipv4.to))) and (item.value.static | default(False)) == False) and item.value.ipv4 is defined
with_dict: "{{ network.devices }}"
when: ((_netdev.value.ipv4 | default(False) | ip_in_range('%s-%s' | format(dhcp_ranges[_netdev.value.class].ipv4.from, dhcp_ranges[_netdev.value.class].ipv4.to))) and (_netdev.value.static | default(False)) == False) and _netdev.value.ipv4 is defined
loop: "{{ network.devices | dict2items }}"
loop_control:
loop_var: _netdev
- name: set IPv4 default gateway
lineinfile:
......@@ -57,20 +63,24 @@
- name: set static IPv6 network interfaces
lineinfile:
dest: "/etc/rc.conf"
line: "{{ 'ifconfig_%s_ipv6=\"inet6 %s prefixlen %s\"' | format(item.key, item.value.ipv6, network.classes[item.value.class].ipv6.prefix) }}"
regexp: "{{ '^ifconfig_%s_ipv6=' | format(item.key) }}"
line: "{{ 'ifconfig_%s_ipv6=\"inet6 %s prefixlen %s\"' | format(_netdev.key, _netdev.value.ipv6, network.classes[_netdev.value.class].ipv6.prefix) }}"
regexp: "{{ '^ifconfig_%s_ipv6=' | format(_netdev.key) }}"
create: yes
when: (not (item.value.ipv6 | default(False) | ip_in_range('%s-%s' | format(dhcp_ranges[item.value.class].ipv6.from, dhcp_ranges[item.value.class].ipv6.to))) or (item.value.static | default(True)) == True) and item.value.ipv6 is defined
with_dict: "{{ network.devices }}"
when: (not (_netdev.value.ipv6 | default(False) | ip_in_range('%s-%s' | format(dhcp_ranges[_netdev.value.class].ipv6.from, dhcp_ranges[_netdev.value.class].ipv6.to))) or (_netdev.value.static | default(True)) == True) and _netdev.value.ipv6 is defined
loop: "{{ network.devices | dict2items }}"
loop_control:
loop_var: _netdev
- name: set dynamic IPv6 network interfaces
lineinfile:
dest: "/etc/rc.conf"
line: "{{ 'ifconfig_%s_ipv6=\"SYNCDHCP\"' | format(item.key) }}"
regexp: "{{ '^ifconfig_%s_ipv6=' | format(item.key) }}"
line: "{{ 'ifconfig_%s_ipv6=\"SYNCDHCP\"' | format(_netdev.key) }}"
regexp: "{{ '^ifconfig_%s_ipv6=' | format(_netdev.key) }}"
create: yes
when: ((item.value.ipv6 | default(False) | ip_in_range('%s-%s' | format(dhcp_ranges[item.value.class].ipv6.from, dhcp_ranges[item.value.class].ipv6.to))) or (item.value.static | default(False)) == False) and item.value.ipv6 is defined
with_dict: "{{ network.devices }}"
when: ((_netdev.value.ipv6 | default(False) | ip_in_range('%s-%s' | format(dhcp_ranges[_netdev.value.class].ipv6.from, dhcp_ranges[_netdev.value.class].ipv6.to))) or (_netdev.value.static | default(False)) == False) and _netdev.value.ipv6 is defined
loop: "{{ network.devices | dict2items }}"
loop_control:
loop_var: _netdev
- name: set IPv6 default gateway
lineinfile:
......@@ -84,9 +94,11 @@
natifs: []
- set_fact:
natifs: "{{ ([item.intdevice] + (natifs | default([]))) | unique() }}"
when: item.intdevice not in network.devices
with_items: "{{ network.nat | default([]) }}"
natifs: "{{ ([_natif.intdevice] + (natifs | default([]))) | unique() }}"
when: _natif.intdevice not in network.devices
loop: "{{ network.nat | default([]) }}"
loop_control:
loop_var: _natif
- block:
- name: enable cloned interfaces
......@@ -99,32 +111,38 @@
set_fact:
ipaddrs: >-
{% set dev = {} -%}
{% if item.intdevice in (ipaddrs | default({})) -%}
{% do dev.update({'ipv4': (ipaddrs[item.intdevice].ipv4 | default([])) + [item.ipv4]}) if item.ipv4 is defined else dev.update({'ipv4': (ipaddrs[item.intdevice].ipv4 | default([])) }) -%}
{% do dev.update({'ipv6': (ipaddrs[item.intdevice].ipv6 | default([])) + [item.ipv6]}) if item.ipv6 is defined else dev.update({'ipv6': (ipaddrs[item.intdevice].ipv6 | default([])) }) -%}
{% if _natif.intdevice in (ipaddrs | default({})) -%}
{% do dev.update({'ipv4': (ipaddrs[_natif.intdevice].ipv4 | default([])) + [_natif.ipv4]}) if _natif.ipv4 is defined else dev.update({'ipv4': (ipaddrs[_natif.intdevice].ipv4 | default([])) }) -%}
{% do dev.update({'ipv6': (ipaddrs[_natif.intdevice].ipv6 | default([])) + [_natif.ipv6]}) if _natif.ipv6 is defined else dev.update({'ipv6': (ipaddrs[_natif.intdevice].ipv6 | default([])) }) -%}
{% else -%}
{% do dev.update({'ipv4': [item.ipv4]}) if item.ipv4 is defined -%}
{% do dev.update({'ipv6': [item.ipv6]}) if item.ipv6 is defined -%}
{% do dev.update({'ipv4': [_natif.ipv4]}) if _natif.ipv4 is defined -%}
{% do dev.update({'ipv6': [_natif.ipv6]}) if _natif.ipv6 is defined -%}
{% endif -%}
{{ (ipaddrs | default({})) | combine({ item.intdevice : dev }) }}
when: item.intdevice in natifs
with_items: "{{ network.nat }}"
{{ (ipaddrs | default({})) | combine({ _natif.intdevice : dev }) }}
when: _natif.intdevice in natifs
loop: "{{ network.nat | default([]) }}"
loop_control:
loop_var: _natif
- name: configure IPv4 cloned interface
lineinfile:
path: /etc/rc.conf
regexp: "^{{ 'ipv4_addrs_%s=' | format(item.key) }}"
line: "{{ 'ipv4_addrs_%s=\"%s\"' | format(item.key, item.value.ipv4 | unique() | join(' ')) }}"
when: (item.value.ipv4 | default([]) | length()) > 0
with_dict: "{{ ipaddrs | default({}) }}"
regexp: "^{{ 'ipv4_addrs_%s=' | format(_natif.key) }}"
line: "{{ 'ipv4_addrs_%s=\"%s\"' | format(_natif.key, _natif.value.ipv4 | unique() | join(' ')) }}"
when: (_natif.value.ipv4 | default([]) | length()) > 0
loop: "{{ ipaddrs | default({}) | dict2items }}"
loop_control:
loop_var: _natif
- name: configure IPv6 cloned interface
lineinfile:
path: /etc/rc.conf
regexp: "^{{ 'ipv6_addrs_%s=' | format(item.key) }}"
line: "{{ 'ipv6_addrs_%s=\"%s\"' | format(item.key, item.value.ipv6 | unique() | join(' ')) }}"
when: (item.value.ipv6 | default([]) | length()) > 0
with_dict: "{{ ipaddrs | default({}) }}"
regexp: "^{{ 'ipv6_addrs_%s=' | format(_natif.key) }}"
line: "{{ 'ipv6_addrs_%s=\"%s\"' | format(_natif.key, _natif.value.ipv6 | unique() | join(' ')) }}"
when: (_natif.value.ipv6 | default([]) | length()) > 0
loop: "{{ ipaddrs | default({}) | dict2items }}"
loop_control:
loop_var: _natif
when: (natifs | length()) > 0
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment