| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright (c) 2018, Simon Weald <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: memset_server_info |
| 11 |
author: "Simon Weald (@glitchcrab)" |
| 12 |
short_description: Retrieve server information |
| 13 |
notes: |
| 14 |
- An API key generated using the Memset customer control panel is needed with the following minimum scope - C(server.info). |
| 15 |
description: |
| 16 |
- Retrieve server information. |
| 17 |
extends_documentation_fragment: |
| 18 |
- community.general._attributes |
| 19 |
- community.general._attributes.info_module |
| 20 |
attributes: |
| 21 |
check_mode: |
| 22 |
version_added: 3.3.0 |
| 23 |
# This was backported to 2.5.4 and 1.3.11 as well, since this was a bugfix |
| 24 |
options: |
| 25 |
api_key: |
| 26 |
required: true |
| 27 |
type: str |
| 28 |
description: |
| 29 |
- The API key obtained from the Memset control panel. |
| 30 |
name: |
| 31 |
required: true |
| 32 |
type: str |
| 33 |
description: |
| 34 |
- The server product name (that is, C(testyaa1)). |
| 35 |
""" |
| 36 |
|
| 37 |
EXAMPLES = r""" |
| 38 |
- name: Get details for testyaa1 |
| 39 |
community.general.memset_server_info: |
| 40 |
name: testyaa1 |
| 41 |
api_key: 5eb86c9896ab03919abcf03857163741 |
| 42 |
delegate_to: localhost |
| 43 |
""" |
| 44 |
|
| 45 |
RETURN = r""" |
| 46 |
memset_api: |
| 47 |
description: Info from the Memset API. |
| 48 |
returned: always |
| 49 |
type: complex |
| 50 |
contains: |
| 51 |
backups: |
| 52 |
description: Whether this server has a backup service. |
| 53 |
returned: always |
| 54 |
type: bool |
| 55 |
sample: true |
| 56 |
control_panel: |
| 57 |
description: Whether the server has a control panel (for example cPanel). |
| 58 |
returned: always |
| 59 |
type: str |
| 60 |
sample: 'cpanel' |
| 61 |
data_zone: |
| 62 |
description: The data zone the server is in. |
| 63 |
returned: always |
| 64 |
type: str |
| 65 |
sample: 'Memset Public Cloud' |
| 66 |
expiry_date: |
| 67 |
description: Current expiry date of the server. |
| 68 |
returned: always |
| 69 |
type: str |
| 70 |
sample: '2018-08-10' |
| 71 |
firewall_rule_group: |
| 72 |
description: Details about the firewall group this server is in. |
| 73 |
returned: always |
| 74 |
type: dict |
| 75 |
sample: |
| 76 |
{ |
| 77 |
"default_outbound_policy": "RETURN", |
| 78 |
"name": "testyaa-fw1", |
| 79 |
"nickname": "testyaa cPanel rules", |
| 80 |
"notes": "", |
| 81 |
"public": false, |
| 82 |
"rules": { |
| 83 |
"51d7db54d39c3544ef7c48baa0b9944f": { |
| 84 |
"action": "ACCEPT", |
| 85 |
"comment": "", |
| 86 |
"dest_ip6s": "any", |
| 87 |
"dest_ips": "any", |
| 88 |
"dest_ports": "any", |
| 89 |
"direction": "Inbound", |
| 90 |
"ip_version": "any", |
| 91 |
"ordering": 2, |
| 92 |
"protocols": "icmp", |
| 93 |
"rule_group_name": "testyaa-fw1", |
| 94 |
"rule_id": "51d7db54d39c3544ef7c48baa0b9944f", |
| 95 |
"source_ip6s": "any", |
| 96 |
"source_ips": "any", |
| 97 |
"source_ports": "any" |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
firewall_type: |
| 102 |
description: The type of firewall the server has (for example self-managed, managed). |
| 103 |
returned: always |
| 104 |
type: str |
| 105 |
sample: 'managed' |
| 106 |
host_name: |
| 107 |
description: The server's hostname. |
| 108 |
returned: always |
| 109 |
type: str |
| 110 |
sample: 'testyaa1.miniserver.com' |
| 111 |
ignore_monitoring_off: |
| 112 |
description: When true, Memset does not remind the customer that monitoring is disabled. |
| 113 |
returned: always |
| 114 |
type: bool |
| 115 |
sample: true |
| 116 |
ips: |
| 117 |
description: List of dictionaries of all IP addresses assigned to the server. |
| 118 |
returned: always |
| 119 |
type: list |
| 120 |
sample: |
| 121 |
[ |
| 122 |
{ |
| 123 |
"address": "1.2.3.4", |
| 124 |
"bytes_in_today": 1000.0, |
| 125 |
"bytes_in_yesterday": 2000.0, |
| 126 |
"bytes_out_today": 1000.0, |
| 127 |
"bytes_out_yesterday": 2000.0 |
| 128 |
} |
| 129 |
] |
| 130 |
monitor: |
| 131 |
description: Whether the server has monitoring enabled. |
| 132 |
returned: always |
| 133 |
type: bool |
| 134 |
sample: true |
| 135 |
monitoring_level: |
| 136 |
description: The server's monitoring level (for example V(basic)). |
| 137 |
returned: always |
| 138 |
type: str |
| 139 |
sample: 'basic' |
| 140 |
name: |
| 141 |
description: Server name (same as the service name). |
| 142 |
returned: always |
| 143 |
type: str |
| 144 |
sample: 'testyaa1' |
| 145 |
network_zones: |
| 146 |
description: The network zone(s) the server is in. |
| 147 |
returned: always |
| 148 |
type: list |
| 149 |
sample: ["reading"] |
| 150 |
nickname: |
| 151 |
description: Customer-set nickname for the server. |
| 152 |
returned: always |
| 153 |
type: str |
| 154 |
sample: 'database server' |
| 155 |
no_auto_reboot: |
| 156 |
description: Whether or not to reboot the server if monitoring detects it down. |
| 157 |
returned: always |
| 158 |
type: bool |
| 159 |
sample: true |
| 160 |
no_nrpe: |
| 161 |
description: Whether Memset should use NRPE to monitor this server. |
| 162 |
returned: always |
| 163 |
type: bool |
| 164 |
sample: true |
| 165 |
os: |
| 166 |
description: The server's Operating System. |
| 167 |
returned: always |
| 168 |
type: str |
| 169 |
sample: 'debian_stretch_64' |
| 170 |
penetration_patrol: |
| 171 |
description: Intrusion detection support level for this server. |
| 172 |
returned: always |
| 173 |
type: str |
| 174 |
sample: 'managed' |
| 175 |
penetration_patrol_alert_level: |
| 176 |
description: The alert level at which notifications are sent. |
| 177 |
returned: always |
| 178 |
type: int |
| 179 |
sample: 10 |
| 180 |
primary_ip: |
| 181 |
description: Server's primary IP. |
| 182 |
returned: always |
| 183 |
type: str |
| 184 |
sample: '1.2.3.4' |
| 185 |
renewal_price_amount: |
| 186 |
description: Renewal cost for the server. |
| 187 |
returned: always |
| 188 |
type: str |
| 189 |
sample: '30.00' |
| 190 |
renewal_price_currency: |
| 191 |
description: Currency for renewal payments. |
| 192 |
returned: always |
| 193 |
type: str |
| 194 |
sample: 'GBP' |
| 195 |
renewal_price_vat: |
| 196 |
description: VAT rate for renewal payments. |
| 197 |
returned: always |
| 198 |
type: str |
| 199 |
sample: '20' |
| 200 |
start_date: |
| 201 |
description: Server's start date. |
| 202 |
returned: always |
| 203 |
type: str |
| 204 |
sample: '2013-04-10' |
| 205 |
status: |
| 206 |
description: Current status of the server (for example live, onhold). |
| 207 |
returned: always |
| 208 |
type: str |
| 209 |
sample: 'LIVE' |
| 210 |
support_level: |
| 211 |
description: Support level included with the server. |
| 212 |
returned: always |
| 213 |
type: str |
| 214 |
sample: 'managed' |
| 215 |
type: |
| 216 |
description: What this server is (for example V(dedicated)). |
| 217 |
returned: always |
| 218 |
type: str |
| 219 |
sample: 'miniserver' |
| 220 |
vlans: |
| 221 |
description: Dictionary of tagged and untagged VLANs this server is in. |
| 222 |
returned: always |
| 223 |
type: dict |
| 224 |
sample: |
| 225 |
{ |
| 226 |
"tagged": [], |
| 227 |
"untagged": [ |
| 228 |
"testyaa-vlan1", |
| 229 |
"testyaa-vlan2" |
| 230 |
] |
| 231 |
} |
| 232 |
vulnscan: |
| 233 |
description: Vulnerability scanning level. |
| 234 |
returned: always |
| 235 |
type: str |
| 236 |
sample: 'basic' |
| 237 |
""" |
| 238 |
|
| 239 |
from ansible.module_utils.basic import AnsibleModule |
| 240 |
|
| 241 |
from ansible_collections.community.general.plugins.module_utils._memset import memset_api_call |
| 242 |
|
| 243 |
|
| 244 |
def get_facts(args=None): |
| 245 |
""" |
| 246 |
Performs a simple API call and returns a JSON blob. |
| 247 |
""" |
| 248 |
retvals, payload = dict(), dict() |
| 249 |
has_changed, has_failed = False, False |
| 250 |
|
| 251 |
payload["name"] = args["name"] |
| 252 |
|
| 253 |
api_method = "server.info" |
| 254 |
has_failed, msg, response = memset_api_call(api_key=args["api_key"], api_method=api_method, payload=payload) |
| 255 |
|
| 256 |
if has_failed: |
| 257 |
# this is the first time the API is called; incorrect credentials will |
| 258 |
# manifest themselves at this point so we need to ensure the user is |
| 259 |
# informed of the reason. |
| 260 |
retvals["failed"] = has_failed |
| 261 |
retvals["msg"] = msg |
| 262 |
if response.status_code is not None: |
| 263 |
retvals["stderr"] = f"API returned an error: {response.status_code}" |
| 264 |
else: |
| 265 |
retvals["stderr"] = f"{response.stderr}" |
| 266 |
return retvals |
| 267 |
|
| 268 |
# we don't want to return the same thing twice |
| 269 |
memset_api = response.json() |
| 270 |
|
| 271 |
retvals["changed"] = has_changed |
| 272 |
retvals["failed"] = has_failed |
| 273 |
retvals["msg"] = None |
| 274 |
retvals["memset_api"] = memset_api |
| 275 |
|
| 276 |
return retvals |
| 277 |
|
| 278 |
|
| 279 |
def main(): |
| 280 |
global module |
| 281 |
module = AnsibleModule( |
| 282 |
argument_spec=dict(api_key=dict(required=True, type="str", no_log=True), name=dict(required=True, type="str")), |
| 283 |
supports_check_mode=True, |
| 284 |
) |
| 285 |
|
| 286 |
# populate the dict with the user-provided vars. |
| 287 |
args = dict(module.params) |
| 288 |
|
| 289 |
retvals = get_facts(args) |
| 290 |
|
| 291 |
if retvals["failed"]: |
| 292 |
module.fail_json(**retvals) |
| 293 |
else: |
| 294 |
module.exit_json(**retvals) |
| 295 |
|
| 296 |
|
| 297 |
if __name__ == "__main__": |
| 298 |
main() |