Deploy network interfaces from LDAP

Signed-off-by: Yohann D'ANELLO <ynerant@crans.org>
This commit is contained in:
2021-06-04 17:02:54 +02:00
parent 50cb4cfc75
commit f1ac6f269b
18 changed files with 367 additions and 0 deletions

View File

@ -0,0 +1,28 @@
---
- name: Install vlan support
apt:
update_cache: true
name: vlan
state: present
register: apt_result
retries: 3
until: apt_result is succeeded
- name: Deploy default interfaces config
template:
src: network/interfaces.j2
dest: /etc/network/interfaces
mode: 0644
- name: Remove cloud-init interface configuration
file:
path: /etc/network/interfaces.d/50-cloud-init
state: absent
- name: Deploy interfaces config
template:
src: "network/interfaces.d/ifalias.j2"
dest: "/etc/network/interfaces.d/{{ '%02d' | format(item.id) }}-{{ item.name | replace('_', '-') }}"
mode: 0644
when: item.name in interfaces
loop: "{{ network_interfaces.vlan }}"

View File

@ -0,0 +1,55 @@
{{ ansible_header | comment }}
{% set vlan_name = (item.name | replace('_', '-')) %}
{% set subnet_network = (query('ldap', 'network', vlan_name) | ipaddr('network')) %}
{% set subnet_netmask = (query('ldap', 'network', vlan_name) | ipaddr('netmask')) %}
{% set ips = query('ldap', 'ip', ansible_hostname, vlan_name) %}
{% if (ips | ipv4 | length) > 0 %}
auto {{ interfaces[item.name] }}
iface {{ interfaces[item.name] }} inet static
{% for ip in (ips | ipv4) %}
address {{ ip }}
{% endfor %}
network {{ subnet_network }}
netmask {{ subnet_netmask }}
{% if item.gateway is defined %}
gateway {{ item.gateway }}
{% endif %}
{% if item.metric is defined %}
metric {{ item.metric }}
{% endif %}
{% if item.dns is defined %}
dns-nameservers {{ item.dns }}
{% endif %}
{% if vlan_name == 'srv' %}
dns-search ynerant.fr
{% else %}
dns-search {{ vlan_name }}.ynerant.fr
{% endif %}
up /sbin/ip link set $IFACE alias {{ vlan_name }}
{% if ansible_local.interfaces.sup_if_4 is defined %}
{% if interfaces[item.name] in ansible_local.interfaces.sup_if_4 %}
{% for line in ansible_local.interfaces.sup_if_4[interfaces[item.name]] %}
{{ line }}
{% endfor %}
{% endif %}
{% endif %}
{% endif %}
{% if (ips | ipv6 | length) > 0 %}
iface {{ interfaces[item.name] }} inet6 static
{% for ip in (ips | ipv6) %}
address {{ ip }}/64
{% endfor %}
{% if item.gateway_v6 is defined %}
gateway {{ item.gateway_v6 }}
{% endif %}
accept_ra 0
{% if ansible_local.interfaces.sup_if_6 is defined %}
{% if interfaces[item.name] in ansible_local.interfaces.sup_if_6 %}
{% for line in ansible_local.interfaces.sup_if_6[interfaces[item.name]] %}
{{ line }}
{% endfor %}
{% endif %}
{% endif %}
{% endif %}

View File

@ -0,0 +1,10 @@
{{ ansible_header | comment }}
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback

View File

@ -8,3 +8,12 @@
register: apt_result
retries: 3
until: apt_result is succeeded
- name: Remove cloud-init
apt:
name: cloud-init
state: absent
purge: true
register: apt_result
retries: 3
until: apt_result is succeeded