| 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_imc_access_policy |
| 16 |
short_description: IMC Access Policy configuration for Cisco Intersight |
| 17 |
description: |
| 18 |
- IMC Access Policy configuration for Cisco Intersight. |
| 19 |
- Used to configure IP addresses and VLAN used for external connectivity to Cisco IMC. |
| 20 |
- For more information see L(Cisco Intersight,https://intersight.com/apidocs). |
| 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 and Policies 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 IMC Access Policy. |
| 39 |
- The name must be between 1 and 62 alphanumeric characters, allowing special characters :-_. |
| 40 |
type: str |
| 41 |
required: true |
| 42 |
tags: |
| 43 |
description: |
| 44 |
- List of tags in Key:<user-defined key> Value:<user-defined value> format. |
| 45 |
type: list |
| 46 |
elements: dict |
| 47 |
description: |
| 48 |
description: |
| 49 |
- The user-defined description of the IMC access policy. |
| 50 |
- Description can contain letters(a-z, A-Z), numbers(0-9), hyphen(-), period(.), colon(:), or an underscore(_). |
| 51 |
type: str |
| 52 |
aliases: [descr] |
| 53 |
out_of_band: |
| 54 |
description: |
| 55 |
- If C(true), will enable Out-Of-Band configuration. |
| 56 |
- If C(false), will enable In-Band configuration. |
| 57 |
type: bool |
| 58 |
default: false |
| 59 |
vlan_id: |
| 60 |
description: |
| 61 |
- VLAN to be used for server access over Inband network. |
| 62 |
- Required if C(out_of_band) is C(false). |
| 63 |
type: int |
| 64 |
ip_pool: |
| 65 |
description: |
| 66 |
- IP Pool used to assign IP address and other required network settings. |
| 67 |
type: dict |
| 68 |
required: true |
| 69 |
suboptions: |
| 70 |
name: |
| 71 |
description: |
| 72 |
- Name of the IP Pool used to assign IP address and other required network settings. |
| 73 |
type: str |
| 74 |
required: true |
| 75 |
organization: |
| 76 |
description: |
| 77 |
- The name of the Organization that owns the IP Pool. |
| 78 |
- Defaults to the value of C(organization) when omitted. |
| 79 |
- Set this when the IMC Access Policy should consume an IP Pool from a different Organization. |
| 80 |
type: str |
| 81 |
author: |
| 82 |
- David Soper (@dsoper2) |
| 83 |
''' |
| 84 |
|
| 85 |
EXAMPLES = r''' |
| 86 |
- name: Configure IMC Access policy |
| 87 |
intersight_imc_access_policy: |
| 88 |
api_private_key: "{{ api_private_key }}" |
| 89 |
api_key_id: "{{ api_key_id }}" |
| 90 |
name: sjc02-d23-access |
| 91 |
description: IMC access for SJC02 rack D23 |
| 92 |
tags: |
| 93 |
- Site: D23 |
| 94 |
vlan_id: 131 |
| 95 |
ip_pool: |
| 96 |
name: sjc02-d23-ext-mgmt |
| 97 |
|
| 98 |
- name: Configure IMC Access policy with IP pool from another organization |
| 99 |
intersight_imc_access_policy: |
| 100 |
api_private_key: "{{ api_private_key }}" |
| 101 |
api_key_id: "{{ api_key_id }}" |
| 102 |
organization: policy-org |
| 103 |
name: sjc02-d23-access |
| 104 |
vlan_id: 131 |
| 105 |
ip_pool: |
| 106 |
name: shared-ext-mgmt |
| 107 |
organization: shared-org |
| 108 |
|
| 109 |
- name: Delete IMC Access policy |
| 110 |
intersight_imc_access_policy: |
| 111 |
api_private_key: "{{ api_private_key }}" |
| 112 |
api_key_id: "{{ api_key_id }}" |
| 113 |
name: sjc02-d23-access |
| 114 |
state: absent |
| 115 |
''' |
| 116 |
|
| 117 |
RETURN = r''' |
| 118 |
api_response: |
| 119 |
description: The API response output returned by the specified resource. |
| 120 |
returned: always |
| 121 |
type: dict |
| 122 |
sample: |
| 123 |
"api_response": { |
| 124 |
"Name": "sjc02-d23-access", |
| 125 |
"ObjectType": "access.Policy", |
| 126 |
"Profiles": [ |
| 127 |
{ |
| 128 |
"Moid": "5e4ec7ae77696e2d30840cfc", |
| 129 |
"ObjectType": "server.Profile", |
| 130 |
}, |
| 131 |
{ |
| 132 |
"Moid": "5e84d78777696e2d302ec195", |
| 133 |
"ObjectType": "server.Profile", |
| 134 |
} |
| 135 |
], |
| 136 |
"Tags": [ |
| 137 |
{ |
| 138 |
"Key": "Site", |
| 139 |
"Value": "SJC02" |
| 140 |
} |
| 141 |
] |
| 142 |
} |
| 143 |
''' |
| 144 |
|
| 145 |
|
| 146 |
from ansible.module_utils.basic import AnsibleModule |
| 147 |
from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec, compare_values |
| 148 |
|
| 149 |
|
| 150 |
def main(): |
| 151 |
argument_spec = intersight_argument_spec.copy() |
| 152 |
argument_spec.update( |
| 153 |
state=dict(type='str', choices=['present', 'absent'], default='present'), |
| 154 |
organization=dict(type='str', default='default'), |
| 155 |
name=dict(type='str', required=True), |
| 156 |
description=dict(type='str', aliases=['descr']), |
| 157 |
tags=dict(type='list', elements='dict'), |
| 158 |
out_of_band=dict(type='bool', default=False), |
| 159 |
vlan_id=dict(type='int'), |
| 160 |
ip_pool=dict( |
| 161 |
type='dict', |
| 162 |
required=True, |
| 163 |
options=dict( |
| 164 |
name=dict(type='str', required=True), |
| 165 |
organization=dict(type='str'), |
| 166 |
), |
| 167 |
), |
| 168 |
) |
| 169 |
|
| 170 |
module = AnsibleModule( |
| 171 |
argument_spec, |
| 172 |
required_if=[ |
| 173 |
('out_of_band', False, ['vlan_id']), |
| 174 |
], |
| 175 |
supports_check_mode=True, |
| 176 |
) |
| 177 |
|
| 178 |
intersight = IntersightModule(module) |
| 179 |
ip_pool = module.params['ip_pool'] |
| 180 |
ip_pool_organization = ip_pool.get('organization') or module.params['organization'] |
| 181 |
|
| 182 |
organization_moid = intersight.get_moid_by_name( |
| 183 |
resource_path='/organization/Organizations', |
| 184 |
resource_name=module.params['organization'], |
| 185 |
) |
| 186 |
if not organization_moid: |
| 187 |
module.fail_json(msg=f"Organization '{module.params['organization']}' not found") |
| 188 |
|
| 189 |
ip_pool_moid = intersight.get_moid_by_name_and_org( |
| 190 |
resource_path='/ippool/Pools', |
| 191 |
resource_name=ip_pool['name'], |
| 192 |
organization_name=ip_pool_organization, |
| 193 |
) |
| 194 |
if not ip_pool_moid: |
| 195 |
module.fail_json( |
| 196 |
msg=f"IP pool '{ip_pool['name']}' not found in organization '{ip_pool_organization}'" |
| 197 |
) |
| 198 |
|
| 199 |
intersight.result['api_response'] = {} |
| 200 |
intersight.result['trace_id'] = '' |
| 201 |
intersight.api_body = { |
| 202 |
'Name': intersight.module.params['name'], |
| 203 |
|
| 204 |
'Organization': { |
| 205 |
'Name': intersight.module.params['organization'], |
| 206 |
}, |
| 207 |
} |
| 208 |
if module.params['state'] == 'present': |
| 209 |
intersight.set_tags_and_description() |
| 210 |
|
| 211 |
if intersight.module.params['out_of_band']: |
| 212 |
intersight.api_body['ConfigurationType'] = { |
| 213 |
'ObjectType': 'access.ConfigurationType', |
| 214 |
'ConfigureInband': False, |
| 215 |
'ConfigureOutOfBand': True, |
| 216 |
} |
| 217 |
intersight.api_body['OutOfBandIpPool'] = { |
| 218 |
'ObjectType': 'ippool.Pool', |
| 219 |
'Moid': ip_pool_moid, |
| 220 |
} |
| 221 |
else: |
| 222 |
intersight.api_body['InbandVlan'] = intersight.module.params['vlan_id'] |
| 223 |
intersight.api_body['ConfigurationType'] = { |
| 224 |
'ObjectType': 'access.ConfigurationType', |
| 225 |
'ConfigureInband': True, |
| 226 |
'ConfigureOutOfBand': False, |
| 227 |
} |
| 228 |
intersight.api_body['InbandIpPool'] = { |
| 229 |
'ObjectType': 'ippool.Pool', |
| 230 |
'Moid': ip_pool_moid, |
| 231 |
} |
| 232 |
|
| 233 |
# get the current state of the resource |
| 234 |
filter_str = "Name eq '" + intersight.module.params['name'] + "'" |
| 235 |
filter_str += "and Organization.Moid eq '" + organization_moid + "'" |
| 236 |
intersight.get_resource( |
| 237 |
resource_path='/access/Policies', |
| 238 |
query_params={ |
| 239 |
'$filter': filter_str, |
| 240 |
}, |
| 241 |
) |
| 242 |
|
| 243 |
moid = None |
| 244 |
resource_values_match = False |
| 245 |
if intersight.result['api_response'].get('Moid'): |
| 246 |
# resource exists and moid was returned |
| 247 |
moid = intersight.result['api_response']['Moid'] |
| 248 |
if module.params['state'] == 'present': |
| 249 |
resource_values_match = compare_values(intersight.api_body, intersight.result['api_response']) |
| 250 |
else: # state == 'absent' |
| 251 |
intersight.delete_resource( |
| 252 |
moid=moid, |
| 253 |
resource_path='/access/Policies', |
| 254 |
) |
| 255 |
moid = None |
| 256 |
|
| 257 |
if module.params['state'] == 'present' and not resource_values_match: |
| 258 |
# remove read-only Organization key |
| 259 |
intersight.api_body.pop('Organization') |
| 260 |
if not moid: |
| 261 |
# Organization must be set, but can't be changed after initial POST |
| 262 |
intersight.api_body['Organization'] = { |
| 263 |
'Moid': organization_moid, |
| 264 |
} |
| 265 |
intersight.configure_resource( |
| 266 |
moid=moid, |
| 267 |
resource_path='/access/Policies', |
| 268 |
body=intersight.api_body, |
| 269 |
query_params={ |
| 270 |
'$filter': filter_str, |
| 271 |
}, |
| 272 |
) |
| 273 |
|
| 274 |
module.exit_json(**intersight.result) |
| 275 |
|
| 276 |
|
| 277 |
if __name__ == '__main__': |
| 278 |
main() |