| 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 |
|
| 14 |
DOCUMENTATION = r''' |
| 15 |
--- |
| 16 |
module: intersight_syslog_policy |
| 17 |
short_description: Syslog Policy configuration for Cisco Intersight |
| 18 |
description: |
| 19 |
- Manages Syslog Policy configuration on Cisco Intersight. |
| 20 |
- This policy configures local logging severity and up to two remote syslog server destinations for managed devices. |
| 21 |
- For more information see L(Cisco Intersight,https://intersight.com/apidocs/syslog/Policy/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 |
type: str |
| 35 |
default: default |
| 36 |
name: |
| 37 |
description: |
| 38 |
- The name assigned to the Syslog Policy. |
| 39 |
type: str |
| 40 |
required: true |
| 41 |
description: |
| 42 |
description: |
| 43 |
- The user-defined description for the Syslog Policy. |
| 44 |
type: str |
| 45 |
aliases: [descr] |
| 46 |
tags: |
| 47 |
description: |
| 48 |
- List of tags in Key:<user-defined key> Value:<user-defined value> format. |
| 49 |
type: list |
| 50 |
elements: dict |
| 51 |
local_logging_minimum_severity: |
| 52 |
description: |
| 53 |
- Lowest level of messages to be included in the local log. |
| 54 |
type: str |
| 55 |
choices: ['warning', 'emergency', 'alert', 'critical', 'error', 'notice', 'informational', 'debug'] |
| 56 |
default: 'warning' |
| 57 |
first_remote_logging_enabled: |
| 58 |
description: |
| 59 |
- If C(true), enables the first remote syslog server destination. |
| 60 |
type: bool |
| 61 |
default: false |
| 62 |
first_remote_logging_hostname: |
| 63 |
description: |
| 64 |
- Hostname or IP Address of the first syslog server where log should be stored. |
| 65 |
- This parameter is required if C(first_remote_logging_enabled) is C(true). |
| 66 |
type: str |
| 67 |
first_remote_logging_port: |
| 68 |
description: |
| 69 |
- Port number used for logging on first syslog server. |
| 70 |
type: int |
| 71 |
default: 514 |
| 72 |
first_remote_logging_protocol: |
| 73 |
description: |
| 74 |
- Transport layer protocol for transmission of log messages to first syslog server. |
| 75 |
type: str |
| 76 |
choices: ['udp', 'tcp'] |
| 77 |
default: 'udp' |
| 78 |
first_remote_logging_minimum_severity: |
| 79 |
description: |
| 80 |
- Lowest level of messages to be included in the first remote log. |
| 81 |
type: str |
| 82 |
choices: ['warning', 'emergency', 'alert', 'critical', 'error', 'notice', 'informational', 'debug'] |
| 83 |
default: 'warning' |
| 84 |
second_remote_logging_enabled: |
| 85 |
description: |
| 86 |
- If C(true), enables the second remote syslog server destination. |
| 87 |
type: bool |
| 88 |
default: false |
| 89 |
second_remote_logging_hostname: |
| 90 |
description: |
| 91 |
- Hostname or IP Address of the second syslog server where log should be stored. |
| 92 |
- This parameter is required if C(second_remote_logging_enabled) is C(true). |
| 93 |
type: str |
| 94 |
second_remote_logging_port: |
| 95 |
description: |
| 96 |
- Port number used for logging on second syslog server. |
| 97 |
type: int |
| 98 |
default: 514 |
| 99 |
second_remote_logging_protocol: |
| 100 |
description: |
| 101 |
- Transport layer protocol for transmission of log messages to second syslog server. |
| 102 |
type: str |
| 103 |
choices: ['udp', 'tcp'] |
| 104 |
default: 'udp' |
| 105 |
second_remote_logging_minimum_severity: |
| 106 |
description: |
| 107 |
- Lowest level of messages to be included in the second remote log. |
| 108 |
type: str |
| 109 |
choices: ['warning', 'emergency', 'alert', 'critical', 'error', 'notice', 'informational', 'debug'] |
| 110 |
default: 'warning' |
| 111 |
author: |
| 112 |
- Ron Gershburg (@rgershbu) |
| 113 |
''' |
| 114 |
|
| 115 |
EXAMPLES = r''' |
| 116 |
- name: Create a Syslog Policy with one remote server enabled |
| 117 |
cisco.intersight.intersight_syslog_policy: |
| 118 |
api_private_key: "{{ api_private_key }}" |
| 119 |
api_key_id: "{{ api_key_id }}" |
| 120 |
name: "Syslog-Policy-PROD" |
| 121 |
description: "Syslog policy for production servers" |
| 122 |
tags: |
| 123 |
- Key: "Env" |
| 124 |
Value: "Production" |
| 125 |
local_logging_minimum_severity: "notice" |
| 126 |
first_remote_logging_enabled: true |
| 127 |
first_remote_logging_hostname: "10.10.10.50" |
| 128 |
first_remote_logging_port: 514 |
| 129 |
first_remote_logging_protocol: "udp" |
| 130 |
first_remote_logging_minimum_severity: "informational" |
| 131 |
|
| 132 |
- name: Create a Syslog Policy with only local logging configured |
| 133 |
cisco.intersight.intersight_syslog_policy: |
| 134 |
api_private_key: "{{ api_private_key }}" |
| 135 |
api_key_id: "{{ api_key_id }}" |
| 136 |
name: "Syslog-Policy-Local-Only" |
| 137 |
description: "Send only critical local logs" |
| 138 |
local_logging_minimum_severity: "critical" |
| 139 |
|
| 140 |
- name: Delete a Syslog Policy |
| 141 |
cisco.intersight.intersight_syslog_policy: |
| 142 |
api_private_key: "{{ api_private_key }}" |
| 143 |
api_key_id: "{{ api_key_id }}" |
| 144 |
name: "Syslog-Policy-PROD" |
| 145 |
state: absent |
| 146 |
''' |
| 147 |
|
| 148 |
RETURN = r''' |
| 149 |
api_response: |
| 150 |
description: The API response output returned by the specified resource. |
| 151 |
returned: always |
| 152 |
type: dict |
| 153 |
sample: |
| 154 |
"api_response": { |
| 155 |
"Name": "Syslog-Policy-Local-Only", |
| 156 |
"ObjectType": "syslog.Policy", |
| 157 |
"Tags": [], |
| 158 |
"LocalClients": [{ |
| 159 |
"ClassId": "syslog.LocalFileLoggingClient", |
| 160 |
"ObjectType": "syslog.LocalFileLoggingClient", |
| 161 |
"MinSeverity": "critical" |
| 162 |
}], |
| 163 |
} |
| 164 |
''' |
| 165 |
|
| 166 |
|
| 167 |
from ansible.module_utils.basic import AnsibleModule |
| 168 |
from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec |
| 169 |
|
| 170 |
|
| 171 |
def validate_input(module: AnsibleModule): |
| 172 |
first_remote_logging_port = module.params['first_remote_logging_port'] |
| 173 |
second_remote_logging_port = module.params['second_remote_logging_port'] |
| 174 |
|
| 175 |
if 0 > first_remote_logging_port or first_remote_logging_port > 65535: |
| 176 |
module.fail_json(msg=f"first_remote_logging_port has to be between 0 and 65535, current value is: {first_remote_logging_port}") |
| 177 |
|
| 178 |
if 0 > second_remote_logging_port or second_remote_logging_port > 65535: |
| 179 |
module.fail_json(msg=f"second_remote_logging_port has to be between 0 and 65535, current value is: {second_remote_logging_port}") |
| 180 |
|
| 181 |
|
| 182 |
def customize_hostname_default(intersight: IntersightModule): |
| 183 |
# Create a custom default |
| 184 |
if not intersight.module.params['first_remote_logging_hostname']: |
| 185 |
intersight.module.params['first_remote_logging_hostname'] = "0.0.0.0" |
| 186 |
|
| 187 |
if not intersight.module.params['second_remote_logging_hostname']: |
| 188 |
intersight.module.params['second_remote_logging_hostname'] = "0.0.0.0" |
| 189 |
|
| 190 |
|
| 191 |
def main(): |
| 192 |
argument_spec = intersight_argument_spec.copy() |
| 193 |
argument_spec.update( |
| 194 |
state=dict(type='str', choices=['present', 'absent'], default='present'), |
| 195 |
organization=dict(type='str', default='default'), |
| 196 |
name=dict(type='str', required=True), |
| 197 |
description=dict(type='str', aliases=['descr']), |
| 198 |
tags=dict(type='list', elements='dict'), |
| 199 |
local_logging_minimum_severity=dict( |
| 200 |
type='str', |
| 201 |
choices=['warning', 'emergency', 'alert', 'critical', 'error', 'notice', 'informational', 'debug'], |
| 202 |
default='warning' |
| 203 |
), |
| 204 |
first_remote_logging_enabled=dict(type='bool', default=False), |
| 205 |
first_remote_logging_hostname=dict(type='str'), |
| 206 |
first_remote_logging_port=dict(type='int', default=514), |
| 207 |
first_remote_logging_protocol=dict( |
| 208 |
type='str', |
| 209 |
choices=['udp', 'tcp'], |
| 210 |
default='udp' |
| 211 |
), |
| 212 |
first_remote_logging_minimum_severity=dict( |
| 213 |
type='str', |
| 214 |
choices=['warning', 'emergency', 'alert', 'critical', 'error', 'notice', 'informational', 'debug'], |
| 215 |
default='warning' |
| 216 |
), |
| 217 |
second_remote_logging_enabled=dict(type='bool', default=False), |
| 218 |
second_remote_logging_hostname=dict(type='str'), |
| 219 |
second_remote_logging_port=dict(type='int', default=514), |
| 220 |
second_remote_logging_protocol=dict( |
| 221 |
type='str', |
| 222 |
choices=['udp', 'tcp'], |
| 223 |
default='udp' |
| 224 |
), |
| 225 |
second_remote_logging_minimum_severity=dict( |
| 226 |
type='str', |
| 227 |
choices=['warning', 'emergency', 'alert', 'critical', 'error', 'notice', 'informational', 'debug'], |
| 228 |
default='warning' |
| 229 |
) |
| 230 |
|
| 231 |
) |
| 232 |
required_if = [ |
| 233 |
('first_remote_logging_enabled', True, ['first_remote_logging_hostname']), |
| 234 |
('second_remote_logging_enabled', True, ['second_remote_logging_hostname']) |
| 235 |
] |
| 236 |
|
| 237 |
module = AnsibleModule( |
| 238 |
argument_spec, |
| 239 |
required_if=required_if, |
| 240 |
supports_check_mode=True, |
| 241 |
) |
| 242 |
|
| 243 |
validate_input(module) |
| 244 |
intersight = IntersightModule(module) |
| 245 |
intersight.result['api_response'] = {} |
| 246 |
intersight.result['trace_id'] = '' |
| 247 |
|
| 248 |
customize_hostname_default(intersight) |
| 249 |
|
| 250 |
# Resource path used to configure policy |
| 251 |
resource_path = '/syslog/Policies' |
| 252 |
# Define API body used in compares or create |
| 253 |
intersight.api_body = { |
| 254 |
'Organization': { |
| 255 |
'Name': intersight.module.params['organization'], |
| 256 |
}, |
| 257 |
'Name': intersight.module.params['name'], |
| 258 |
'LocalClients': [ |
| 259 |
{ |
| 260 |
'ObjectType': 'syslog.LocalFileLoggingClient', |
| 261 |
"MinSeverity": intersight.module.params['local_logging_minimum_severity'] |
| 262 |
} |
| 263 |
], |
| 264 |
'RemoteClients': [ |
| 265 |
{ |
| 266 |
'Type': 'syslog.RemoteLoggingClient', |
| 267 |
'Enabled': intersight.module.params['first_remote_logging_enabled'], |
| 268 |
'ObjectType': 'syslog.RemoteLoggingClient', |
| 269 |
'Hostname': intersight.module.params['first_remote_logging_hostname'], |
| 270 |
'Port': intersight.module.params['first_remote_logging_port'], |
| 271 |
'Protocol': intersight.module.params['first_remote_logging_protocol'], |
| 272 |
'MinSeverity': intersight.module.params['first_remote_logging_minimum_severity'] |
| 273 |
}, |
| 274 |
{ |
| 275 |
'Type': 'syslog.RemoteLoggingClient', |
| 276 |
'Enabled': intersight.module.params['second_remote_logging_enabled'], |
| 277 |
'ObjectType': 'syslog.RemoteLoggingClient', |
| 278 |
'Hostname': intersight.module.params['second_remote_logging_hostname'], |
| 279 |
'Port': intersight.module.params['second_remote_logging_port'], |
| 280 |
'Protocol': intersight.module.params['second_remote_logging_protocol'], |
| 281 |
'MinSeverity': intersight.module.params['second_remote_logging_minimum_severity'] |
| 282 |
} |
| 283 |
], |
| 284 |
} |
| 285 |
|
| 286 |
intersight.set_tags_and_description() |
| 287 |
|
| 288 |
intersight.configure_policy_or_profile(resource_path=resource_path) |
| 289 |
|
| 290 |
module.exit_json(**intersight.result) |
| 291 |
|
| 292 |
|
| 293 |
if __name__ == '__main__': |
| 294 |
main() |