1
1
#!/usr/bin/python
2
2
# -*- coding: utf-8 -*-
3
-
3
+ #
4
4
# Copyright (c) 2025, Jana Hoch <[email protected] >
5
5
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
6
6
# SPDX-License-Identifier: GPL-3.0-or-later
9
9
10
10
__metaclass__ = type
11
11
12
- from pygments .lexer import default
13
-
14
- DOCUMENTATION = r""""""
15
-
16
- EXAMPLES = r""""""
17
-
18
- RETURN = r""""""
12
+ DOCUMENTATION = r"""
13
+ module: proxmox_subnet
14
+ short_description: Create/Update/Delete subnets from SDN
15
+ description:
16
+ - Create, update, or delete subnets in Proxmox SDN.
17
+ author: 'Jana Hoch <[email protected] > (!UNKNOWN)'
18
+ attributes:
19
+ check_mode:
20
+ support: none
21
+ diff_mode:
22
+ support: none
23
+ options:
24
+ state:
25
+ description:
26
+ - Desired state of the network configuration.
27
+ - Choices include present (create), absent (delete), or update (modify).
28
+ type: str
29
+ choices: ['present', 'absent', 'update']
30
+ default: present
31
+ force:
32
+ description:
33
+ - If true it will create subnet when state is update but subnet is missing and update the subnet when state is present and subnet already exists
34
+ type: bool
35
+ default: False
36
+ subnet:
37
+ description:
38
+ - subnet CIDR.
39
+ type: str
40
+ required: true
41
+ type:
42
+ description:
43
+ - Type of network configuration.
44
+ - Currently only supports 'subnet'.
45
+ type: str
46
+ choices: ['subnet']
47
+ default: subnet
48
+ vnet:
49
+ description:
50
+ - The virtual network to which the subnet belongs.
51
+ type: str
52
+ required: true
53
+ zone:
54
+ description:
55
+ - Vnet Zone
56
+ type: str
57
+ dhcp_dns_server:
58
+ description:
59
+ - IP address for the DNS server.
60
+ type: str
61
+ dhcp_range:
62
+ description:
63
+ - Range of IP addresses for DHCP.
64
+ type: list
65
+ elements: dict
66
+ suboptions:
67
+ start:
68
+ description:
69
+ - Starting IP address of the DHCP range.
70
+ type: str
71
+ required: true
72
+ end:
73
+ description:
74
+ - Ending IP address of the DHCP range.
75
+ type: str
76
+ required: true
77
+ dnszoneprefix:
78
+ description:
79
+ - Prefix for the DNS zone.
80
+ type: str
81
+ gateway:
82
+ description:
83
+ - Subnet Gateway. Will be assign on vnet for layer3 zones.
84
+ type: str
85
+ lock_token:
86
+ description:
87
+ - the token for unlocking the global SDN configuration.
88
+ type: str
89
+ snat:
90
+ description:
91
+ - Enable Source NAT for the subnet.
92
+ type: bool
93
+ default: False
94
+ delete:
95
+ description:
96
+ - A list of settings you want to delete.
97
+ type: str
98
+ extends_documentation_fragment:
99
+ - community.proxmox.proxmox.actiongroup_proxmox
100
+ - community.proxmox.proxmox.documentation
101
+ - community.proxmox.attributes
102
+ """
103
+
104
+ EXAMPLES = r"""
105
+ - name: Create a subnet
106
+ community.proxmox.proxmox_subnet:
107
+ api_user: "{{ pc.proxmox.api_user }}"
108
+ api_token_id: "{{ pc.proxmox.api_token_id }}"
109
+ api_token_secret: "{{ vault.proxmox.api_token_secret }}"
110
+ api_host: "{{ pc.proxmox.api_host }}"
111
+ validate_certs: no
112
+ vnet: test
113
+ subnet: 10.10.2.0/24
114
+ zone: ans1
115
+ state: present
116
+
117
+ - name: Update a subnet
118
+ community.proxmox.proxmox_subnet:
119
+ api_user: "{{ pc.proxmox.api_user }}"
120
+ api_token_id: "{{ pc.proxmox.api_token_id }}"
121
+ api_token_secret: "{{ vault.proxmox.api_token_secret }}"
122
+ api_host: "{{ pc.proxmox.api_host }}"
123
+ validate_certs: no
124
+ vnet: test
125
+ subnet: 10.10.2.0/24
126
+ zone: ans1
127
+ state: update
128
+ dhcp_range:
129
+ - start: 10.10.2.5
130
+ end: 10.10.2.50
131
+ - start: 10.10.2.100
132
+ end: 10.10.2.150
133
+ snat: True
134
+
135
+ - name: Delete a subnet
136
+ community.proxmox.proxmox_subnet:
137
+ api_user: "{{ pc.proxmox.api_user }}"
138
+ api_token_id: "{{ pc.proxmox.api_token_id }}"
139
+ api_token_secret: "{{ vault.proxmox.api_token_secret }}"
140
+ api_host: "{{ pc.proxmox.api_host }}"
141
+ validate_certs: no
142
+ vnet: test
143
+ subnet: 10.10.2.0/24
144
+ zone: ans1
145
+ state: absent
146
+ """
147
+
148
+ RETURN = r"""
149
+ subnet:
150
+ description:
151
+ - Subnet ID which was created/updated/deleted
152
+ returned: on success
153
+ type: str
154
+ sample:
155
+ ans1-10.10.2.0-24
156
+ """
19
157
20
158
from ansible .module_utils .basic import AnsibleModule
21
159
from ansible_collections .community .proxmox .plugins .module_utils .proxmox import (
24
162
ProxmoxAnsible
25
163
)
26
164
165
+
27
166
def get_proxmox_args ():
28
167
return dict (
29
168
state = dict (type = "str" , choices = ["present" , "absent" , "update" ], default = 'present' , required = False ),
@@ -44,11 +183,12 @@ def get_proxmox_args():
44
183
),
45
184
dnszoneprefix = dict (type = 'str' , required = False ),
46
185
gateway = dict (type = 'str' , required = False ),
47
- lock_token = dict (type = "str" , required = False ),
186
+ lock_token = dict (type = "str" , required = False , no_log = False ),
48
187
snat = dict (type = 'bool' , default = False , required = False ),
49
188
delete = dict (type = "str" , required = False )
50
189
)
51
190
191
+
52
192
def get_ansible_module ():
53
193
module_args = proxmox_auth_argument_spec ()
54
194
module_args .update (get_proxmox_args ())
@@ -62,6 +202,7 @@ def get_ansible_module():
62
202
]
63
203
)
64
204
205
+
65
206
class ProxmoxSubnetAnsible (ProxmoxAnsible ):
66
207
def __init__ (self , module ):
67
208
super (ProxmoxSubnetAnsible , self ).__init__ (module )
@@ -96,7 +237,7 @@ def get_dhcp_range(self):
96
237
dhcp_range = [f"start-address={ x ['start' ]} ,end-address={ x ['end' ]} " for x in self .params .get ('dhcp_range' )]
97
238
return dhcp_range
98
239
99
- def subnet_present (self ,force , ** subnet_params ):
240
+ def subnet_present (self , force , ** subnet_params ):
100
241
vnet_name = subnet_params ['vnet' ]
101
242
lock = subnet_params ['lock-token' ]
102
243
subnet = subnet_params ['subnet' ]
@@ -123,10 +264,10 @@ def subnet_present(self,force, **subnet_params):
123
264
except Exception as e :
124
265
self .rollback_sdn_changes_and_release_lock (lock = lock )
125
266
self .module .fail_json (
126
- msg = f'Failed to create subnet. Rolling back all changes. : { e } '
267
+ msg = f'Failed to create subnet. Rolling back all changes : { e } '
127
268
)
128
269
129
- def subnet_update (self ,force , ** subnet_params ):
270
+ def subnet_update (self , force , ** subnet_params ):
130
271
lock = subnet_params ['lock-token' ]
131
272
vnet_id = subnet_params ['vnet' ]
132
273
subnet_id = f"{ self .params ['zone' ]} -{ subnet_params ['subnet' ].replace ('/' , '-' )} "
0 commit comments