From 31cc54ddbcac1809177249a4f742f06bf462e943 Mon Sep 17 00:00:00 2001 From: Dale McDiarmid Date: Sun, 24 Jul 2016 01:10:07 +0100 Subject: [PATCH] Templates now invoked as handler to cover edge cases --- handlers/elasticsearch-templates.yml | 16 ++++++++++++++++ handlers/main.yml | 11 ++++++++++- tasks/elasticsearch-templates.yml | 22 +++------------------- 3 files changed, 29 insertions(+), 20 deletions(-) create mode 100644 handlers/elasticsearch-templates.yml diff --git a/handlers/elasticsearch-templates.yml b/handlers/elasticsearch-templates.yml new file mode 100644 index 0000000..9c46eed --- /dev/null +++ b/handlers/elasticsearch-templates.yml @@ -0,0 +1,16 @@ +--- + +- name: Ensure elasticsearch is started + service: name={{instance_init_script | basename}} state=started enabled=yes + +- name: Wait for elasticsearch to startup + wait_for: port={{es_api_port}} delay=10 + +- name: Get template files + shell: find . -maxdepth 1 -type f | sed "s#\./##" | sed "s/.json//" chdir=/etc/elasticsearch/templates + register: resultstemplate + +#The basic auth details here may not be required - send always if they are defined. If not needed they will be ignored. +- name: Install template(s) with auth + command: "curl -sL -XPUT http://{{es_api_host}}:{{es_api_port}}/_template/{{item}} -d @/etc/elasticsearch/templates/{{item}}.json {% if es_api_basic_auth_username is defined and es_api_basic_auth_password is defined%}-u {{es_api_basic_auth_username}}:{{es_api_basic_auth_password}}{% endif %}" + with_items: "{{ resultstemplate.stdout_lines }}" \ No newline at end of file diff --git a/handlers/main.yml b/handlers/main.yml index ee37b75..e98d041 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -5,4 +5,13 @@ - name: load-native-realms include: ./handlers/shield/elasticsearch-shield-native.yml - when: (es_users is defined and es_users.native is defined) or (es_roles is defined and es_roles.native is defined) \ No newline at end of file + when: (es_users is defined and es_users.native is defined) or (es_roles is defined and es_roles.native is defined) + + +#Templates are a handler as they need to come after a restart e.g. suppose user removes shield on a running node and doesn't +#specify es_api_basic_auth_username and es_api_basic_auth_password. The templates will subsequently not be removed if we don't wait for the node to restart. +#Templates done after restart therefore - as a handler. + +- name: load-templates + include: ./handlers/elasticsearch-templates.yml + when: es_templates \ No newline at end of file diff --git a/tasks/elasticsearch-templates.yml b/tasks/elasticsearch-templates.yml index 7649215..76a471d 100644 --- a/tasks/elasticsearch-templates.yml +++ b/tasks/elasticsearch-templates.yml @@ -4,28 +4,12 @@ - name: Copy default templates to elasticsearch copy: src=templates dest=/etc/elasticsearch/ owner={{ es_user }} group={{ es_group }} + notify: load-templates when: es_templates_fileglob is not defined - name: Copy templates to elasticsearch copy: src={{ item }} dest=/etc/elasticsearch/templates owner={{ es_user }} group={{ es_group }} when: es_templates_fileglob is defined + notify: load-templates with_fileglob: - - "{{ es_templates_fileglob }}" - -- name: Ensure elasticsearch is started - service: name={{instance_init_script | basename}} state=started enabled=yes - -- name: Wait for elasticsearch to startup - wait_for: port={{es_api_port}} delay=10 - -- name: Get template files - shell: find . -maxdepth 1 -type f | sed "s#\./##" | sed "s/.json//" chdir=/etc/elasticsearch/templates - register: resultstemplate - -#The basic auth details here may not be required - send always if they are defined. If not needed they will be ignored. -- name: Install template(s) with auth - command: "curl -sL -XPUT http://{{es_api_host}}:{{es_api_port}}/_template/{{item}} -d @/etc/elasticsearch/templates/{{item}}.json {% if es_api_basic_auth_username is defined and es_api_basic_auth_password is defined%}-u {{es_api_basic_auth_username}}:{{es_api_basic_auth_password}}{% endif %}" - with_items: "{{ resultstemplate.stdout_lines }}" - -#Suppose user removes shield on a running node, doesn't specify es_api_basic_auth_username and es_api_basic_auth_password. The templates will subsequently not be removed. -#Templates should probably be done after a restart therefore - as a handler. \ No newline at end of file + - "{{ es_templates_fileglob }}" \ No newline at end of file