90 | 91 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,182 @@ |
0 |
+#!/usr/bin/python |
|
1 |
+# -*- coding: utf-8 -*- |
|
2 |
+ |
|
3 |
+""" |
|
4 |
+Ansible module to manage elasticsearch plugins |
|
5 |
+(c) 2017, Loic Blot <loic.blot@unix-experience.fr> |
|
6 |
+ |
|
7 |
+This file is part of Ansible |
|
8 |
+ |
|
9 |
+Ansible is free software: you can redistribute it and/or modify |
|
10 |
+it under the terms of the GNU General Public License as published by |
|
11 |
+the Free Software Foundation, either version 3 of the License, or |
|
12 |
+(at your option) any later version. |
|
13 |
+ |
|
14 |
+Ansible is distributed in the hope that it will be useful, |
|
15 |
+but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 |
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17 |
+GNU General Public License for more details. |
|
18 |
+You should have received a copy of the GNU General Public License |
|
19 |
+along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
|
20 |
+""" |
|
21 |
+ |
|
22 |
+ANSIBLE_METADATA = {'status': ['preview'], |
|
23 |
+ 'supported_by': 'community', |
|
24 |
+ 'version': '1.0'} |
|
25 |
+ |
|
26 |
+DOCUMENTATION = ''' |
|
27 |
+--- |
|
28 |
+module: logstash_plugin |
|
29 |
+short_description: Manage Logstash plugins |
|
30 |
+description: |
|
31 |
+ - Manages Logstash plugins. |
|
32 |
+version_added: "2.3" |
|
33 |
+author: Loic Blot (@nerzhul) |
|
34 |
+options: |
|
35 |
+ name: |
|
36 |
+ description: |
|
37 |
+ - Install plugin with that name. |
|
38 |
+ required: True |
|
39 |
+ state: |
|
40 |
+ description: |
|
41 |
+ - Apply plugin state. |
|
42 |
+ required: False |
|
43 |
+ choices: ["present", "absent"] |
|
44 |
+ default: present |
|
45 |
+ plugin_bin: |
|
46 |
+ description: |
|
47 |
+ - Specify logstash-plugin to use for plugin management. |
|
48 |
+ required: False |
|
49 |
+ default: /usr/share/logstash/bin/logstash-plugin |
|
50 |
+ proxy_host: |
|
51 |
+ description: |
|
52 |
+ - Proxy host to use during plugin installation. |
|
53 |
+ required: False |
|
54 |
+ default: None |
|
55 |
+ proxy_port: |
|
56 |
+ description: |
|
57 |
+ - Proxy port to use during plugin installation. |
|
58 |
+ required: False |
|
59 |
+ default: None |
|
60 |
+ version: |
|
61 |
+ description: |
|
62 |
+ - Specify plugin Version of the plugin to install. |
|
63 |
+ If plugin exists with previous version, it will NOT be updated. |
|
64 |
+ required: False |
|
65 |
+ default: None |
|
66 |
+''' |
|
67 |
+ |
|
68 |
+EXAMPLES = ''' |
|
69 |
+- name: Install Logstash beats input plugin |
|
70 |
+ logstash_plugin: |
|
71 |
+ state: present |
|
72 |
+ name: logstash-input-beats |
|
73 |
+ |
|
74 |
+- name: Install specific version of a plugin |
|
75 |
+ logstash_plugin: |
|
76 |
+ state: present |
|
77 |
+ name: logstash-input-syslog |
|
78 |
+ version: '3.2.0' |
|
79 |
+ |
|
80 |
+- name: Uninstall Logstash plugin |
|
81 |
+ logstash_plugin: |
|
82 |
+ state: absent |
|
83 |
+ name: logstash-filter-multiline |
|
84 |
+''' |
|
85 |
+ |
|
86 |
+PACKAGE_STATE_MAP = dict( |
|
87 |
+ present="install", |
|
88 |
+ absent="remove" |
|
89 |
+) |
|
90 |
+ |
|
91 |
+from ansible.module_utils.basic import AnsibleModule |
|
92 |
+ |
|
93 |
+def is_plugin_present(module, plugin_bin, plugin_name): |
|
94 |
+ cmd_args = [plugin_bin, "list", plugin_name] |
|
95 |
+ rc, out, err = module.run_command(" ".join(cmd_args)) |
|
96 |
+ return rc == 0 |
|
97 |
+ |
|
98 |
+ |
|
99 |
+def parse_error(string): |
|
100 |
+ reason = "reason: " |
|
101 |
+ try: |
|
102 |
+ return string[string.index(reason) + len(reason):].strip() |
|
103 |
+ except ValueError: |
|
104 |
+ return string |
|
105 |
+ |
|
106 |
+ |
|
107 |
+def install_plugin(module, plugin_bin, plugin_name, version, proxy_host, proxy_port): |
|
108 |
+ cmd_args = [plugin_bin, PACKAGE_STATE_MAP["present"], plugin_name] |
|
109 |
+ |
|
110 |
+ if version: |
|
111 |
+ cmd_args.append("--version %s" % version) |
|
112 |
+ |
|
113 |
+ if proxy_host and proxy_port: |
|
114 |
+ cmd_args.append("-DproxyHost=%s -DproxyPort=%s" % (proxy_host, proxy_port)) |
|
115 |
+ |
|
116 |
+ cmd = " ".join(cmd_args) |
|
117 |
+ |
|
118 |
+ if module.check_mode: |
|
119 |
+ rc, out, err = 0, "check mode", "" |
|
120 |
+ else: |
|
121 |
+ rc, out, err = module.run_command(cmd) |
|
122 |
+ |
|
123 |
+ if rc != 0: |
|
124 |
+ reason = parse_error(out) |
|
125 |
+ module.fail_json(msg=reason) |
|
126 |
+ |
|
127 |
+ return True, cmd, out, err |
|
128 |
+ |
|
129 |
+ |
|
130 |
+def remove_plugin(module, plugin_bin, plugin_name): |
|
131 |
+ cmd_args = [plugin_bin, PACKAGE_STATE_MAP["absent"], plugin_name] |
|
132 |
+ |
|
133 |
+ cmd = " ".join(cmd_args) |
|
134 |
+ |
|
135 |
+ if module.check_mode: |
|
136 |
+ rc, out, err = 0, "check mode", "" |
|
137 |
+ else: |
|
138 |
+ rc, out, err = module.run_command(cmd) |
|
139 |
+ |
|
140 |
+ if rc != 0: |
|
141 |
+ reason = parse_error(out) |
|
142 |
+ module.fail_json(msg=reason) |
|
143 |
+ |
|
144 |
+ return True, cmd, out, err |
|
145 |
+ |
|
146 |
+ |
|
147 |
+def main(): |
|
148 |
+ module = AnsibleModule( |
|
149 |
+ argument_spec=dict( |
|
150 |
+ name=dict(required=True), |
|
151 |
+ state=dict(default="present", choices=PACKAGE_STATE_MAP.keys()), |
|
152 |
+ plugin_bin=dict(default="/usr/share/logstash/bin/logstash-plugin", type="path"), |
|
153 |
+ proxy_host=dict(default=None), |
|
154 |
+ proxy_port=dict(default=None), |
|
155 |
+ version=dict(default=None) |
|
156 |
+ ), |
|
157 |
+ supports_check_mode=True |
|
158 |
+ ) |
|
159 |
+ |
|
160 |
+ name = module.params["name"] |
|
161 |
+ state = module.params["state"] |
|
162 |
+ plugin_bin = module.params["plugin_bin"] |
|
163 |
+ proxy_host = module.params["proxy_host"] |
|
164 |
+ proxy_port = module.params["proxy_port"] |
|
165 |
+ version = module.params["version"] |
|
166 |
+ |
|
167 |
+ present = is_plugin_present(module, plugin_bin, name) |
|
168 |
+ |
|
169 |
+ # skip if the state is correct |
|
170 |
+ if (present and state == "present") or (state == "absent" and not present): |
|
171 |
+ module.exit_json(changed=False, name=name, state=state) |
|
172 |
+ |
|
173 |
+ if state == "present": |
|
174 |
+ changed, cmd, out, err = install_plugin(module, plugin_bin, name, version, proxy_host, proxy_port) |
|
175 |
+ elif state == "absent": |
|
176 |
+ changed, cmd, out, err = remove_plugin(module, plugin_bin, name) |
|
177 |
+ |
|
178 |
+ module.exit_json(changed=changed, cmd=cmd, name=name, state=state, stdout=out, stderr=err) |
|
179 |
+ |
|
180 |
+if __name__ == '__main__': |
|
181 |
+ main() |