2023-04-14 22:58:51 +00:00
#!/usr/bin/python3
import pathlib
from typing import List , Union
2023-04-15 10:20:56 +00:00
try :
from ansible . module_utils . generic import SYSTEMD_NETWORK_CONFIG as SYSTEMD_PATH
from ansible . module_utils . generic import Types
from ansible . module_utils . module import SystemdUnitModule
except ImportError :
2023-04-15 13:02:14 +00:00
from generic import SYSTEMD_NETWORK_CONFIG as SYSTEMD_PATH
from generic import Types
from module import SystemdUnitModule
2023-04-14 22:58:51 +00:00
def boolconvert ( b : Union [ bool , str ] ) - > str :
if b is True :
return " yes "
elif b is False :
return " no "
return b
class Module ( SystemdUnitModule ) :
""" Sets up the systemd network unit """
name = " systemd_network "
module_spec = dict (
argument_spec = dict (
2023-04-15 10:37:03 +00:00
mac = Types . str ( help = " The MAC-Address of the device " ) ,
device = Types . str ( help = " The name of the network device " ) ,
name = Types . str ( required = True , help = " name of the unit " ) ,
description = Types . str ( help = " An optional description " ) ,
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 " ) ,
dnssec = Types . bool ( " 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. " ) ,
dns = Types . list ( elements = str , help = " List of DNS-Servers " ) ,
domain = Types . list ( elements = str , help = " List of domains that are on this device " ) ,
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 " ) ,
address = Types . list ( elements = str , required = True , help = " IP-Addresses of this networkdevice " ) ,
route = Types . list ( elements = str , help = " Routes of networks that can be reached with this device " ) ,
2023-04-14 22:58:51 +00:00
) ,
required_if = ( ( " defaultdns " , True , ( " dns " , ) , False ) , ) ,
required_one_of = ( ( " mac " , " device " ) , ) ,
)
def prepare ( self ) :
self . unitfile = SYSTEMD_PATH . joinpath ( self . get ( " name " ) ) . with_suffix ( " .network " )
self . __unit = None
def unit ( self ) - > str :
if self . __unit is None :
self . __unit = " \n " . join (
(
self . match ( ) ,
self . network ( ) ,
self . addresses ( ) ,
self . routes ( ) ,
)
)
return self . __unit
def match ( self ) - > str :
matches = [ ]
if self . get ( " mac " , False ) :
matches . append ( " MACAddress= {} \n " . format ( self . get ( " mac " ) ) )
if self . get ( " device " , False ) :
matches . append ( " Name= {} \n " . format ( self . get ( " device " ) ) )
return " [Match] \n " + " " . join ( matches )
def network ( self ) - > str :
output = " [Network] \n "
options = [ ]
try :
options . append ( " Description= {} " . format ( self . get ( " description " ) ) )
except KeyError :
pass
try :
for server in self . get ( " dns " , [ ] ) :
options . append ( f " DNS= { server } " )
options . append ( " DNSDefaultRoute= {} " . format ( self . get ( " defaultdns " , False ) ) )
except KeyError :
pass
try :
domain = self . get ( " domain " )
self . set ( " domainlog " , str ( domain ) )
options . append ( " Domains= {} " . format ( " " . join ( domain ) ) )
options . append (
" DNSOverTLS= {} " . format ( boolconvert ( self . get ( " dot " , " opportunistic " ) ) )
)
options . append (
" DNSSEC= {} " . format ( boolconvert ( self . get ( " dnssec " , " allow-downgrade " ) ) )
)
except KeyError :
pass
output + = " \n " . join ( options )
return output
def addresses ( self ) - > str :
output = [ ]
for address in self . get ( " address " ) :
output . append ( f " [Address] \n Address= { address } \n " )
return " \n " . join ( output )
def routes ( self ) - > str :
output = [ ]
routes = self . get ( " route " , [ ] )
self . set ( " routes " , routes )
for gw in routes :
output . append ( f " [Route] \n Gateway= { gw } \n GatewayOnLink=yes \n QuickAck=yes \n " )
self . set ( " routes " , output )
return " \n " . join ( output )
DOCUMENTATION = """ ---
description :
- Sets up the systemd network unit
module : systemd_network
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
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 :
- An optional description
required : false
type : str
device :
description :
- The name of the network device
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-15 10:37:03 +00:00
required : true
2023-04-14 22:58:51 +00:00
type : bool
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 :
- The MAC - Address of the device
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
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
short_description : Sets up the systemd network unit
"""
if __name__ == " __main__ " :
Module ( ) ( )