Vendor Galaxy Roles and Collections
Some checks failed
/ Ansible Lint (push) Failing after 5m45s
/ Ansible Lint (pull_request) Failing after 4m59s

This commit is contained in:
Stefan Bethke 2026-02-06 22:07:16 +01:00
commit 2aed20393f
8 changed files with 387444 additions and 2 deletions

View file

@ -0,0 +1,19 @@
debops.networkd - Manage network interfaces using systemd-networkd
Copyright (C) 2023 Maciej Delmanowski <drybjed@gmail.com>
Copyright (C) 2023 DebOps <http://debops.org/>
SPDX-License-Identifier: GPL-3.0-only
This Ansible role is part of DebOps.
DebOps is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3, as
published by the Free Software Foundation.
DebOps is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with DebOps. If not, see https://www.gnu.org/licenses/.

View file

@ -0,0 +1,252 @@
---
# .. vim: foldmarker=[[[,]]]:foldmethod=marker
# .. Copyright (C) 2023 Maciej Delmanowski <drybjed@gmail.com>
# .. Copyright (C) 2023 DebOps <https://debops.org/>
# .. SPDX-License-Identifier: GPL-3.0-only
# .. _networkd__ref_defaults:
# debops.networkd default variables
# =================================
# .. contents:: Sections
# :local:
#
# .. include:: ../../../../includes/global.rst
# General options [[[
# -------------------
# .. envvar:: networkd__enabled [[[
#
# Enable or disable management of the :command:`systemd-networkd` service using
# DebOps. If the parameter is set to ``False``, the role will not touch service
# configuration.
networkd__enabled: '{{ True
if (ansible_service_mgr == "systemd")
else False }}'
# ]]]
# .. envvar:: networkd__unattended_restart [[[
#
# On the first installation, :command:`systemd-networkd` cannot be restarted
# unattended because changes in network configuration might make a host
# unreachable and stop the playbook execution. When the service is configured,
# it should be fine to restart on any changes.
networkd__unattended_restart: '{{ True
if ((ansible_local.networkd.state | d()) == "enabled")
else False }}'
# ]]]
# .. envvar:: networkd__deploy_state [[[
#
# This variable controls if the :command:`systemd-networkd` main configuration
# file is managed on the host (``present``) or not (``absent``, default). If
# deployment state is disabled, :command:`systemd-networkd` will use the
# configuration provided with the OS package. Configuration of the specific
# service units is not affected by this variable.
networkd__deploy_state: 'absent'
# ]]]
# .. envvar:: networkd__version [[[
#
# Specify the version of the :command:`systemd-networkd` daemon installed on
# the host. By default this variable is defined using Ansible local facts and
# can be used to alter configuration depending on the version of the service.
networkd__version: '{{ ansible_local.networkd.version | d("0") }}'
# ]]]
# ]]]
# The :command:`systemd-networkd` daemon configuration [[[
# --------------------------------------------------------
# These variables define the contents of the ``[Network]`` section in the
# :file:`/etc/systemd/networkd.conf` configuration file. Check the
# :man:`networkd.conf(5)` manual page for more information about the
# configuration options, and :ref:`networkd__ref_configuration` for details
# about the configuration of the role itself.
#
# By default the configuration is not applied on the hosts, you need to set
# :envvar:`networkd__deploy_state` variable to ``present`` to deploy the
# configuration.
# .. envvar:: networkd__default_configuration [[[
#
# List of the default configuration options defined by the role.
networkd__default_configuration:
- name: 'SpeedMeter'
value: False
state: 'init'
- name: 'SpeedMeterIntervalSec'
value: '10sec'
state: 'init'
- name: 'ManageForeignRoutes'
value: True
state: 'init'
# ]]]
# .. envvar:: networkd__configuration [[[
#
# List of the configuration options which should be present on all hosts in the
# Ansible inventory.
networkd__configuration: []
# ]]]
# .. envvar:: networkd__group_configuration [[[
#
# List of the configuration options which should be present on hosts in
# a specific Ansible inventory group.
networkd__group_configuration: []
# ]]]
# .. envvar:: networkd__host_configuration [[[
#
# List of the configuration options which should be present on specific hosts
# in the Ansible inventory.
networkd__host_configuration: []
# ]]]
# .. envvar:: networkd__combined_configuration [[[
#
# Variable which combines all configuration lists and is used in the role tasks
# and templates.
networkd__combined_configuration: '{{ networkd__default_configuration
+ networkd__configuration
+ networkd__group_configuration
+ networkd__host_configuration }}'
# ]]]
# ]]]
# The :command:`systemd-networkd` DHCP configuration [[[
# ------------------------------------------------------
# These variables define configuration of the ``[DHCP]`` section in the
# :file:`/etc/systemd/networkd.conf` configuration file. Check the
# :man:`networkd.conf(5)` manual page for more information about the
# configuration options, and :ref:`networkd__ref_configuration` for details
# about the configuration of the role itself.
#
# By default the configuration is not applied on the hosts, you need to set
# :envvar:`networkd__deploy_state` variable to ``present`` to deploy the
# configuration.
# .. envvar:: networkd__dhcp_default_configuration [[[
#
# List of the default DHCP configuration options for the user instances,
# defined by the role.
networkd__dhcp_default_configuration:
- name: 'DUIDType'
value: 'vendor'
state: 'init'
- name: 'DUIDRawData'
value: ''
state: 'init'
# ]]]
# .. envvar:: networkd__dhcp_configuration [[[
#
# List of the DHCP configuration options which should be present on all hosts
# in the Ansible inventory.
networkd__dhcp_configuration: []
# ]]]
# .. envvar:: networkd__dhcp_group_configuration [[[
#
# List of the DHCP configuration options which should be present on hosts in
# a specific Ansible inventory group.
networkd__dhcp_group_configuration: []
# ]]]
# .. envvar:: networkd__dhcp_host_configuration [[[
#
# List of the DHCP configuration options which should be present on specific
# hosts in the Ansible inventory.
networkd__dhcp_host_configuration: []
# ]]]
# .. envvar:: networkd__dhcp_combined_configuration [[[
#
# Variable which combines all DHCP configuration lists and is used in role
# tasks and templates.
networkd__dhcp_combined_configuration: '{{ networkd__dhcp_default_configuration
+ networkd__dhcp_configuration
+ networkd__dhcp_group_configuration
+ networkd__dhcp_host_configuration }}'
# ]]]
# ]]]
# The :command:`systemd-networkd` configuration units [[[
# -------------------------------------------------------
# These variables can be used to manage :command:`systemd-networkd`
# :file:`*.network`, :file:`*.netdev` and :file:`*.link` files located in the
# :file:`/etc/systemd/network/` directory. Check the :man:`systemd.network(5)`,
# :man:`systemd.netdev(5)` and :file:`systemd.link(5)` manual pages for more
# information about files themselves, and :ref:`networkd__ref_units` for
# details about configuring units using this role.
# .. envvar:: networkd__default_units [[[
#
# List of the default :command:`systemd-networkd` units defined by the role.
networkd__default_units:
- name: 'wired-dhcp.network'
comment: 'Configure any wired Ethernet interface via DHCP'
raw: |
[Match]
Name=e*
[Network]
DHCP=yes
MulticastDNS=yes
LLDP=yes
EmitLLDP=yes
[DHCPv4]
UseDomains=true
state: 'present'
# ]]]
# .. envvar:: networkd__units [[[
#
# List of :command:`systemd-networkd` units which should be present on all
# hosts in the Ansible inventory.
networkd__units: []
# ]]]
# .. envvar:: networkd__group_units [[[
#
# List of :command:`systemd-networkd` units which should be present on hosts in
# a specific Ansible inventory group.
networkd__group_units: []
# ]]]
# .. envvar:: networkd__host_units [[[
#
# List of :command:`systemd-networkd` units which should be present on specific
# hosts in the Ansible inventory.
networkd__host_units: []
# ]]]
# .. envvar:: networkd__dependent_units [[[
#
# List of :command:`systemd-networkd` units which are defined by other Ansible
# roles using dependent role variables.
networkd__dependent_units: []
# ]]]
# .. envvar:: networkd__combined_units [[[
#
# Variable which combines all of the :command:`systemd-networkd` unit lists and
# is used in role tasks and templates.
networkd__combined_units: '{{ networkd__default_units
+ networkd__dependent_units
+ networkd__units
+ networkd__group_units
+ networkd__host_units }}'
# ]]]
# ]]]

