| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
ANSIBLE_METADATA = {'metadata_version': '1.1', |
| 10 |
'status': ['preview'], |
| 11 |
'supported_by': 'community'} |
| 12 |
|
| 13 |
DOCUMENTATION = r''' |
| 14 |
--- |
| 15 |
module: intersight_network_connectivity_policy |
| 16 |
short_description: Network Connectivity Policy configuration for Cisco Intersight |
| 17 |
description: |
| 18 |
- Manages Network Connectivity Policy configuration on Cisco Intersight. |
| 19 |
- A policy to configure network connectivity settings including DNS and IPv6 on Cisco Intersight managed servers. |
| 20 |
- For more information see L(Cisco Intersight,https://intersight.com/apidocs/networkconfig/Policy/get/). |
| 21 |
extends_documentation_fragment: intersight |
| 22 |
options: |
| 23 |
state: |
| 24 |
description: |
| 25 |
- If C(present), will verify the resource is present and will create if needed. |
| 26 |
- If C(absent), will verify the resource is absent and will delete if needed. |
| 27 |
type: str |
| 28 |
choices: [present, absent] |
| 29 |
default: present |
| 30 |
organization: |
| 31 |
description: |
| 32 |
- The name of the Organization this resource is assigned to. |
| 33 |
- Profiles, Policies, and Pools that are created within a Custom Organization are applicable only to devices in the same Organization. |
| 34 |
type: str |
| 35 |
default: default |
| 36 |
name: |
| 37 |
description: |
| 38 |
- The name assigned to the Network Connectivity Policy. |
| 39 |
- The name must be between 1 and 62 alphanumeric characters, allowing special characters :-_. |
| 40 |
type: str |
| 41 |
required: true |
| 42 |
description: |
| 43 |
description: |
| 44 |
- The user-defined description for the Network Connectivity Policy. |
| 45 |
- Description can contain letters(a-z, A-Z), numbers(0-9), hyphen(-), period(.), colon(:), or an underscore(_). |
| 46 |
type: str |
| 47 |
aliases: [descr] |
| 48 |
tags: |
| 49 |
description: |
| 50 |
- List of tags in Key:<user-defined key> Value:<user-defined value> format. |
| 51 |
type: list |
| 52 |
elements: dict |
| 53 |
enable_dynamic_dns: |
| 54 |
description: |
| 55 |
- If enabled, updates the resource records to the DNS from Cisco IMC. |
| 56 |
type: bool |
| 57 |
default: false |
| 58 |
dynamic_dns_domain: |
| 59 |
description: |
| 60 |
- The domain name appended to a hostname for a Dynamic DNS (DDNS) update. |
| 61 |
- If left blank, only a hostname is sent to the DDNS update request. |
| 62 |
- This parameter is optional even when enable_dynamic_dns is true. |
| 63 |
type: str |
| 64 |
enable_ipv4_dns_from_dhcp: |
| 65 |
description: |
| 66 |
- Enable IPv4 DNS from DHCP. |
| 67 |
- If enabled, Cisco IMC retrieves the DNS server addresses from DHCP. |
| 68 |
type: bool |
| 69 |
default: false |
| 70 |
preferred_ipv4_dns_server: |
| 71 |
description: |
| 72 |
- IP address of the primary DNS server. |
| 73 |
- This is used when enable_ipv4_dns_from_dhcp is false. |
| 74 |
type: str |
| 75 |
default: "0.0.0.0" |
| 76 |
alternate_ipv4_dns_server: |
| 77 |
description: |
| 78 |
- IP address of the secondary DNS server. |
| 79 |
- This is used when enable_ipv4_dns_from_dhcp is false. |
| 80 |
type: str |
| 81 |
default: "0.0.0.0" |
| 82 |
enable_ipv6: |
| 83 |
description: |
| 84 |
- If enabled, allows to configure IPv6 properties. |
| 85 |
type: bool |
| 86 |
default: false |
| 87 |
enable_ipv6_dns_from_dhcp: |
| 88 |
description: |
| 89 |
- If enabled, Cisco IMC retrieves the DNS server addresses from DHCP. |
| 90 |
- Use DHCP field must be enabled for IPv6 in Cisco IMC to enable it. |
| 91 |
type: bool |
| 92 |
default: false |
| 93 |
preferred_ipv6_dns_server: |
| 94 |
description: |
| 95 |
- IP address of the primary DNS server. |
| 96 |
type: str |
| 97 |
default: "::" |
| 98 |
alternate_ipv6_dns_server: |
| 99 |
description: |
| 100 |
- IP address of the secondary DNS server. |
| 101 |
type: str |
| 102 |
default: "::" |
| 103 |
author: |
| 104 |
- Ron Gershburg (@rgershbu) |
| 105 |
''' |
| 106 |
|
| 107 |
EXAMPLES = r''' |
| 108 |
- name: Create a Network Connectivity Policy with Dynamic DNS enabled |
| 109 |
cisco.intersight.intersight_network_connectivity_policy: |
| 110 |
api_private_key: "{{ api_private_key }}" |
| 111 |
api_key_id: "{{ api_key_id }}" |
| 112 |
organization: "default" |
| 113 |
name: "DynamicDNS-Policy" |
| 114 |
description: "Network connectivity policy with Dynamic DNS" |
| 115 |
tags: |
| 116 |
- Key: "Site" |
| 117 |
Value: "DataCenter-A" |
| 118 |
enable_dynamic_dns: true |
| 119 |
dynamic_dns_domain: "company.com" |
| 120 |
enable_ipv4_dns_from_dhcp: true |
| 121 |
enable_ipv6: true |
| 122 |
enable_ipv6_dns_from_dhcp: true |
| 123 |
state: present |
| 124 |
|
| 125 |
- name: Create a Network Connectivity Policy with static DNS servers |
| 126 |
cisco.intersight.intersight_network_connectivity_policy: |
| 127 |
api_private_key: "{{ api_private_key }}" |
| 128 |
api_key_id: "{{ api_key_id }}" |
| 129 |
name: "StaticDNS-Policy" |
| 130 |
description: "Network connectivity policy with static DNS servers" |
| 131 |
enable_dynamic_dns: false |
| 132 |
enable_ipv4_dns_from_dhcp: false |
| 133 |
preferred_ipv4_dns_server: "8.8.8.8" |
| 134 |
alternate_ipv4_dns_server: "8.8.4.4" |
| 135 |
enable_ipv6: true |
| 136 |
enable_ipv6_dns_from_dhcp: false |
| 137 |
preferred_ipv6_dns_server: "2001:4860:4860::8888" |
| 138 |
alternate_ipv6_dns_server: "2001:4860:4860::8844" |
| 139 |
state: present |
| 140 |
|
| 141 |
- name: Create a basic Network Connectivity Policy with defaults |
| 142 |
cisco.intersight.intersight_network_connectivity_policy: |
| 143 |
api_private_key: "{{ api_private_key }}" |
| 144 |
api_key_id: "{{ api_key_id }}" |
| 145 |
name: "Basic-Network-Policy" |
| 146 |
description: "A basic network connectivity policy" |
| 147 |
state: present |
| 148 |
|
| 149 |
- name: Delete a Network Connectivity Policy |
| 150 |
cisco.intersight.intersight_network_connectivity_policy: |
| 151 |
api_private_key: "{{ api_private_key }}" |
| 152 |
api_key_id: "{{ api_key_id }}" |
| 153 |
name: "DynamicDNS-Policy" |
| 154 |
state: absent |
| 155 |
''' |
| 156 |
|
| 157 |
RETURN = r''' |
| 158 |
api_response: |
| 159 |
description: The API response output returned by the specified resource. |
| 160 |
returned: always |
| 161 |
type: dict |
| 162 |
sample: |
| 163 |
"api_response": { |
| 164 |
"Name": "test_network_connectivity_policy", |
| 165 |
"ObjectType": "networkconfig.Policy", |
| 166 |
"EnableDynamicDns": true, |
| 167 |
"DynamicDnsDomain": "company.com", |
| 168 |
"EnableIpv4dnsFromDhcp": true, |
| 169 |
"EnableIpv6": true, |
| 170 |
"EnableIpv6dnsFromDhcp": true, |
| 171 |
"Tags": [ |
| 172 |
{ |
| 173 |
"Key": "Site", |
| 174 |
"Value": "DataCenter-A" |
| 175 |
} |
| 176 |
] |
| 177 |
} |
| 178 |
''' |
| 179 |
|
| 180 |
|
| 181 |
from ansible.module_utils.basic import AnsibleModule |
| 182 |
from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec |
| 183 |
|
| 184 |
|
| 185 |
def main(): |
| 186 |
argument_spec = intersight_argument_spec.copy() |
| 187 |
argument_spec.update( |
| 188 |
state=dict(type='str', choices=['present', 'absent'], default='present'), |
| 189 |
organization=dict(type='str', default='default'), |
| 190 |
name=dict(type='str', required=True), |
| 191 |
description=dict(type='str', aliases=['descr']), |
| 192 |
tags=dict(type='list', elements='dict'), |
| 193 |
enable_dynamic_dns=dict(type='bool', default=False), |
| 194 |
dynamic_dns_domain=dict(type='str'), |
| 195 |
enable_ipv4_dns_from_dhcp=dict(type='bool', default=False), |
| 196 |
preferred_ipv4_dns_server=dict(type='str', default='0.0.0.0'), |
| 197 |
alternate_ipv4_dns_server=dict(type='str', default='0.0.0.0'), |
| 198 |
enable_ipv6=dict(type='bool', default=False), |
| 199 |
enable_ipv6_dns_from_dhcp=dict(type='bool', default=False), |
| 200 |
preferred_ipv6_dns_server=dict(type='str', default='::'), |
| 201 |
alternate_ipv6_dns_server=dict(type='str', default='::') |
| 202 |
) |
| 203 |
module = AnsibleModule( |
| 204 |
argument_spec, |
| 205 |
supports_check_mode=True |
| 206 |
) |
| 207 |
|
| 208 |
intersight = IntersightModule(module) |
| 209 |
intersight.result['api_response'] = {} |
| 210 |
intersight.result['trace_id'] = '' |
| 211 |
|
| 212 |
# Resource path used to configure policy |
| 213 |
resource_path = '/networkconfig/Policies' |
| 214 |
|
| 215 |
# Define API body used in compares or create |
| 216 |
intersight.api_body = { |
| 217 |
'Organization': { |
| 218 |
'Name': intersight.module.params['organization'], |
| 219 |
}, |
| 220 |
'Name': intersight.module.params['name'], |
| 221 |
'EnableDynamicDns': intersight.module.params['enable_dynamic_dns'], |
| 222 |
'EnableIpv4dnsFromDhcp': intersight.module.params['enable_ipv4_dns_from_dhcp'], |
| 223 |
'EnableIpv6': intersight.module.params['enable_ipv6'], |
| 224 |
} |
| 225 |
|
| 226 |
if intersight.module.params['state'] == 'present': |
| 227 |
intersight.set_tags_and_description() |
| 228 |
|
| 229 |
# Add fields for static DNS servers if not using DHCP |
| 230 |
if not intersight.module.params['enable_ipv4_dns_from_dhcp']: |
| 231 |
intersight.api_body['PreferredIpv4dnsServer'] = intersight.module.params['preferred_ipv4_dns_server'] |
| 232 |
intersight.api_body['AlternateIpv4dnsServer'] = intersight.module.params['alternate_ipv4_dns_server'] |
| 233 |
|
| 234 |
# Add IPv6 fields if enabled |
| 235 |
if intersight.module.params['enable_ipv6']: |
| 236 |
intersight.api_body['EnableIpv6dnsFromDhcp'] = intersight.module.params['enable_ipv6_dns_from_dhcp'] |
| 237 |
|
| 238 |
# Add fields for static DNS servers if not using DHCP for IPv6 |
| 239 |
if not intersight.module.params['enable_ipv6_dns_from_dhcp']: |
| 240 |
intersight.api_body['PreferredIpv6dnsServer'] = intersight.module.params['preferred_ipv6_dns_server'] |
| 241 |
intersight.api_body['AlternateIpv6dnsServer'] = intersight.module.params['alternate_ipv6_dns_server'] |
| 242 |
|
| 243 |
# Add Dynamic DNS fields if enabled and domain is provided |
| 244 |
if intersight.module.params['enable_dynamic_dns'] and intersight.module.params['dynamic_dns_domain']: |
| 245 |
intersight.api_body['DynamicDnsDomain'] = intersight.module.params['dynamic_dns_domain'] |
| 246 |
|
| 247 |
intersight.configure_policy_or_profile(resource_path=resource_path) |
| 248 |
|
| 249 |
module.exit_json(**intersight.result) |
| 250 |
|
| 251 |
|
| 252 |
if __name__ == '__main__': |
| 253 |
main() |