Bladeren bron

simplify role

master
bovenliggende
commit
85798b9944
10 gewijzigde bestanden met toevoegingen van 49 en 258 verwijderingen
  1. +6
    -0
      tasks/main.yaml
  2. +0
    -55
      tasks/main.yml
  3. +22
    -0
      tasks/network-interfaces.yaml
  4. +0
    -0
      tasks/setup.yaml
  5. +0
    -9
      templates/netifs.json.j2
  6. +0
    -80
      templates/network-interface-debian.j2
  7. +0
    -96
      templates/network-interfaces-alpine.j2
  8. +0
    -11
      templates/network-interfaces-debian.j2
  9. +21
    -0
      templates/profiles/hetzner_server.j2
  10. +0
    -7
      templates/systemd-network.link.j2

+ 6
- 0
tasks/main.yaml Bestand weergeven

@@ -0,0 +1,6 @@
---

- import_tasks: setup.yaml
when: not root_target_directory is defined

- import_tasks: network-interfaces.yaml

+ 0
- 55
tasks/main.yml Bestand weergeven

@@ -1,55 +0,0 @@
---

- include_tasks: setup_{{ansible_os_family}}.yml
when: not root_target_directory is defined

- name: set netifs
set_fact:
_netifs: "{{ lookup('template','netifs.json.j2') }}"

- name: /etc/network directory
file:
path: "{{ root_target_directory | default('') }}/etc/network"
state: directory

- name: debian network interfaces configuration
template:
src: network-interfaces-debian.j2
dest: "{{ root_target_directory | default('') }}/etc/network/interfaces"
when:
- ( root_target_dist | default('debian') ) == "debian"

- name: alpine network interfaces configuration
template:
src: network-interfaces-alpine.j2
dest: "{{ root_target_directory | default('') }}/etc/network/interfaces"
when:
- ( root_target_dist | default('debian') ) == "alpine"

- name: /etc/network/interfaces.d directory
file:
path: "{{ root_target_directory | default('') }}/etc/network/interfaces.d"
state: directory
when:
- ( root_target_dist | default('debian') ) == "debian"

- name: debian network interface configuration
template:
src: network-interface-debian.j2
dest: "{{ root_target_directory | default('') }}/etc/network/interfaces.d/{{ item }}"
with_items: "{{ _netifs }}"
when:
- ( root_target_dist | default('debian') ) == "debian"

- name: systemd network dir
file:
path: "{{ root_target_directory | default('') }}/etc/systemd/network"
state: directory

- name: systemd network interface names
template:
src: systemd-network.link.j2
dest: "{{ root_target_directory | default('') }}/etc/systemd/network/10-{{ item }}.link"
with_items: "{{ _netifs }}"
when:
- ( root_target_dist | default('debian') ) == "debian"

+ 22
- 0
tasks/network-interfaces.yaml Bestand weergeven

@@ -0,0 +1,22 @@
---

- name: /etc/network directory
file:
path: "{{ root_target_directory | default('') }}/etc/network"
state: directory

- name: network interfaces configuration
template:
src: profiles/{{network_interfaces_profile}}.j2
dest: "{{ root_target_directory | default('') }}/etc/network/interfaces"

- name: /etc/network/interfaces.d directory
file:
path: "{{ root_target_directory | default('') }}/etc/network/interfaces.d"
state: directory

# - name: debian network interface configuration
# template:
# src: network-interface.j2
# dest: "{{ root_target_directory | default('') }}/etc/network/interfaces.d/{{ item }}"
# with_items: "{{ _netifs }}"

tasks/setup_Debian.yml → tasks/setup.yaml Bestand weergeven


+ 0
- 9
templates/netifs.json.j2 Bestand weergeven

@@ -1,9 +0,0 @@
[
{% if netifs is defined %}
{% for netif in netifs %}
{% if netifs[netif].type == "ethernet" or netifs[netif].type == "virt" or netifs[netif].type == "virt_ptp" or netifs[netif].type == "virt_bridge" or netifs[netif].type == "bridge" %}
"{{netif}}",
{% endif %}
{% endfor %}
{% endif %}
]

+ 0
- 80
templates/network-interface-debian.j2 Bestand weergeven

@@ -1,80 +0,0 @@
{{ ansible_managed | comment }}
{% set netif = item %}

### netif {{netif}}
{% set dev = netifs[netif].device | default( 'enx' + netifs[netif].mac | regex_replace(':') ) %}
auto {{dev}}
allow-hotplug {{dev}}

{% if netifs[netif].type == "virt_ptp" %}
# virt_pointopoint -> {{virt_host}} {{netifs[netif].virt_host_netif}}

# ipv4
{% for ip in netifs[netif] | json_query("ips[].ip") | ipv4("address") %}
iface {{ dev }} inet static
address {{ ip }}
netmask 32
{% if loop.index == 1 %}
{% set gateway4 = hostvars[virt_host].netifs[netifs[netif].virt_host_netif] | json_query("ips[].ip") | ipv4("address") | first %}
pointopoint {{ gateway4 }}
gateway {{ gateway4 }}
{% endif %}
{% endfor %}