View file

@ -0,0 +1,10 @@
---
# Copyright (C) 2023 Maciej Delmanowski <drybjed@gmail.com>
# Copyright (C) 2023 DebOps <https://debops.org/>
# SPDX-License-Identifier: GPL-3.0-only
- name: Restart systemd-networkd service
ansible.builtin.systemd:
name: 'systemd-networkd.service'
state: 'restarted'
when: (ansible_service_mgr == 'systemd' and networkd__unattended_restart | bool)

View file

@ -0,0 +1,32 @@
---
# Copyright (C) 2023 Maciej Delmanowski <drybjed@gmail.com>
# Copyright (C) 2023 DebOps <http://debops.org/>
# SPDX-License-Identifier: GPL-3.0-only
# Ensure that custom Ansible plugins and modules included in the main DebOps
# collection are available to roles in other collections.
collections: [ 'debops.debops' ]
dependencies: []
galaxy_info:
author: 'Maciej Delmanowski'
description: 'Manage network interfaces using systemd-networkd'
company: 'DebOps'
license: 'GPL-3.0-only'
min_ansible_version: '2.9.0'
platforms:
- name: 'Ubuntu'
versions: [ 'all' ]
- name: 'Debian'
versions: [ 'all' ]
galaxy_tags:
- system
- systemd
- network
- networking

