feat: add ilm policy
This commit is contained in:
parent
7bf3927302
commit
bcac417a5f
4 changed files with 99 additions and 0 deletions
|
@ -5,6 +5,10 @@ Which is based on [Keep A Changelog](http://keepachangelog.com/)
|
|||
|
||||
## [Unreleased]
|
||||
|
||||
### Added
|
||||
|
||||
- feat: add ilm policy
|
||||
|
||||
### Changed
|
||||
|
||||
- replace kitchen to molecule
|
||||
|
|
10
README.md
10
README.md
|
@ -37,6 +37,16 @@ Install and configure Elasticsearch
|
|||
format: YYYY-MM-dd
|
||||
```
|
||||
|
||||
* `elasticsearch_ilm_policies` - hash with the ilm policies configuration
|
||||
|
||||
```
|
||||
autoclean:
|
||||
delete:
|
||||
min_age: 30d
|
||||
actions:
|
||||
delete: {}
|
||||
```
|
||||
|
||||
## How to use
|
||||
|
||||
```
|
||||
|
|
75
library/elasticsearch_ilm_policy.py
Normal file
75
library/elasticsearch_ilm_policy.py
Normal file
|
@ -0,0 +1,75 @@
|
|||
#!/usr/bin/python
|
||||
|
||||
from ansible.module_utils.basic import *
|
||||
from ansible.module_utils.elasticsearch_api import *
|
||||
|
||||
class ElasticsearchILMPolicy:
|
||||
def __init__(self, api, name):
|
||||
self.api = api
|
||||
self.name = name
|
||||
|
||||
def get_data(self):
|
||||
status_code, data = self.api.get('_ilm/policy/{}'.format(self.name))
|
||||
if status_code == 200:
|
||||
return data[self.name]
|
||||
else:
|
||||
return {}
|
||||
|
||||
def create(self, phases):
|
||||
self.api.put(
|
||||
'_ilm/policy/{}'.format(self.name),
|
||||
{ 'policy': { 'phases': phases } }
|
||||
)
|
||||
|
||||
def dict_has_changed(self, new_data, old_data):
|
||||
for option, value in new_data.items():
|
||||
if not option in old_data:
|
||||
if value:
|
||||
return True
|
||||
|
||||
elif type(value) is dict:
|
||||
if self.dict_has_changed(value, old_data[option]):
|
||||
return True
|
||||
|
||||
elif type(value) is list:
|
||||
if not type(data[option]) is list:
|
||||
return True
|
||||
if old_data[option].sort() != value.sort():
|
||||
return True
|
||||
|
||||
elif str(old_data[option]) != str(value):
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def has_changed(self, phases):
|
||||
old_data = self.get_data()
|
||||
if not old_data or self.dict_has_changed(phases, old_data['policy']['phases']):
|
||||
self.create(phases)
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def main():
|
||||
fields = {
|
||||
'name': { 'type': 'str', 'required': True },
|
||||
'phases': { 'type': 'dict', 'default': {} },
|
||||
'api_url': { 'type': 'str', 'default': 'http://127.0.0.1:9200' },
|
||||
'api_user': { 'type': 'str', 'default': None },
|
||||
'api_password': { 'type': 'str', 'default': None, 'no_log': True },
|
||||
}
|
||||
module = AnsibleModule(argument_spec=fields)
|
||||
|
||||
api = ElasticsearchApi(
|
||||
module.params['api_url'],
|
||||
module.params['api_user'],
|
||||
module.params['api_password']
|
||||
)
|
||||
|
||||
ilm = ElasticsearchILMPolicy(api, module.params['name'])
|
||||
changed = ilm.has_changed(module.params['phases'])
|
||||
|
||||
module.exit_json(changed=changed)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
|
@ -1,4 +1,14 @@
|
|||
---
|
||||
- name: copy ilm policies
|
||||
elasticsearch_ilm_policy:
|
||||
name: '{{ item.key }}'
|
||||
phases: '{{ item.value|default({}) }}'
|
||||
loop: '{{ elasticsearch_ilm_policies|dict2items }}'
|
||||
loop_control:
|
||||
label: '{{ item.key }}'
|
||||
run_once: true
|
||||
tags: elasticsearch
|
||||
|
||||
- name: copy index templates
|
||||
elasticsearch_template:
|
||||
name: '{{ item.key }}'
|
||||
|
|
Loading…
Add table
Reference in a new issue