# ipv6
{% for ip in netifs[netif] | json_query("ips[].ip") | ipv6("address") %}
iface {{ dev }} inet6 static
address {{ ip }}
netmask 128
{% if loop.index == 1 %}
gateway {{ hostvars[virt_host].netifs[netifs[netif].virt_host_netif].ll6 }}
{% endif %}
{% endfor %}

{% else %}

{% if netifs[netif].type == "bridge" %}
# bridge
iface {{ dev }} inet manual
{% if netifs[netif].devices is defined %}
bridge_ports {{ netifs[netif].devices | join(" ") }}
{% else %}
bridge_ports none
{% endif %}
{% endif %}

{% for ip in netifs[netif].ips %}
{% if ip.ip | ipv6 %}
# ipv6 {{ip.ip}}
iface {{ dev }} inet6 static
address {{ ip.ip | ipv6('address') }}
netmask {{ ip.ip | ipv6('prefix') }}
{% endif %}
{% if ip.ip | ipv4 %}
# ipv4 {{ip.ip}}
iface {{ dev }} inet static
address {{ ip.ip | ipv4('address') }}
network {{ ip.ip | ipv4('network') }}
netmask {{ ip.ip | ipv4('prefix') }}
broadcast {{ ip.ip | ipv4('broadcast') }}
{% if loop.index == 1 and netifs[netif].routes is defined %}
{% set default_route = netifs[netif].routes | json_query("[?to=='default']") | first %}
gateway {{default_route.via}}
{% endif %}
{% endif %}
{% endfor %}

{% endif %}

# dns resolvers
iface {{ dev }} inet manual
{% if netifs[netif].dns_resolvers is defined %}
{% for nameserver in netifs[netif].dns_resolvers %}
dns-nameserver {{ nameserver }}
{% endfor %}
{% endif %}
{% if netifs[netif].dns_search is defined%}
{% for search in netifs[netif].dns_search %}
dns-search {{ search }}
{% endfor %}
{% endif %}

+ 0
- 96
templates/network-interfaces-alpine.j2 Bestand weergeven

@@ -1,96 +0,0 @@
{{ ansible_managed | comment }}
{% if netifs is defined %}
{% for netif in netifs %}
{% if netifs[netif].type == "ethernet" or netifs[netif].type == "virt" %}