View file

@ -0,0 +1,102 @@
---
# Copyright (C) 2023 Maciej Delmanowski <drybjed@gmail.com>
# Copyright (C) 2023 DebOps <https://debops.org/>
# SPDX-License-Identifier: GPL-3.0-only
- name: Import DebOps global handlers
ansible.builtin.import_role:
name: 'global_handlers'
- name: Make sure that Ansible local facts directory exists
ansible.builtin.file:
path: '/etc/ansible/facts.d'
state: 'directory'
mode: '0755'
- name: Save networkd local facts
ansible.builtin.template:
src: 'etc/ansible/facts.d/networkd.fact.j2'
dest: '/etc/ansible/facts.d/networkd.fact'
mode: '0755'
notify: [ 'Refresh host facts' ]
tags: [ 'meta::facts' ]
- name: Update Ansible facts if they were modified
ansible.builtin.meta: 'flush_handlers'
# Enable the service after checking the facts for the first time, to capture
# the state change and not restart the service on the first run
- name: Ensure that systemd-networkd service is enabled
ansible.builtin.systemd:
name: 'systemd-networkd.service'
enabled: True
when: networkd__enabled | bool
- name: Remove systemd-networkd configuuration if requested
ansible.builtin.file:
path: '/etc/systemd/networkd.conf.d/ansible.conf'
state: 'absent'
notify: [ 'Restart systemd-networkd service' ]
when: networkd__enabled | bool and networkd__deploy_state == 'absent'
- name: Create systemd-networkd configuration directory
ansible.builtin.file:
path: '/etc/systemd/networkd.conf.d'
state: 'directory'
mode: '0755'
when: networkd__enabled | bool and networkd__deploy_state != 'absent'
- name: Generate systemd-networkd configuration
ansible.builtin.template:
src: 'etc/systemd/networkd.conf.d/ansible.conf.j2'
dest: '/etc/systemd/networkd.conf.d/ansible.conf'
mode: '0644'
notify: [ 'Restart systemd-networkd service' ]
when: networkd__enabled | bool and networkd__deploy_state != 'absent'
- name: Remove network units if requested
ansible.builtin.file:
path: '{{ "/etc/systemd/network/" + item.name }}'
state: 'absent'
loop: '{{ networkd__combined_units | flatten | debops.debops.parse_kv_items }}'
loop_control:
label: '{{ {"name": item.name, "state": item.state | d("present")} }}'
notify: [ 'Restart systemd-networkd service' ]
when: networkd__enabled | bool and item.state | d("present") == 'absent'
- name: Remove network unit overrides if requested
ansible.builtin.file:
path: '{{ "/etc/systemd/network/" + item.name + ".d" }}'
state: 'absent'
loop: '{{ networkd__combined_units | flatten | debops.debops.parse_kv_items }}'
loop_control:
label: '{{ {"name": item.name, "state": item.state | d("present")} }}'
notify: [ 'Restart systemd-networkd service' ]
when: networkd__enabled | bool and item.state | d("present") == 'absent'
- name: Create directories for network units
ansible.builtin.file:
path: '{{ "/etc/systemd/network/" + (item.name | dirname) }}'
state: 'directory'
mode: '0755'
loop: '{{ networkd__combined_units | flatten | debops.debops.parse_kv_items }}'
loop_control:
label: '{{ {"name": item.name, "state": item.state | d("present")} }}'
when: networkd__enabled | bool and item.raw | d() and
item.state | d("present") not in ['absent', 'ignore', 'init'] and
(item.name | dirname).endswith('.d')
- name: Generate network units
ansible.builtin.template:
src: 'etc/systemd/network/template.j2'
dest: '{{ "/etc/systemd/network/" + item.name }}'
mode: '0644'
loop: '{{ networkd__combined_units | flatten | debops.debops.parse_kv_items }}'
loop_control:
label: '{{ {"name": item.name, "state": item.state | d("present")} }}'
notify: [ 'Restart systemd-networkd service' ]
when: networkd__enabled | bool and item.raw | d() and
item.state | d("present") not in ['absent', 'ignore', 'init']
- name: Flush handlers when needed
ansible.builtin.meta: 'flush_handlers'

