| 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_ssh_policy |
| 16 |
short_description: SSH Policy configuration for Cisco Intersight |
| 17 |
description: |
| 18 |
- Manages SSH Policy configuration on Cisco Intersight. |
| 19 |
- A policy to configure SSH service settings on Cisco Intersight managed servers. |
| 20 |
- This policy is applicable only for UCS Servers (Standalone). |
| 21 |
- For more information see L(Cisco Intersight,https://intersight.com/apidocs/ssh/Policies/get/). |
| 22 |
extends_documentation_fragment: intersight |
| 23 |
options: |
| 24 |
state: |
| 25 |
description: |
| 26 |
- If C(present), will verify the resource is present and will create if needed. |
| 27 |
- If C(absent), will verify the resource is absent and will delete if needed. |
| 28 |
type: str |
| 29 |
choices: [present, absent] |
| 30 |
default: present |
| 31 |
organization: |
| 32 |
description: |
| 33 |
- The name of the Organization this resource is assigned to. |
| 34 |
- Profiles and Policies that are created within a Custom Organization are applicable only to devices in the same Organization. |
| 35 |
type: str |
| 36 |
default: default |
| 37 |
name: |
| 38 |
description: |
| 39 |
- The name assigned to the SSH Policy. |
| 40 |
- The name must be between 1 and 62 alphanumeric characters, allowing special characters :-_. |
| 41 |
type: str |
| 42 |
required: true |
| 43 |
description: |
| 44 |
description: |
| 45 |
- The user-defined description for the SSH Policy. |
| 46 |
- Description can contain letters(a-z, A-Z), numbers(0-9), hyphen(-), period(.), colon(:), or an underscore(_). |
| 47 |
type: str |
| 48 |
aliases: [descr] |
| 49 |
tags: |
| 50 |
description: |
| 51 |
- List of tags in Key:<user-defined key> Value:<user-defined value> format. |
| 52 |
type: list |
| 53 |
elements: dict |
| 54 |
enable_ssh: |
| 55 |
description: |
| 56 |
- State of SSH service on the endpoint. |
| 57 |
- If set to false, SSH service will be disabled and ssh_port and ssh_timeout will be ignored. |
| 58 |
type: bool |
| 59 |
default: true |
| 60 |
ssh_port: |
| 61 |
description: |
| 62 |
- Port used for secure shell access. |
| 63 |
- Valid range is 1-65535. |
| 64 |
- This parameter is only used when enable_ssh is true. |
| 65 |
type: int |
| 66 |
default: 22 |
| 67 |
ssh_timeout: |
| 68 |
description: |
| 69 |
- Number of seconds to wait before the system considers a SSH request to have timed out. |
| 70 |
- Valid range is 60-10800 seconds. |
| 71 |
- This parameter is only used when enable_ssh is true. |
| 72 |
type: int |
| 73 |
default: 1800 |
| 74 |
author: |
| 75 |
- Ron Gershburg (@rgershbu) |
| 76 |
''' |
| 77 |
|
| 78 |
EXAMPLES = r''' |
| 79 |
- name: Create SSH Policy with default settings |
| 80 |
cisco.intersight.intersight_ssh_policy: |
| 81 |
api_private_key: "{{ api_private_key }}" |
| 82 |
api_key_id: "{{ api_key_id }}" |
| 83 |
organization: "default" |
| 84 |
name: "SSH-Policy-Default" |
| 85 |
description: "SSH policy with default settings" |
| 86 |
tags: |
| 87 |
- Key: "Environment" |
| 88 |
Value: "Production" |
| 89 |
enable_ssh: true |
| 90 |
state: present |
| 91 |
|
| 92 |
- name: Create SSH Policy with custom port and timeout |
| 93 |
cisco.intersight.intersight_ssh_policy: |
| 94 |
api_private_key: "{{ api_private_key }}" |
| 95 |
api_key_id: "{{ api_key_id }}" |
| 96 |
name: "SSH-Policy-Custom" |
| 97 |
description: "SSH policy with custom port and timeout" |
| 98 |
enable_ssh: true |
| 99 |
ssh_port: 2222 |
| 100 |
ssh_timeout: 3600 |
| 101 |
state: present |
| 102 |
|
| 103 |
- name: Create SSH Policy with SSH disabled |
| 104 |
cisco.intersight.intersight_ssh_policy: |
| 105 |
api_private_key: "{{ api_private_key }}" |
| 106 |
api_key_id: "{{ api_key_id }}" |
| 107 |
name: "SSH-Policy-Disabled" |
| 108 |
description: "SSH policy with SSH service disabled" |
| 109 |
enable_ssh: false |
| 110 |
state: present |
| 111 |
|
| 112 |
- name: Delete SSH Policy |
| 113 |
cisco.intersight.intersight_ssh_policy: |
| 114 |
api_private_key: "{{ api_private_key }}" |
| 115 |
api_key_id: "{{ api_key_id }}" |
| 116 |
name: "SSH-Policy-Default" |
| 117 |
state: absent |
| 118 |
''' |
| 119 |
|
| 120 |
RETURN = r''' |
| 121 |
api_response: |
| 122 |
description: The API response output returned by the specified resource. |
| 123 |
returned: always |
| 124 |
type: dict |
| 125 |
sample: |
| 126 |
"api_response": { |
| 127 |
"Name": "SSH-Policy-Default", |
| 128 |
"ObjectType": "ssh.Policy", |
| 129 |
"Enabled": true, |
| 130 |
"Port": 22, |
| 131 |
"Timeout": 1800, |
| 132 |
"Tags": [ |
| 133 |
{ |
| 134 |
"Key": "Environment", |
| 135 |
"Value": "Production" |
| 136 |
} |
| 137 |
] |
| 138 |
} |
| 139 |
''' |
| 140 |
|
| 141 |
|
| 142 |
from ansible.module_utils.basic import AnsibleModule |
| 143 |
from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec |
| 144 |
|
| 145 |
|
| 146 |
def main(): |
| 147 |
argument_spec = intersight_argument_spec.copy() |
| 148 |
argument_spec.update( |
| 149 |
state=dict(type='str', choices=['present', 'absent'], default='present'), |
| 150 |
organization=dict(type='str', default='default'), |
| 151 |
name=dict(type='str', required=True), |
| 152 |
description=dict(type='str', aliases=['descr']), |
| 153 |
tags=dict(type='list', elements='dict'), |
| 154 |
enable_ssh=dict(type='bool', default=True), |
| 155 |
ssh_port=dict(type='int', default=22), |
| 156 |
ssh_timeout=dict(type='int', default=1800) |
| 157 |
) |
| 158 |
module = AnsibleModule( |
| 159 |
argument_spec, |
| 160 |
supports_check_mode=True |
| 161 |
) |
| 162 |
|
| 163 |
# Validate ssh_port range |
| 164 |
if module.params['ssh_port'] < 1 or module.params['ssh_port'] > 65535: |
| 165 |
module.fail_json(msg="ssh_port must be between 1 and 65535") |
| 166 |
|
| 167 |
# Validate ssh_timeout range |
| 168 |
if module.params['ssh_timeout'] < 60 or module.params['ssh_timeout'] > 10800: |
| 169 |
module.fail_json(msg="ssh_timeout must be between 60 and 10800 seconds") |
| 170 |
|
| 171 |
intersight = IntersightModule(module) |
| 172 |
intersight.result['api_response'] = {} |
| 173 |
intersight.result['trace_id'] = '' |
| 174 |
|
| 175 |
# Resource path used to configure policy |
| 176 |
resource_path = '/ssh/Policies' |
| 177 |
|
| 178 |
# Define API body used in compares or create |
| 179 |
intersight.api_body = { |
| 180 |
'Organization': { |
| 181 |
'Name': intersight.module.params['organization'], |
| 182 |
}, |
| 183 |
'Name': intersight.module.params['name'], |
| 184 |
'Enabled': intersight.module.params['enable_ssh'], |
| 185 |
} |
| 186 |
|
| 187 |
if intersight.module.params['state'] == 'present': |
| 188 |
intersight.set_tags_and_description() |
| 189 |
|
| 190 |
# Add SSH port and timeout only if SSH is enabled |
| 191 |
if intersight.module.params['enable_ssh']: |
| 192 |
intersight.api_body['Port'] = intersight.module.params['ssh_port'] |
| 193 |
intersight.api_body['Timeout'] = intersight.module.params['ssh_timeout'] |
| 194 |
|
| 195 |
intersight.configure_policy_or_profile(resource_path=resource_path) |
| 196 |
|
| 197 |
module.exit_json(**intersight.result) |
| 198 |
|
| 199 |
|
| 200 |
if __name__ == '__main__': |
| 201 |
main() |