Debian Codegraph @

ansible_collections/community/general/plugins/modules/icinga2_feature.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2016, Loic Blot <[email protected]>
4 # Copyright (c) 2018, Ansible Project
5 # Sponsored by Infopro Digital. http://www.infopro-digital.com/
6 # Sponsored by E.T.A.I. http://www.etai.fr/
7 #
8 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
9 # SPDX-License-Identifier: GPL-3.0-or-later
10
11 from __future__ import annotations
12
13 DOCUMENTATION = r"""
14 module: icinga2_feature
15
16 short_description: Manage Icinga2 feature
17 description:
18 - This module can be used to enable or disable an Icinga2 feature.
19 author: "Loic Blot (@nerzhul)"
20 extends_documentation_fragment:
21 - community.general._attributes
22 attributes:
23 check_mode:
24 support: full
25 diff_mode:
26 support: none
27 options:
28 name:
29 type: str
30 description:
31 - This is the feature name to enable or disable.
32 required: true
33 state:
34 type: str
35 description:
36 - If set to V(present) and feature is disabled, then feature is enabled.
37 - If set to V(present) and feature is already enabled, then nothing is changed.
38 - If set to V(absent) and feature is enabled, then feature is disabled.
39 - If set to V(absent) and feature is already disabled, then nothing is changed.
40 choices: ["present", "absent"]
41 default: present
42 """
43
44 EXAMPLES = r"""
45 - name: Enable ido-pgsql feature
46 community.general.icinga2_feature:
47 name: ido-pgsql
48 state: present
49
50 - name: Disable api feature
51 community.general.icinga2_feature:
52 name: api
53 state: absent
54 """
55
56 RETURN = r"""
57 #
58 """
59
60 import re
61
62 from ansible.module_utils.basic import AnsibleModule
63
64
65 class Icinga2FeatureHelper:
66 def __init__(self, module):
67 self.module = module
68 self._icinga2 = module.get_bin_path("icinga2", True)
69 self.feature_name = self.module.params["name"]
70 self.state = self.module.params["state"]
71
72 def _exec(self, args):
73 cmd = [self._icinga2, "feature"]
74 rc, out, err = self.module.run_command(cmd + args, check_rc=True)
75 return rc, out
76
77 def manage(self):
78 rc, out = self._exec(["list"])
79 if rc != 0:
80 self.module.fail_json(
81 msg="Unable to list icinga2 features. Ensure icinga2 is installed and present in binary path."
82 )
83
84 # If feature is already in good state, just exit
85 if (re.search(f"Disabled features:.* {self.feature_name}[ \n]", out) and self.state == "absent") or (
86 re.search(f"Enabled features:.* {self.feature_name}[ \n]", out) and self.state == "present"
87 ):
88 self.module.exit_json(changed=False)
89
90 if self.module.check_mode:
91 self.module.exit_json(changed=True)
92
93 feature_enable_str = "enable" if self.state == "present" else "disable"
94
95 rc, out = self._exec([feature_enable_str, self.feature_name])
96
97 change_applied = False
98 if self.state == "present":
99 if rc != 0:
100 self.module.fail_json(
101 msg=f"Failed to {feature_enable_str} feature {self.feature_name}. icinga2 command returned {out}"
102 )
103
104 if re.search("already enabled", out) is None:
105 change_applied = True
106 else:
107 if rc == 0:
108 change_applied = True
109 # RC is not 0 for this already disabled feature, handle it as no change applied
110 elif re.search(f"Cannot disable feature '{self.feature_name}'. Target file .* does not exist", out):
111 change_applied = False
112 else:
113 self.module.fail_json(msg=f"Failed to disable feature. Command returns {out}")
114
115 self.module.exit_json(changed=change_applied)
116
117
118 def main():
119 module = AnsibleModule(
120 argument_spec=dict(
121 name=dict(type="str", required=True),
122 state=dict(type="str", choices=["present", "absent"], default="present"),
123 ),
124 supports_check_mode=True,
125 )
126
127 module.run_command_environ_update = dict(LANGUAGE="C", LC_ALL="C")
128 Icinga2FeatureHelper(module).manage()
129
130
131 if __name__ == "__main__":
132 main()