View file

@ -0,0 +1,45 @@
#!{{ ansible_python['executable'] }}
# -*- coding: utf-8 -*-
# Copyright (C) 2023 Maciej Delmanowski <drybjed@gmail.com>
# Copyright (C) 2023 DebOps <https://debops.org/>
# SPDX-License-Identifier: GPL-3.0-only
# {{ ansible_managed }}
from __future__ import print_function
from json import loads, dumps
import subprocess
import os
def cmd_exists(cmd):
return any(
os.access(os.path.join(path, cmd), os.X_OK)
for path in os.environ["PATH"].split(os.pathsep)
)
output = {'installed': cmd_exists('networkctl')}
try:
is_enabled = subprocess.check_output(
["systemctl", "is-enabled",
"systemd-networkd.service"]).decode('utf-8')
output['state'] = is_enabled.strip()
except subprocess.CalledProcessError:
output['state'] = 'disabled'
try:
networkd_version_stdout = subprocess.check_output(
["networkctl", "--version"]
).decode('utf-8').strip()
for line in networkd_version_stdout.split('\n'):
if line.lower().startswith('systemd '):
output['version'] = line.split()[1]
except Exception:
pass
print(dumps(output, sort_keys=True, indent=4))

View file

@ -0,0 +1,10 @@
{# Copyright (C) 2023 Maciej Delmanowski <drybjed@gmail.com>
# Copyright (C) 2023 DebOps <https://debops.org/>
# SPDX-License-Identifier: GPL-3.0-only
#}
# {{ ansible_managed }}
{% if item.comment | d() %}
{{ item.comment | regex_replace('\n$', '') | comment(prefix='', postfix='') -}}
{% endif %}
{{ item.raw | regex_replace('\n$', '') }}

View file

@ -0,0 +1,65 @@
{# Copyright (C) 2023 Maciej Delmanowski <drybjed@gmail.com>
# Copyright (C) 2023 DebOps <https://debops.org/>
# SPDX-License-Identifier: GPL-3.0-only
#}
# {{ ansible_managed }}
# See networkd.conf(5) for details.
[Network]
{% for element in networkd__combined_configuration | debops.debops.parse_kv_config %}
{% if element.name | d() and element.value is defined and element.state | d('present') not in [ 'absent', 'ignore' ] %}
{% set element_comment = ('#' if (element.state | d('present') in [ 'init', 'comment' ]) else '') %}
{% if element.value is string and not element.value | bool %}
{% set element_value = element.value %}
{% elif element.value | bool and element.value is not iterable %}
{% if element.value | string == '1' %}
{% set element_value = element.value %}
{% else %}
{% set element_value = 'yes' %}
{% endif %}
{% elif not element.value | bool and element.value is not iterable %}
{% if element.value is not none %}
{% if element.value | int %}
{% set element_value = element.value %}
{% else %}
{% if element.value | string == '0' %}
{% set element_value = element.value %}
{% else %}
{% set element_value = 'no' %}
{% endif %}
{% endif %}
{% endif %}
{% endif %}
{{ '{}{}={}'.format(element_comment, element.name, element_value) }}
{% endif %}
{% endfor %}
[DHCP]
{% for element in networkd__dhcp_combined_configuration | debops.debops.parse_kv_config %}
{% if element.name | d() and element.value is defined and element.state | d('present') not in [ 'absent', 'ignore' ] %}
{% set element_comment = ('#' if (element.state | d('present') in [ 'init', 'comment' ]) else '') %}
{% if element.value is string and not element.value | bool %}
{% set element_value = element.value %}
{% elif element.value | bool and element.value is not iterable %}
{% if element.value | string == '1' %}
{% set element_value = element.value %}
{% else %}
{% set element_value = 'yes' %}
{% endif %}
{% elif not element.value | bool and element.value is not iterable %}
{% if element.value is not none %}
{% if element.value | int %}
{% set element_value = element.value %}
{% else %}
{% if element.value | string == '0' %}
{% set element_value = element.value %}
{% else %}
{% set element_value = 'no' %}
{% endif %}
{% endif %}
{% endif %}
{% endif %}
{{ '{}{}={}'.format(element_comment, element.name, element_value) }}
{% endif %}
{% endfor %}