### netif {{netif}}
{# TODO list virt_pci_ids of all netifs and pick eth number by index of list #}
{% set dev = 'eth'+(netifs[netif].virt_pci_id|string) %}
auto {{dev}}

{% if netifs[netif].type == "virt" and hostvars[virt_host].netifs[netifs[netif].virt_host_netif].type == "virt_pointopoint" %}
# virt_pointopoint -> {{virt_host}} {{netifs[netif].virt_host_netif}}
{% set ips_ipv4 = netifs[netif] | json_query("ips[].ip") | ipv4("address") %}
{% set ips_ipv4_first = ips_ipv4 | first %}
{% set ips_ipv4 = ips_ipv4 | difference(ips_ipv4_first) %}
{% set virt_pointopoint_ips_ipv4 = hostvars[virt_host].netifs[netifs[netif].virt_host_netif] | json_query("ips[].ip") | ipv4("address") %}
{% set virt_pointopoint_ips_ipv4_first = virt_pointopoint_ips_ipv4 | first %}
{% set virt_pointopoint_ips_ipv4 = virt_pointopoint_ips_ipv4 | difference(virt_pointopoint_ips_ipv4_first) %}
{% set ips_ipv6 = netifs[netif] | json_query("ips[].ip") | ipv6("address") %}
{% set ips_ipv6_first = ips_ipv6 | first %}
{% set ips_ipv6 = ips_ipv6 | difference(ips_ipv6_first) %}
{% set virt_pointopoint_ips_ipv6 = hostvars[virt_host].netifs[netifs[netif].virt_host_netif] | json_query("ips[].ip") | ipv6("address") %}
{% set virt_pointopoint_ips_ipv6_first = virt_pointopoint_ips_ipv6 | first %}
{% set virt_pointopoint_ips_ipv6 = virt_pointopoint_ips_ipv6 | difference(virt_pointopoint_ips_ipv6_first) %}
iface {{ dev }} inet manual
post-up /sbin/ip link set dev {{ dev }} up
pre-down /sbin/ip link set dev {{ dev }} down
# ipv4
post-up /sbin/ip -family inet address add {{ ips_ipv4_first }} peer {{ virt_pointopoint_ips_ipv4_first }} dev {{ dev }}
pre-down /sbin/ip -family inet address del {{ ips_ipv4_first }} peer {{ virt_pointopoint_ips_ipv4_first }} dev {{ dev }}
post-up /sbin/ip -family inet route add default dev {{ dev }} via {{ virt_pointopoint_ips_ipv4_first }}
pre-down /sbin/ip -family inet route del default dev {{ dev }} via {{ virt_pointopoint_ips_ipv4_first }}
{% for ptpip in virt_pointopoint_ips_ipv4 %}
post-up /sbin/ip -family inet route add {{ ptpip }}/32 dev {{ dev }} via {{ virt_pointopoint_ips_ipv4_first }}
pre-down /sbin/ip -family inet route del {{ ptpip }}/32 dev {{ dev }} via {{ virt_pointopoint_ips_ipv4_first }}
{% endfor %}
{% for ip in ips_ipv4 %}
post-up /sbin/ip -family inet address add {{ ip }}/32 dev {{ dev }}
pre-down /sbin/ip -family inet address del {{ ip }}/32 dev {{ dev }}
{% endfor %}
# ipv6
post-up /sbin/ip -family inet6 address add {{ ips_ipv6_first }}/128 dev {{ dev }}
pre-down /sbin/ip -family inet6 address del {{ ips_ipv6_first }}/128 dev {{ dev }}
post-up /sbin/ip -family inet6 route add {{ virt_pointopoint_ips_ipv6_first }} dev {{ dev }}
post-up /sbin/ip -family inet6 route add default via {{ virt_pointopoint_ips_ipv6_first }} dev {{ dev }}
pre-down /sbin/ip -family inet6 route del default via {{ virt_pointopoint_ips_ipv6_first }} dev {{ dev }}
pre-down /sbin/ip -family inet6 route del {{ virt_pointopoint_ips_ipv6_first }} dev {{ dev }}
{% for ptpip in virt_pointopoint_ips_ipv6 %}
post-up /sbin/ip -family inet6 route add {{ ptpip }} dev {{ dev }} via {{ virt_pointopoint_ips_ipv6_first }}
pre-down /sbin/ip -family inet6 route del {{ ptpip }} dev {{ dev }} via {{ virt_pointopoint_ips_ipv6_first }}
{% endfor %}
{% for ip in ips_ipv6 %}
post-up /sbin/ip -family inet6 address add {{ ip }}/128 dev {{ dev }}
pre-down /sbin/ip -family inet6 address del {{ ip }}/128 dev {{ dev }}
{% endfor %}

{% else %}

{% for ip in netifs[netif].ips %}
{% if ip.ip | ipv6 %}
# ipv6 {{ip.ip}}
iface {{ dev }} inet6 static
address {{ ip.ip | ipv6('address') }}
netmask {{ ip.ip | ipv6('netmask') }}
{% if netifs[netif].dns_resolvers is defined and netifs[netif].dns_resolvers | ipv6 %}
dns-nameservers {{ netifs[netif].dns_resolvers | ipv6 | join(" ") }}
{% endif %}
{% if netifs[netif].dns_search is defined %}
dns-search {{ netifs[netif].dns_search | join(" ") }}
{% endif %}
{% endif %}
{% if ip.ip | ipv4 %}
# ipv4 {{ip.ip}}
iface {{ dev }} inet static
address {{ ip.ip | ipv4('address') }}
network {{ ip.ip | ipv4('network') }}
netmask {{ ip.ip | ipv4('prefix') }}
broadcast {{ ip.ip | ipv4('broadcast') }}
{% if loop.index == 1 and netifs[netif].routes is defined %}
{% set default_route = netifs[netif].routes | json_query("[?to=='default']") | first %}
gateway {{default_route.via}}
{% endif %}
{% if netifs[netif].dns_resolvers is defined and netifs[netif].dns_resolvers | ipv4 %}
dns-nameservers {{ netifs[netif].dns_resolvers | ipv4 | join(" ") }}
{% endif %}
{% if netifs[netif].dns_search is defined %}
dns-search {{ netifs[netif].dns_search | join(" ") }}
{% endif %}
{% endif %}
{% endfor %}

{% endif %}

{% endif %}
{% endfor %}
{% endif %}

+ 0
- 11
templates/network-interfaces-debian.j2 Bestand weergeven

@@ -1,11 +0,0 @@
# interfaces(5) file used by ifup(8) and ifdown(8)

{{ ansible_managed | comment }}

{% if not (root_target_dist is defined and root_target_dist == "alpine") %}
auto lo
iface lo inet loopback
{% endif %}

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

+ 21
- 0
templates/profiles/hetzner_server.j2 Bestand weergeven

@@ -0,0 +1,21 @@
{{ ansible_managed | comment }}

# interfaces(5) file used by ifup(8) and ifdown(8)

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address {{hetzner_server_main_ip}}
netmask 32
gateway {{hetzner_server_gateway}}
pointopoint {{hetzner_server_gateway}}

iface eth0 inet6 static
address {{hetzner_server_main_ipv6}}
netmask 128
gateway fe80::1

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

+ 0
- 7
templates/systemd-network.link.j2 Bestand weergeven

@@ -1,7 +0,0 @@
{{ ansible_managed | comment }}
{% set netif = item %}
{% set dev = netifs[netif].device | default( 'enx' + netifs[netif].mac | regex_replace(':') ) %}
[Match]
MACAddress={{netifs[netif].mac}}
[Link]
Name={{dev}}

Laden…
Annuleren
Opslaan