2023-04-14 22:58:51 +00:00
#!/usr/bin/python3
import pathlib
2024-03-11 19:41:12 +00:00
from typing import List , Optional , Union
2023-04-14 22:58:51 +00:00
2024-02-11 13:35:00 +00:00
try :
2024-02-11 13:39:52 +00:00
from ansible_module . module_utils . generic import SYSTEMD_NETWORK_CONFIG , Types , modspec , systemdbool
from ansible_module . module_utils . module import SystemdUnitModule
2024-02-11 13:35:00 +00:00
except ImportError :
2024-02-11 19:59:50 +00:00
from ansible_collections . sebastian . base . plugins . module_utils . generic import SYSTEMD_NETWORK_CONFIG , Types , modspec , systemdbool
from ansible_collections . sebastian . base . plugins . module_utils . module import SystemdUnitModule
2023-04-15 22:10:13 +00:00
2023-04-14 22:58:51 +00:00
2023-04-23 08:32:42 +00:00
class Module ( SystemdUnitModule ) : # type: ignore
2023-04-14 22:58:51 +00:00
""" Sets up the systemd network unit """
2023-04-20 20:09:58 +00:00
name = " network "
2023-04-23 08:32:42 +00:00
module_spec = modspec (
2023-04-14 22:58:51 +00:00
argument_spec = dict (
2023-07-15 20:25:59 +00:00
mac = Types . str ( help = " The MAC-Address of the device. An ! before the value matches anything but this value. " ) ,
device = Types . str ( help = " The name of the network device. An ! before the value matches anything but this value. " ) ,
virtualization = Types . str ( help = " The virtualization type. An ! before the value matches anything but this value. " ) ,
2023-04-15 10:37:03 +00:00
name = Types . str ( required = True , help = " name of the unit " ) ,
2023-04-20 22:17:36 +00:00
dot = Types . bool ( help = " if DNS-over-TLS should be required or disabled. If it is unset, it will used if the server supports it " ) ,
2023-04-20 20:09:58 +00:00
dnssec = Types . bool (
2023-04-23 08:32:42 +00:00
help = " if the Domainqueries should require DNSSEC or not. \n If its missing, domains that have DNSSEC enabled will be validated, all others it will be assumed to be okay. "
2023-04-20 20:09:58 +00:00
) ,
2023-04-15 10:37:03 +00:00
dns = Types . list ( elements = str , help = " List of DNS-Servers " ) ,
2023-04-20 22:17:36 +00:00
domain = Types . list ( elements = str , help = " List of domains that are on this device " ) ,
2023-04-20 20:09:58 +00:00
defaultdns = Types . bool (
help = " If the DNS-Server(s) on this device should be used for all domains that are not set on other devices "
) ,
2023-04-20 22:17:36 +00:00
address = Types . list ( elements = str , required = True , help = " IP-Addresses of this networkdevice " ) ,
2023-04-20 20:09:58 +00:00
route = Types . list (
elements = str ,
help = " Routes of networks that can be reached with this device " ,
) ,
2023-11-23 18:08:51 +00:00
masquerade = Types . str (
help = " how the packets are modified to look like the come from the computer itself. " ,
2024-03-08 23:13:24 +00:00
choices = ( " true " , " false " , " both " , " ipv4 " , " ipv6 " , " no " ) ,
2023-11-23 18:08:51 +00:00
) ,
2023-04-14 22:58:51 +00:00
) ,
required_if = ( ( " defaultdns " , True , ( " dns " , ) , False ) , ) ,
2023-07-15 20:25:59 +00:00
required_one_of = ( ( " mac " , " device " , " virtualization " ) , ) ,
2023-04-14 22:58:51 +00:00
)
def prepare ( self ) :
2023-04-20 22:17:36 +00:00
self . unitfile = SYSTEMD_NETWORK_CONFIG . joinpath ( self . get ( " name " ) ) . with_suffix ( " .network " )
2023-04-14 22:58:51 +00:00
self . __unit = None
def unit ( self ) - > str :
if self . __unit is None :
2024-03-08 23:13:24 +00:00
self . __unit = self . _unit (
self . match ( ) ,
self . network ( ) ,
self . addresses ( ) ,
self . routes ( ) ,
2023-04-14 22:58:51 +00:00
)
return self . __unit
2024-03-08 23:13:24 +00:00
def match ( self ) - > Optional [ str ] :
matches = self . map_param (
mac = " MACAddress " ,
device = " Name " ,
virtualization = " Virtualization " ,
2023-07-15 20:25:59 +00:00
)
2024-03-08 23:13:24 +00:00
if len ( matches ) == 0 :
return None
return " [Match] \n " + " " . join ( matches )
2023-04-14 22:58:51 +00:00
2024-03-08 23:13:24 +00:00
def network ( self ) - > Optional [ str ] :
2023-04-14 22:58:51 +00:00
options = [ ]
2024-03-08 23:13:24 +00:00
if self . get ( " description " , None ) is None :
2023-04-14 22:58:51 +00:00
options . append ( " Description= {} " . format ( self . get ( " description " ) ) )
2024-03-08 23:13:24 +00:00
server : str
for server in self . get ( " dns " , [ ] ) :
options . append ( f " DNS= { server } " )
options . append ( " DNSDefaultRoute= {} " . format ( self . get ( " defaultdns " , False ) ) )
if self . get ( " domain " , False ) :
options . append ( " Domains= {} " . format ( " " . join ( self . get ( " domain " ) ) ) )
2023-04-21 18:49:07 +00:00
options . append ( " DNSOverTLS= {} " . format ( systemdbool ( self . get ( " dot " , " opportunistic " ) ) ) )
options . append ( " DNSSEC= {} " . format ( systemdbool ( self . get ( " dnssec " , " allow-downgrade " ) ) ) )
2024-03-08 23:13:24 +00:00
if self . get ( " masquerade " , None ) is not None :
masquerade : str = self . get ( " masquerade " )
2023-11-23 18:08:51 +00:00
if masquerade == " true " :
masquerade = " both "
elif masquerade == " false " :
masquerade = " no "
options . append ( f " IPMasquerade= { masquerade } " )
2024-03-08 23:13:24 +00:00
if len ( options ) == 0 :
return None
return " [Network] \n " + " " . join ( options )
2023-04-14 22:58:51 +00:00
def addresses ( self ) - > str :
output = [ ]
for address in self . get ( " address " ) :
output . append ( f " [Address] \n Address= { address } \n " )
return " \n " . join ( output )
2024-03-08 23:13:24 +00:00
def routes ( self ) - > Optional [ str ] :
2023-04-14 22:58:51 +00:00
output = [ ]
2024-03-08 23:13:24 +00:00
routes : list [ str ] = self . get ( " route " , [ ] )
2023-04-14 22:58:51 +00:00
for gw in routes :
output . append ( f " [Route] \n Gateway= { gw } \n GatewayOnLink=yes \n QuickAck=yes \n " )
2024-03-08 23:13:24 +00:00
if len ( output ) == 0 :
return None
2023-04-14 22:58:51 +00:00
return " \n " . join ( output )
DOCUMENTATION = """ ---
description :
- Sets up the systemd network unit
2023-04-20 20:09:58 +00:00
module : network
2023-04-14 22:58:51 +00:00
options :
address :
2023-04-15 10:37:03 +00:00
description :
- IP - Addresses of this networkdevice
2023-04-14 22:58:51 +00:00
elements : str
required : true
type : list
2023-04-20 22:19:35 +00:00
after :
default : [ ]
description :
- list of units that this unit wants to be started after this unit
elements : str
required : false
type : list
before :
default : [ ]
description :
- list of units that this unit needs to be started before this unit .
elements : str
required : false
type : list
2023-04-14 22:58:51 +00:00
defaultdns :
2023-04-15 10:37:03 +00:00
description :
- If the DNS - Server ( s ) on this device should be used for all domains that are
not set on other devices
2023-04-14 22:58:51 +00:00
required : false
type : bool
description :
2023-04-15 10:37:03 +00:00
description :
2023-04-21 15:32:28 +00:00
- An description for programs that access systemd
2023-04-15 10:37:03 +00:00
required : false
type : str
device :
description :
2023-07-15 20:25:59 +00:00
- The name of the network device . An ! before the value matches anything but this
value .
2023-04-14 22:58:51 +00:00
required : false
type : str
dns :
default : [ ]
2023-04-15 10:37:03 +00:00
description :
- List of DNS - Servers
2023-04-14 22:58:51 +00:00
elements : str
required : false
type : list
dnssec :
2023-04-23 08:32:42 +00:00
description :
- if the Domainqueries should require DNSSEC or not .
- If its missing , domains that have DNSSEC enabled will be validated , all others
it will be assumed to be okay .
required : false
2023-04-14 22:58:51 +00:00
type : bool
2023-04-20 22:19:35 +00:00
documentation :
default : [ ]
description :
- Paths where documentation can be found
elements : str
required : false
type : list
2023-04-14 22:58:51 +00:00
domain :
default : [ ]
2023-04-15 10:37:03 +00:00
description :
- List of domains that are on this device
2023-04-14 22:58:51 +00:00
elements : str
required : false
type : list
dot :
2023-04-15 10:37:03 +00:00
description :
- if DNS - over - TLS should be required or disabled . If it is unset , it will used
if the server supports it
2023-04-14 22:58:51 +00:00
required : false
type : bool
2023-04-15 10:37:03 +00:00
mac :
description :
2023-07-15 20:25:59 +00:00
- The MAC - Address of the device . An ! before the value matches anything but this
value .
2023-04-15 10:37:03 +00:00
required : false
type : str
2023-11-23 18:08:51 +00:00
masquerade :
choices :
- ' true '
- ' false '
- both
- ipv4
- ipv6
2024-03-08 23:13:24 +00:00
- ' no '
2023-11-23 18:08:51 +00:00
description :
- how the packets are modified to look like the come from the computer itself .
required : false
type : str
2023-04-14 22:58:51 +00:00
name :
2023-04-15 10:37:03 +00:00
description :
- name of the unit
2023-04-14 22:58:51 +00:00
required : true
type : str
2023-04-20 22:19:35 +00:00
partof :
default : [ ]
description :
- list of units that this unit is part of .
- If the restart this unit does it too , but if this restarts it does not affect
the other units .
elements : str
required : false
type : list
requires :
default : [ ]
description :
- list of units that this unit requires . If it fails or can ' t be started this
unit fails . without before / after this is started at the same time
elements : str
required : false
type : list
2023-04-14 22:58:51 +00:00
route :
default : [ ]
2023-04-15 10:37:03 +00:00
description :
- Routes of networks that can be reached with this device
2023-04-14 22:58:51 +00:00
elements : str
required : false
type : list
2023-07-15 20:25:59 +00:00
virtualization :
description :
- The virtualization type . An ! before the value matches anything but this value .
required : false
type : str
2023-04-20 22:19:35 +00:00
wants :
default : [ ]
description :
- list of units that this unit wants . If it fails or can ' t be started it does
not affect this unit
elements : str
required : false
type : list
2023-04-14 22:58:51 +00:00
short_description : Sets up the systemd network unit
"""
if __name__ == " __main__ " :
Module ( ) ( )