2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# This module is proudly sponsored by CGI (www.cgi.com) and
|
|
|
|
# KPN (www.kpn.com).
|
2022-08-08 23:23:08 +02:00
|
|
|
# Copyright (c) Ansible project
|
2022-08-05 12:28:29 +02:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: icinga2_host
|
|
|
|
short_description: Manage a host in Icinga2
|
|
|
|
description:
|
|
|
|
- "Add or remove a host to Icinga2 through the API."
|
|
|
|
- "See U(https://www.icinga.com/docs/icinga2/latest/doc/12-icinga2-api/)"
|
|
|
|
author: "Jurgen Brand (@t794104)"
|
[PR #5963/673c79f6 backport][stable-6] Add attributes to apache2, cobbler, dimensiondata, icinga2, lxca, pritunl, and spectrum modules (#6059)
Add attributes to apache2, cobbler, dimensiondata, icinga2, lxca, pritunl, and spectrum modules (#5963)
Add attributes to apache2, cobbler, dimensiondata, icinga2, lxca, pritunl, and spectrum modules.
(cherry picked from commit 673c79f6d9744595ddbef21c2754ab88603ab456)
Co-authored-by: Felix Fontein <felix@fontein.de>
2023-02-24 11:05:04 +01:00
|
|
|
attributes:
|
|
|
|
check_mode:
|
|
|
|
support: full
|
|
|
|
diff_mode:
|
|
|
|
support: none
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
|
|
|
url:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- HTTP, HTTPS, or FTP URL in the form (http|https|ftp)://[user[:pass]]@host.domain[:port]/path
|
|
|
|
use_proxy:
|
|
|
|
description:
|
2022-08-24 19:59:30 +02:00
|
|
|
- If C(false), it will not use a proxy, even if one is defined in
|
2020-03-09 10:11:07 +01:00
|
|
|
an environment variable on the target hosts.
|
|
|
|
type: bool
|
2022-08-24 19:59:30 +02:00
|
|
|
default: true
|
2020-03-09 10:11:07 +01:00
|
|
|
validate_certs:
|
|
|
|
description:
|
2022-08-24 19:59:30 +02:00
|
|
|
- If C(false), SSL certificates will not be validated. This should only be used
|
2020-03-09 10:11:07 +01:00
|
|
|
on personally controlled sites using self-signed certificates.
|
|
|
|
type: bool
|
2022-08-24 19:59:30 +02:00
|
|
|
default: true
|
2020-03-09 10:11:07 +01:00
|
|
|
url_username:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- The username for use in HTTP basic authentication.
|
|
|
|
- This parameter can be used without C(url_password) for sites that allow empty passwords.
|
|
|
|
url_password:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- The password for use in HTTP basic authentication.
|
|
|
|
- If the C(url_username) parameter is not specified, the C(url_password) parameter will not be used.
|
|
|
|
force_basic_auth:
|
|
|
|
description:
|
|
|
|
- httplib2, the library used by the uri module only sends authentication information when a webservice
|
|
|
|
responds to an initial request with a 401 status. Since some basic auth services do not properly
|
|
|
|
send a 401, logins will fail. This option forces the sending of the Basic authentication header
|
|
|
|
upon initial request.
|
|
|
|
type: bool
|
2022-08-24 19:59:30 +02:00
|
|
|
default: false
|
2020-03-09 10:11:07 +01:00
|
|
|
client_cert:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: path
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- PEM formatted certificate chain file to be used for SSL client
|
|
|
|
authentication. This file can also include the key as well, and if
|
|
|
|
the key is included, C(client_key) is not required.
|
|
|
|
client_key:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: path
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- PEM formatted file that contains your private key to be used for SSL
|
|
|
|
client authentication. If C(client_cert) contains both the certificate
|
|
|
|
and key, this option is not required.
|
|
|
|
state:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Apply feature state.
|
|
|
|
choices: [ "present", "absent" ]
|
|
|
|
default: present
|
|
|
|
name:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Name used to create / delete the host. This does not need to be the FQDN, but does needs to be unique.
|
|
|
|
required: true
|
2021-02-26 11:37:23 +01:00
|
|
|
aliases: [host]
|
2020-03-09 10:11:07 +01:00
|
|
|
zone:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- The zone from where this host should be polled.
|
|
|
|
template:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- The template used to define the host.
|
|
|
|
- Template cannot be modified after object creation.
|
|
|
|
check_command:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- The command used to check if the host is alive.
|
|
|
|
default: "hostalive"
|
|
|
|
display_name:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- The name used to display the host.
|
2020-11-23 22:07:03 +01:00
|
|
|
- If not specified, it defaults to the value of the I(name) parameter.
|
2020-03-09 10:11:07 +01:00
|
|
|
ip:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- The IP address of the host.
|
|
|
|
required: true
|
|
|
|
variables:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: dict
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2020-12-16 07:27:24 +01:00
|
|
|
- Dictionary of variables.
|
2020-11-23 22:07:03 +01:00
|
|
|
extends_documentation_fragment:
|
[PR #5963/673c79f6 backport][stable-6] Add attributes to apache2, cobbler, dimensiondata, icinga2, lxca, pritunl, and spectrum modules (#6059)
Add attributes to apache2, cobbler, dimensiondata, icinga2, lxca, pritunl, and spectrum modules (#5963)
Add attributes to apache2, cobbler, dimensiondata, icinga2, lxca, pritunl, and spectrum modules.
(cherry picked from commit 673c79f6d9744595ddbef21c2754ab88603ab456)
Co-authored-by: Felix Fontein <felix@fontein.de>
2023-02-24 11:05:04 +01:00
|
|
|
- ansible.builtin.url
|
|
|
|
- community.general.attributes
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- name: Add host to icinga
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.icinga2_host:
|
2020-03-09 10:11:07 +01:00
|
|
|
url: "https://icinga2.example.com"
|
|
|
|
url_username: "ansible"
|
|
|
|
url_password: "a_secret"
|
|
|
|
state: present
|
|
|
|
name: "{{ ansible_fqdn }}"
|
|
|
|
ip: "{{ ansible_default_ipv4.address }}"
|
2020-12-16 07:27:24 +01:00
|
|
|
variables:
|
|
|
|
foo: "bar"
|
2020-03-09 10:11:07 +01:00
|
|
|
delegate_to: 127.0.0.1
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
name:
|
|
|
|
description: The name used to create, modify or delete the host
|
|
|
|
type: str
|
|
|
|
returned: always
|
|
|
|
data:
|
|
|
|
description: The data structure used for create, modify or delete of the host
|
|
|
|
type: dict
|
|
|
|
returned: always
|
|
|
|
'''
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible.module_utils.urls import fetch_url, url_argument_spec
|
|
|
|
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# Icinga2 API class
|
|
|
|
#
|
|
|
|
class icinga2_api:
|
|
|
|
module = None
|
|
|
|
|
|
|
|
def __init__(self, module):
|
|
|
|
self.module = module
|
|
|
|
|
|
|
|
def call_url(self, path, data='', method='GET'):
|
|
|
|
headers = {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'X-HTTP-Method-Override': method,
|
|
|
|
}
|
|
|
|
url = self.module.params.get("url") + "/" + path
|
|
|
|
rsp, info = fetch_url(module=self.module, url=url, data=data, headers=headers, method=method, use_proxy=self.module.params['use_proxy'])
|
|
|
|
body = ''
|
|
|
|
if rsp:
|
|
|
|
body = json.loads(rsp.read())
|
|
|
|
if info['status'] >= 400:
|
|
|
|
body = info['body']
|
|
|
|
return {'code': info['status'], 'data': body}
|
|
|
|
|
|
|
|
def check_connection(self):
|
|
|
|
ret = self.call_url('v1/status')
|
|
|
|
if ret['code'] == 200:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def exists(self, hostname):
|
|
|
|
data = {
|
|
|
|
"filter": "match(\"" + hostname + "\", host.name)",
|
|
|
|
}
|
|
|
|
ret = self.call_url(
|
|
|
|
path="v1/objects/hosts",
|
|
|
|
data=self.module.jsonify(data)
|
|
|
|
)
|
|
|
|
if ret['code'] == 200:
|
|
|
|
if len(ret['data']['results']) == 1:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def create(self, hostname, data):
|
|
|
|
ret = self.call_url(
|
|
|
|
path="v1/objects/hosts/" + hostname,
|
|
|
|
data=self.module.jsonify(data),
|
|
|
|
method="PUT"
|
|
|
|
)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def delete(self, hostname):
|
|
|
|
data = {"cascade": 1}
|
|
|
|
ret = self.call_url(
|
|
|
|
path="v1/objects/hosts/" + hostname,
|
|
|
|
data=self.module.jsonify(data),
|
|
|
|
method="DELETE"
|
|
|
|
)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def modify(self, hostname, data):
|
|
|
|
ret = self.call_url(
|
|
|
|
path="v1/objects/hosts/" + hostname,
|
|
|
|
data=self.module.jsonify(data),
|
|
|
|
method="POST"
|
|
|
|
)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def diff(self, hostname, data):
|
|
|
|
ret = self.call_url(
|
|
|
|
path="v1/objects/hosts/" + hostname,
|
|
|
|
method="GET"
|
|
|
|
)
|
|
|
|
changed = False
|
|
|
|
ic_data = ret['data']['results'][0]
|
|
|
|
for key in data['attrs']:
|
|
|
|
if key not in ic_data['attrs'].keys():
|
|
|
|
changed = True
|
|
|
|
elif data['attrs'][key] != ic_data['attrs'][key]:
|
|
|
|
changed = True
|
|
|
|
return changed
|
|
|
|
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# Module execution.
|
|
|
|
#
|
|
|
|
def main():
|
|
|
|
# use the predefined argument spec for url
|
|
|
|
argument_spec = url_argument_spec()
|
|
|
|
# add our own arguments
|
|
|
|
argument_spec.update(
|
|
|
|
state=dict(default="present", choices=["absent", "present"]),
|
|
|
|
name=dict(required=True, aliases=['host']),
|
|
|
|
zone=dict(),
|
|
|
|
template=dict(default=None),
|
|
|
|
check_command=dict(default="hostalive"),
|
|
|
|
display_name=dict(default=None),
|
|
|
|
ip=dict(required=True),
|
|
|
|
variables=dict(type='dict', default=None),
|
|
|
|
)
|
|
|
|
|
|
|
|
# Define the main module
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=argument_spec,
|
|
|
|
supports_check_mode=True
|
|
|
|
)
|
|
|
|
|
|
|
|
state = module.params["state"]
|
|
|
|
name = module.params["name"]
|
|
|
|
zone = module.params["zone"]
|
2023-04-20 06:59:05 +02:00
|
|
|
template = []
|
2020-03-09 10:11:07 +01:00
|
|
|
if module.params["template"]:
|
2023-04-20 06:59:05 +02:00
|
|
|
template = [module.params["template"]]
|
2020-03-09 10:11:07 +01:00
|
|
|
check_command = module.params["check_command"]
|
|
|
|
ip = module.params["ip"]
|
|
|
|
display_name = module.params["display_name"]
|
|
|
|
if not display_name:
|
|
|
|
display_name = name
|
|
|
|
variables = module.params["variables"]
|
|
|
|
|
|
|
|
try:
|
|
|
|
icinga = icinga2_api(module=module)
|
|
|
|
icinga.check_connection()
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg="unable to connect to Icinga. Exception message: %s" % (e))
|
|
|
|
|
|
|
|
data = {
|
2023-04-20 06:59:05 +02:00
|
|
|
'templates': template,
|
2020-03-09 10:11:07 +01:00
|
|
|
'attrs': {
|
|
|
|
'address': ip,
|
|
|
|
'display_name': display_name,
|
|
|
|
'check_command': check_command,
|
|
|
|
'zone': zone,
|
2023-04-20 06:59:05 +02:00
|
|
|
'vars.made_by': "ansible"
|
2020-03-09 10:11:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-20 06:59:05 +02:00
|
|
|
for key, value in variables.items():
|
|
|
|
data['attrs']['vars.' + key] = value
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
changed = False
|
|
|
|
if icinga.exists(name):
|
|
|
|
if state == "absent":
|
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=True, name=name, data=data)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
ret = icinga.delete(name)
|
|
|
|
if ret['code'] == 200:
|
|
|
|
changed = True
|
|
|
|
else:
|
2020-12-02 07:38:56 +01:00
|
|
|
module.fail_json(msg="bad return code (%s) deleting host: '%s'" % (ret['code'], ret['data']))
|
2020-03-09 10:11:07 +01:00
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg="exception deleting host: " + str(e))
|
|
|
|
|
|
|
|
elif icinga.diff(name, data):
|
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=False, name=name, data=data)
|
|
|
|
|
|
|
|
# Template attribute is not allowed in modification
|
2023-06-20 19:23:06 +02:00
|
|
|
del data['templates']
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
ret = icinga.modify(name, data)
|
|
|
|
|
|
|
|
if ret['code'] == 200:
|
|
|
|
changed = True
|
|
|
|
else:
|
2020-12-02 07:38:56 +01:00
|
|
|
module.fail_json(msg="bad return code (%s) modifying host: '%s'" % (ret['code'], ret['data']))
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
else:
|
|
|
|
if state == "present":
|
|
|
|
if module.check_mode:
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
ret = icinga.create(name, data)
|
|
|
|
if ret['code'] == 200:
|
|
|
|
changed = True
|
|
|
|
else:
|
2020-12-02 07:38:56 +01:00
|
|
|
module.fail_json(msg="bad return code (%s) creating host: '%s'" % (ret['code'], ret['data']))
|
2020-03-09 10:11:07 +01:00
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg="exception creating host: " + str(e))
|
|
|
|
|
|
|
|
module.exit_json(changed=changed, name=name, data=data)
|
|
|
|
|
|
|
|
|
|
|
|
# import module snippets
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|