| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
from __future__ import absolute_import, division, print_function |
| 19 |
__metaclass__ = type |
| 20 |
|
| 21 |
|
| 22 |
DOCUMENTATION = ''' |
| 23 |
--- |
| 24 |
module: subnet |
| 25 |
version_added: 1.0.0 |
| 26 |
short_description: Manage Subnets |
| 27 |
description: |
| 28 |
- Create, update, and delete Subnets |
| 29 |
author: |
| 30 |
- "Baptiste Agasse (@bagasse)" |
| 31 |
options: |
| 32 |
name: |
| 33 |
description: Subnet name |
| 34 |
required: true |
| 35 |
type: str |
| 36 |
description: |
| 37 |
description: Description of the subnet |
| 38 |
type: str |
| 39 |
updated_name: |
| 40 |
description: New subnet name. When this parameter is set, the module will not be idempotent. |
| 41 |
type: str |
| 42 |
network_type: |
| 43 |
description: Subnet type |
| 44 |
default: IPv4 |
| 45 |
choices: ["IPv4", "IPv6"] |
| 46 |
type: str |
| 47 |
dns_primary: |
| 48 |
description: Primary DNS server for this subnet |
| 49 |
required: false |
| 50 |
type: str |
| 51 |
dns_secondary: |
| 52 |
description: Secondary DNS server for this subnet |
| 53 |
required: false |
| 54 |
type: str |
| 55 |
domains: |
| 56 |
description: List of DNS domains the subnet should assigned to |
| 57 |
required: false |
| 58 |
type: list |
| 59 |
elements: str |
| 60 |
gateway: |
| 61 |
description: Subnet gateway IP address |
| 62 |
required: false |
| 63 |
type: str |
| 64 |
network: |
| 65 |
description: Subnet IP address |
| 66 |
required: true |
| 67 |
type: str |
| 68 |
cidr: |
| 69 |
description: CIDR prefix length; Required if I(network_type=IPv4) and no I(mask) provided |
| 70 |
type: int |
| 71 |
mask: |
| 72 |
description: Subnet netmask. Required if I(network_type=IPv4) and no I(cidr) prefix length provided |
| 73 |
type: str |
| 74 |
from_ip: |
| 75 |
description: First IP address of the host IP allocation pool |
| 76 |
required: false |
| 77 |
type: str |
| 78 |
to_ip: |
| 79 |
description: Last IP address of the host IP allocation pool |
| 80 |
required: false |
| 81 |
type: str |
| 82 |
boot_mode: |
| 83 |
description: Boot mode used by hosts in this subnet |
| 84 |
required: false |
| 85 |
default: DHCP |
| 86 |
choices: ["DHCP", "Static"] |
| 87 |
type: str |
| 88 |
ipam: |
| 89 |
description: IPAM mode for this subnet |
| 90 |
required: false |
| 91 |
default: DHCP |
| 92 |
choices: |
| 93 |
- "DHCP" |
| 94 |
- "Internal DB" |
| 95 |
- "Random DB" |
| 96 |
- "EUI-64" |
| 97 |
- "External IPAM" |
| 98 |
- "None" |
| 99 |
type: str |
| 100 |
dhcp_proxy: |
| 101 |
description: DHCP Smart proxy for this subnet |
| 102 |
required: false |
| 103 |
type: str |
| 104 |
httpboot_proxy: |
| 105 |
description: HTTP Boot Smart proxy for this subnet |
| 106 |
required: false |
| 107 |
type: str |
| 108 |
tftp_proxy: |
| 109 |
description: TFTP Smart proxy for this subnet |
| 110 |
required: false |
| 111 |
type: str |
| 112 |
discovery_proxy: |
| 113 |
description: |
| 114 |
- Discovery Smart proxy for this subnet |
| 115 |
- This option is only available if the discovery plugin is installed. |
| 116 |
required: false |
| 117 |
type: str |
| 118 |
dns_proxy: |
| 119 |
description: Reverse DNS Smart proxy for this subnet |
| 120 |
required: false |
| 121 |
type: str |
| 122 |
template_proxy: |
| 123 |
description: Template Smart proxy for this subnet |
| 124 |
required: false |
| 125 |
type: str |
| 126 |
bmc_proxy: |
| 127 |
description: BMC Smart proxy for this subnet |
| 128 |
required: false |
| 129 |
type: str |
| 130 |
version_added: 2.1.0 |
| 131 |
remote_execution_proxies: |
| 132 |
description: |
| 133 |
- Remote execution Smart proxies for this subnet |
| 134 |
- This option is only available if the remote_execution plugin is installed. |
| 135 |
- This will always report I(changed=true) when used with I(remote_execution < 4.1.0), due to a bug in the plugin. |
| 136 |
required: false |
| 137 |
type: list |
| 138 |
elements: str |
| 139 |
externalipam_proxy: |
| 140 |
description: |
| 141 |
- External IPAM proxy for this subnet. |
| 142 |
- Only relevant if I(ipam=External IPAM). |
| 143 |
required: false |
| 144 |
type: str |
| 145 |
externalipam_group: |
| 146 |
description: |
| 147 |
- External IPAM group for this subnet. |
| 148 |
- Only relevant if I(ipam=External IPAM). |
| 149 |
version_added: 1.5.0 |
| 150 |
required: false |
| 151 |
type: str |
| 152 |
vlanid: |
| 153 |
description: VLAN ID |
| 154 |
required: false |
| 155 |
type: int |
| 156 |
mtu: |
| 157 |
description: MTU |
| 158 |
required: false |
| 159 |
type: int |
| 160 |
parameters: |
| 161 |
description: |
| 162 |
- Subnet specific host parameters |
| 163 |
extends_documentation_fragment: |
| 164 |
- theforeman.foreman.foreman |
| 165 |
- theforeman.foreman.foreman.entity_state |
| 166 |
- theforeman.foreman.foreman.taxonomy |
| 167 |
- theforeman.foreman.foreman.nested_parameters |
| 168 |
''' |
| 169 |
|
| 170 |
EXAMPLES = ''' |
| 171 |
- name: My subnet |
| 172 |
theforeman.foreman.subnet: |
| 173 |
name: "My subnet" |
| 174 |
description: "My description" |
| 175 |
network: "192.168.0.0" |
| 176 |
mask: "255.255.255.192" |
| 177 |
gateway: "192.168.0.1" |
| 178 |
from_ip: "192.168.0.2" |
| 179 |
to_ip: "192.168.0.42" |
| 180 |
boot_mode: "Static" |
| 181 |
dhcp_proxy: "smart-proxy1.foo.example.com" |
| 182 |
tftp_proxy: "smart-proxy1.foo.example.com" |
| 183 |
dns_proxy: "smart-proxy2.foo.example.com" |
| 184 |
template_proxy: "smart-proxy2.foo.example.com" |
| 185 |
vlanid: 452 |
| 186 |
mtu: 9000 |
| 187 |
domains: |
| 188 |
- "foo.example.com" |
| 189 |
- "bar.example.com" |
| 190 |
organizations: |
| 191 |
- "Example Org" |
| 192 |
locations: |
| 193 |
- "Toulouse" |
| 194 |
server_url: "https://foreman.example.com" |
| 195 |
username: "admin" |
| 196 |
password: "changeme" |
| 197 |
state: present |
| 198 |
''' |
| 199 |
|
| 200 |
RETURN = ''' |
| 201 |
entity: |
| 202 |
description: Final state of the affected entities grouped by their type. |
| 203 |
returned: success |
| 204 |
type: dict |
| 205 |
contains: |
| 206 |
subnets: |
| 207 |
description: List of subnets. |
| 208 |
type: list |
| 209 |
elements: dict |
| 210 |
''' |
| 211 |
|
| 212 |
import ipaddress |
| 213 |
from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import ( |
| 214 |
ForemanTaxonomicEntityAnsibleModule, ParametersMixin |
| 215 |
) |
| 216 |
|
| 217 |
|
| 218 |
class ForemanSubnetModule(ParametersMixin, ForemanTaxonomicEntityAnsibleModule): |
| 219 |
pass |
| 220 |
|
| 221 |
|
| 222 |
def main(): |
| 223 |
module = ForemanSubnetModule( |
| 224 |
argument_spec=dict( |
| 225 |
updated_name=dict(), |
| 226 |
), |
| 227 |
foreman_spec=dict( |
| 228 |
name=dict(required=True), |
| 229 |
description=dict(), |
| 230 |
network_type=dict(choices=['IPv4', 'IPv6'], default='IPv4'), |
| 231 |
dns_primary=dict(), |
| 232 |
dns_secondary=dict(), |
| 233 |
domains=dict(type='entity_list'), |
| 234 |
gateway=dict(), |
| 235 |
network=dict(required=True), |
| 236 |
cidr=dict(type='int'), |
| 237 |
mask=dict(), |
| 238 |
from_ip=dict(flat_name='from'), |
| 239 |
to_ip=dict(flat_name='to'), |
| 240 |
boot_mode=dict(choices=['DHCP', 'Static'], default='DHCP'), |
| 241 |
ipam=dict(choices=['DHCP', 'Internal DB', 'Random DB', 'EUI-64', 'External IPAM', 'None'], default='DHCP'), |
| 242 |
dhcp_proxy=dict(type='entity', flat_name='dhcp_id', resource_type='smart_proxies'), |
| 243 |
httpboot_proxy=dict(type='entity', flat_name='httpboot_id', resource_type='smart_proxies'), |
| 244 |
tftp_proxy=dict(type='entity', flat_name='tftp_id', resource_type='smart_proxies'), |
| 245 |
discovery_proxy=dict(type='entity', flat_name='discovery_id', resource_type='smart_proxies'), |
| 246 |
dns_proxy=dict(type='entity', flat_name='dns_id', resource_type='smart_proxies'), |
| 247 |
template_proxy=dict(type='entity', flat_name='template_id', resource_type='smart_proxies'), |
| 248 |
bmc_proxy=dict(type='entity', flat_name='bmc_id', resource_type='smart_proxies'), |
| 249 |
remote_execution_proxies=dict(type='entity_list', resource_type='smart_proxies'), |
| 250 |
externalipam_proxy=dict(type='entity', flat_name='externalipam_id', resource_type='smart_proxies'), |
| 251 |
externalipam_group=dict(), |
| 252 |
vlanid=dict(type='int'), |
| 253 |
mtu=dict(type='int'), |
| 254 |
), |
| 255 |
required_plugins=[ |
| 256 |
('discovery', ['discovery_proxy']), |
| 257 |
('remote_execution', ['remote_execution_proxies']), |
| 258 |
], |
| 259 |
) |
| 260 |
|
| 261 |
module_params = module.foreman_params |
| 262 |
|
| 263 |
if not module.desired_absent: |
| 264 |
if module_params['network_type'] == 'IPv4': |
| 265 |
if 'mask' not in module_params and 'cidr' not in module_params: |
| 266 |
module.fail_json(msg='When specifying IPv4 networks, either "mask" or "cidr" is required.') |
| 267 |
IPNetwork = ipaddress.IPv4Network |
| 268 |
else: |
| 269 |
IPNetwork = ipaddress.IPv6Network |
| 270 |
if 'mask' in module_params and 'cidr' not in module_params: |
| 271 |
module_params['cidr'] = IPNetwork(u'%s/%s' % (module_params['network'], module_params['mask'])).prefixlen |
| 272 |
elif 'mask' not in module_params and 'cidr' in module_params: |
| 273 |
module_params['mask'] = str(IPNetwork(u'%s/%s' % (module_params['network'], module_params['cidr'])).netmask) |
| 274 |
|
| 275 |
with module.api_connection(): |
| 276 |
module.run() |
| 277 |
|
| 278 |
|
| 279 |
if __name__ == '__main__': |
| 280 |
main() |