Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # Copyright (c) 2017, Loic Blot <[email protected]>
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6 from __future__ import annotations
7
8 DOCUMENTATION = r"""
9 module: logstash_plugin
10 short_description: Manage Logstash plugins
11 description:
12 - Manages Logstash plugins.
13 author: Loic Blot (@nerzhul)
14 extends_documentation_fragment:
15 - community.general._attributes
16 attributes:
17 check_mode:
18 support: full
19 diff_mode:
20 support: none
21 options:
22 name:
23 type: str
24 description:
25 - Install plugin with that name.
26 required: true
27 state:
28 type: str
29 description:
30 - Apply plugin state.
31 choices: ["present", "absent"]
32 default: present
33 plugin_bin:
34 type: path
35 description:
36 - Specify logstash-plugin to use for plugin management.
37 default: /usr/share/logstash/bin/logstash-plugin
38 proxy_host:
39 type: str
40 description:
41 - Proxy host to use during plugin installation.
42 - Can be specified as a hostname (for example, V(myproxy.example.com)) or as a URL (for example, V(http://myproxy.example.com)).
43 When specified without a scheme, V(http://) is assumed.
44 - Sets the O(proxy_host):O(proxy_port) combination as the E(http_proxy) and E(https_proxy) environment variables
45 when running the C(logstash-plugin) command.
46 proxy_port:
47 type: str
48 description:
49 - Proxy port to use during plugin installation.
50 version:
51 type: str
52 description:
53 - Specify version of the plugin to install. If the plugin exists with a previous version, it is B(not) updated.
54 """
55
56 EXAMPLES = r"""
57 - name: Install Logstash beats input plugin
58 community.general.logstash_plugin:
59 state: present
60 name: logstash-input-beats
61
62 - name: Install specific version of a plugin
63 community.general.logstash_plugin:
64 state: present
65 name: logstash-input-syslog
66 version: '3.2.0'
67
68 - name: Uninstall Logstash plugin
69 community.general.logstash_plugin:
70 state: absent
71 name: logstash-filter-multiline
72
73 - name: Install Logstash plugin with alternate heap size
74 community.general.logstash_plugin:
75 state: present
76 name: logstash-input-beats
77 environment:
78 LS_JAVA_OPTS: "-Xms256m -Xmx256m"
79 """
80
81 from ansible.module_utils.basic import AnsibleModule
82
83 PACKAGE_STATE_MAP = dict(present="install", absent="remove")
84
85
86 def is_plugin_present(module, plugin_bin, plugin_name):
87 cmd_args = [plugin_bin, "list", plugin_name]
88 rc, out, err = module.run_command(cmd_args)
89 return rc == 0
90
91
92 def parse_error(string):
93 reason = "reason: "
94 try:
95 return string[string.index(reason) + len(reason) :].strip()
96 except ValueError:
97 return string
98
99
100 def install_plugin(module, plugin_bin, plugin_name, version, proxy_host, proxy_port):
101 cmd_args = [plugin_bin, PACKAGE_STATE_MAP["present"]]
102
103 if version:
104 cmd_args.extend(["--version", version])
105
106 cmd_args.append(plugin_name)
107
108 environ_update = {}
109 if proxy_host and proxy_port:
110 scheme = proxy_host if "://" in proxy_host else f"http://{proxy_host}"
111 proxy_url = f"{scheme}:{proxy_port}"
112 environ_update = {"http_proxy": proxy_url, "https_proxy": proxy_url}
113
114 cmd = " ".join(cmd_args)
115
116 if module.check_mode:
117 rc, out, err = 0, "check mode", ""
118 else:
119 rc, out, err = module.run_command(cmd_args, environ_update=environ_update)
120
121 if rc != 0:
122 reason = parse_error(out)
123 module.fail_json(msg=reason, stderr=err)
124
125 return True, cmd, out, err
126
127
128 def remove_plugin(module, plugin_bin, plugin_name):
129 cmd_args = [plugin_bin, PACKAGE_STATE_MAP["absent"], plugin_name]
130
131 cmd = " ".join(cmd_args)
132
133 if module.check_mode:
134 rc, out, err = 0, "check mode", ""
135 else:
136 rc, out, err = module.run_command(cmd_args)
137
138 if rc != 0:
139 reason = parse_error(out)
140 module.fail_json(msg=reason, stderr=err)
141
142 return True, cmd, out, err
143
144
145 def main():
146 module = AnsibleModule(
147 argument_spec=dict(
148 name=dict(required=True),
149 state=dict(default="present", choices=list(PACKAGE_STATE_MAP.keys())),
150 plugin_bin=dict(default="/usr/share/logstash/bin/logstash-plugin", type="path"),
151 proxy_host=dict(),
152 proxy_port=dict(),
153 version=dict(),
154 ),
155 supports_check_mode=True,
156 )
157 module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"}
158
159 name = module.params["name"]
160 state = module.params["state"]
161 plugin_bin = module.params["plugin_bin"]
162 proxy_host = module.params["proxy_host"]
163 proxy_port = module.params["proxy_port"]
164 version = module.params["version"]
165
166 present = is_plugin_present(module, plugin_bin, name)
167
168 # skip if the state is correct
169 if (present and state == "present") or (state == "absent" and not present):
170 module.exit_json(changed=False, name=name, state=state)
171
172 if state == "present":
173 changed, cmd, out, err = install_plugin(module, plugin_bin, name, version, proxy_host, proxy_port)
174 elif state == "absent":
175 changed, cmd, out, err = remove_plugin(module, plugin_bin, name)
176
177 module.exit_json(changed=changed, cmd=cmd, name=name, state=state, stdout=out, stderr=err)
178
179
180 if __name__ == "__main__":
181 main()