Skip to content

Commit

Permalink
Merge pull request elastic#82 from gingerwizard/master
Browse files Browse the repository at this point in the history
Improved Plugin Support, More Tests, Refactors, Consistent Ansible version
  • Loading branch information
gingerwizard committed Mar 21, 2016
2 parents 40fd351 + f55f5c3 commit b7d8300
Show file tree
Hide file tree
Showing 21 changed files with 253 additions and 71 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.kitchen/
.kitchen.local.yml
*.lock
.vendor
.bundle
Expand Down
15 changes: 10 additions & 5 deletions .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,17 @@ platforms:
image: electrical/debian:7.3
privileged: true
provision_command:
- echo 'deb http://http.debian.net/debian/ wheezy-backports main' >> /etc/apt/sources.list
- apt-get update
- apt-get install -y -q ansible
- apt-get update && apt-get -y install python python-dev python-pip build-essential libyaml-dev python-yaml
- pip install ansible
- apt-get install -y -q net-tools
use_sudo: false
- name: debian-8
driver_config:
image: electrical/debian:8
privileged: true
provision_command:
- apt-get update
- apt-get install -y -q ansible
- apt-get update && apt-get -y install python python-dev python-pip build-essential libyaml-dev python-yaml
- pip install ansible
- apt-get install -y -q net-tools
- sed -ri 's/^#?PermitRootLogin .*/PermitRootLogin yes/' /etc/ssh/sshd_config
- sed -ri 's/^#?PasswordAuthentication .*/PasswordAuthentication yes/' /etc/ssh/sshd_config
Expand All @@ -48,11 +47,17 @@ platforms:
driver_config:
image: electrical/centos:6.4-1
privileged: true
provision_command:
- yum -y install gmp-devel python-devel python-pip
- pip install ansible
use_sudo: false
- name: centos-7
driver_config:
image: electrical/centos:7
provision_command:
- yum -y install gmp-devel python-devel python-pip
- yum -y groupinstall "Development Tools"
- pip install ansible
- sed -ri 's/^#?PermitRootLogin .*/PermitRootLogin yes/' /etc/ssh/sshd_config
- sed -ri 's/^#?PasswordAuthentication .*/PasswordAuthentication yes/' /etc/ssh/sshd_config
- sed -ri 's/^#?UsePAM .*/UsePAM no/' /etc/ssh/sshd_config
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ source 'https://rubygems.org'

gem 'test-kitchen'
gem "kitchen-docker", '< 2.2.0'
gem 'kitchen-ansible'
gem 'kitchen-ansible', '0.40.1'
gem 'net-ssh', '~> 2.0'
63 changes: 56 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Ansible role for Elasticsearch. Currently this works on Debian and RedHat based
* Centos 6
* Centos 7

The latest Elasticsearch versions of 1.7.x and 2.1.x are actively tested.
The latest Elasticsearch versions of 1.7.x and 2.x are actively tested.

## Usage

Expand Down Expand Up @@ -54,7 +54,18 @@ The following illustrates applying configuration parameters to an Elasticsearch
hosts: localhost
roles:
#expand to all available parameters
- { role: elasticsearch, es_instance_name: "node1", es_data_dir: "/opt/elasticsearch/data", es_log_dir: "/opt/elasticsearch/logs", es_work_dir: "/opt/elasticsearch/temp", es_config: {node.name: "node1", cluster.name: "custom-cluster", discovery.zen.ping.unicast.hosts: "localhost:9301", http.port: 9201, transport.tcp.port: 9301, node.data: false, node.master: true, bootstrap.mlockall: true, discovery.zen.ping.multicast.enabled: false } }
- { role: elasticsearch, es_instance_name: "node1", es_data_dirs: "/opt/elasticsearch/data", es_log_dir: "/opt/elasticsearch/logs", es_work_dir: "/opt/elasticsearch/temp",
es_config: {
node.name: "node1",
cluster.name: "custom-cluster",
discovery.zen.ping.unicast.hosts: "localhost:9301",
http.port: 9201,
transport.tcp.port: 9301,
node.data: false,
node.master: true,
bootstrap.mlockall: true,
discovery.zen.ping.multicast.enabled: false }
}
vars:
es_scripts: false
es_templates: false
Expand Down Expand Up @@ -84,7 +95,18 @@ A more complex example:
hosts: localhost
roles:
#expand to all available parameters
- { role: elasticsearch, es_instance_name: "node1", es_data_dir: "/opt/elasticsearch/data", es_log_dir: "/opt/elasticsearch/logs", es_work_dir: "/opt/elasticsearch/temp", es_config: {node.name: "node1", cluster.name: "custom-cluster", discovery.zen.ping.unicast.hosts: "localhost:9301", http.port: 9201, transport.tcp.port: 9301, node.data: false, node.master: true, bootstrap.mlockall: true, discovery.zen.ping.multicast.enabled: false } }
- { role: elasticsearch, es_instance_name: "node1", es_data_dirs: "/opt/elasticsearch/data", es_log_dir: "/opt/elasticsearch/logs", es_work_dir: "/opt/elasticsearch/temp",
es_config: {
node.name: "node1",
cluster.name: "custom-cluster",
discovery.zen.ping.unicast.hosts: "localhost:9301",
http.port: 9201,
transport.tcp.port: 9301,
node.data: false,
node.master: true,
bootstrap.mlockall: true,
discovery.zen.ping.multicast.enabled: false }
}
vars:
es_scripts: false
es_templates: false
Expand Down Expand Up @@ -126,8 +148,28 @@ recommended in any multi node cluster configuration.
- hosts: data_nodes
roles:
- { role: elasticsearch, es_instance_name: "node1", es_data_dir: "/opt/elasticsearch", es_config: { "discovery.zen.ping.multicast.enabled": false, discovery.zen.ping.unicast.hosts: "elastic02:9300", http.port: 9200, transport.tcp.port: 9300, node.data: true, node.master: false, bootstrap.mlockall: false, discovery.zen.ping.multicast.enabled: false } }
- { role: elasticsearch, es_instance_name: "node2", es_config: { "discovery.zen.ping.multicast.enabled": false, discovery.zen.ping.unicast.hosts: "elastic02:9300", http.port: 9201, transport.tcp.port: 9301, node.data: true, node.master: false, bootstrap.mlockall: false, discovery.zen.ping.multicast.enabled: false } }
- { role: elasticsearch, es_instance_name: "node1", es_data_dirs: "/opt/elasticsearch",
es_config: {
"discovery.zen.ping.multicast.enabled": false,
discovery.zen.ping.unicast.hosts: "elastic02:9300",
http.port: 9200,
transport.tcp.port: 9300,
node.data: true,
node.master: false,
bootstrap.mlockall: false,
discovery.zen.ping.multicast.enabled: false }
}
- { role: elasticsearch, es_instance_name: "node2",
es_config: {
"discovery.zen.ping.multicast.enabled": false,
discovery.zen.ping.unicast.hosts: "elastic02:9300",
http.port: 9201,
transport.tcp.port: 9301,
node.data: true,
node.master: false,
bootstrap.mlockall: false,
discovery.zen.ping.multicast.enabled: false }
}
vars:
es_scripts: false
es_templates: false
Expand Down Expand Up @@ -159,7 +201,7 @@ Following variables affect the versions installed:
* ```es_version``` (e.g. "1.5.2").
* ```es_start_service``` (true (default) or false)
* ```es_plugins_reinstall``` (true or false (default) )
* ```es_plugins``` (an array of plugin definitons e.g.:
* ```es_plugins``` (an array of plugin definitions e.g.:

```
es_plugins:
Expand All @@ -183,10 +225,12 @@ By default, each node on a host will be installed to use unique pid, plugin, wor
controlled by the following parameters:

* ```es_pid_dir``` - defaults to "/var/run/elasticsearch".
* ```es_data_dir``` - defaults to "/var/lib/elasticsearch".
* ```es_data_dirs``` - defaults to "/var/lib/elasticsearch". This can be a list or comma separated string e.g. ["/opt/elasticsearch/data-1","/opt/elasticsearch/data-2"] or "/opt/elasticsearch/data-1,/opt/elasticsearch/data-2"
* ```es_log_dir``` - defaults to "/var/log/elasticsearch".
* ```es_work_dir``` - defaults to "/tmp/elasticsearch".
* ```es_plugin_dir``` - defaults to "/usr/share/elasticsearch/plugins".
* ```es_restart_on_change``` - defaults to true. If false, changes will not result in Elasticsearch being restarted.
* ```es_plugins_reinstall``` - defaults to false. If true, all currently installed plugins will be removed from a node. Listed plugins will then be re-installed.

This role ships with sample scripts and templates located in the [files/scripts/](files/scripts) and [files/templates/](files/templates) directories, respectively. These variables are used with the Ansible [with_fileglob](http://docs.ansible.com/ansible/playbooks_loops.html#id4) loop. When setting the globs, be sure to use an absolute path.
* ```es_scripts_fileglob``` - defaults to `<role>/files/scripts/`.
Expand All @@ -203,3 +247,8 @@ all supported platforms.
Elasticsearch restarted where required.
* Systemd is used for Ubuntu versions >= 15, Debian >=8, Centos >=7. All other versions use init for service scripts.

## IMPORTANT NOTES RE PLUGIN MANAGEMENT

* If the ES version is changed, all plugins will be removed. Those listed in the playbook will be re-installed. This is behaviour is required in ES 2.x.
* If no plugins are listed in the playbook for a node, all currently installed plugins will be removed.
* The role does not currently support automatic detection of differences between installed and listed plugins (other than if none are listed). Should users wish to change installed plugins should set es_plugins_reinstall to true. This will cause all currently installed plugins to be removed and those listed to be installed. Change detection will be implemented in future releases.
2 changes: 1 addition & 1 deletion defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ es_group: elasticsearch
es_config: {}
#Need to provide default directories
es_pid_dir: "/var/run/elasticsearch"
es_data_dir: "/var/lib/elasticsearch"
es_data_dirs: "/var/lib/elasticsearch"
es_log_dir: "/var/log/elasticsearch"
es_work_dir: "/tmp/elasticsearch"
es_plugin_dir: "/usr/share/elasticsearch/plugins"
26 changes: 26 additions & 0 deletions filter_plugins/custom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
__author__ = 'dale mcdiarmid'

import re

def modify_list(values=[], pattern='', replacement='', ignorecase=False):
''' Perform a `re.sub` on every item in the list'''
if ignorecase:
flags = re.I
else:
flags = 0
_re = re.compile(pattern, flags=flags)
return [_re.sub(replacement, value) for value in values]

def append_to_list(values=[], suffix=''):
if isinstance(values, basestring):
values = values.split(',')
return [str(value+suffix) for value in values]

def array_to_str(values=[],separator=','):
return separator.join(values)

class FilterModule(object):
def filters(self):
return {'modify_list': modify_list,
'append_to_list':append_to_list,
'array_to_str':array_to_str}
47 changes: 27 additions & 20 deletions tasks/elasticsearch-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,31 @@

#For directories we also use the {{inventory_hostname}}-{{ es_instance_name }} - this helps if we have a shared SAN.

- set_fact: pid_dir={{ es_pid_dir }}/{{inventory_hostname}}-{{ es_instance_name }}
- set_fact: instance_suffix={{inventory_hostname}}-{{ es_instance_name }}

- set_fact: data_dir={{ es_data_dir }}/{{inventory_hostname}}-{{ es_instance_name }}
- set_fact: pid_dir={{ es_pid_dir }}/{{instance_suffix}}

- set_fact: log_dir={{ es_log_dir }}/{{inventory_hostname}}-{{ es_instance_name }}
- set_fact: log_dir={{ es_log_dir }}/{{instance_suffix}}

- set_fact: work_dir={{ es_work_dir }}/{{inventory_hostname}}-{{ es_instance_name }}
- set_fact: work_dir={{ es_work_dir }}/{{instance_suffix}}

#Create required directories
- name: Create PID Directory
file: path={{ pid_dir }} state=directory owner={{ es_user }} group={{ es_group }}
- name: Create Directories
file: path={{ item }} state=directory owner={{ es_user }} group={{ es_group }}
with_items:
- "{{pid_dir}}"
- "{{work_dir}}"
- "{{log_dir}}"
- "{{conf_dir}}"
- "{{plugin_dir}}"

- name: Create data dir
file: state=directory path={{ data_dir }} owner={{ es_user }} group={{ es_group }}
- set_fact: data_dirs={{ es_data_dirs | append_to_list('/'+instance_suffix) }}

- name: Create work dir
file: state=directory path={{ work_dir }} owner={{ es_user }} group={{ es_group }}
- name: Create Data Directories
file: path={{ item }} state=directory owner={{ es_user }} group={{ es_group }}
with_items:
- "{{data_dirs}}"

- name: Create log dir
file: state=directory path={{ log_dir }} owner={{ es_user }} group={{ es_group }}

- name: Create Config Directory
file: path={{ conf_dir }} state=directory owner={{ es_user }} group={{ es_group }}

- name: Create Plugin Directory
file: state=directory path={{ plugin_dir }} owner={{ es_user }} group={{ es_group }}

#Copy the config template
- name: Copy Configuration File
Expand All @@ -57,13 +56,13 @@
#Copy the instance specific init file
- name: Copy Debian Init File for Instance
template: src=init/debian/elasticsearch.j2 dest={{instance_init_script}} mode=0755 force=yes
when: ansible_os_family == 'Debian'
when: ansible_os_family == 'Debian' and not use_system_d
notify: restart elasticsearch

#Copy the instance specific init file
- name: Copy Redhat Init File for Instance
template: src=init/redhat/elasticsearch.j2 dest={{instance_init_script}} mode=0755 force=yes
when: ansible_os_family == 'RedHat'
when: ansible_os_family == 'RedHat' and not use_system_d
notify: restart elasticsearch

#Copy the systemd specific file if systemd is installed
Expand Down Expand Up @@ -92,3 +91,11 @@

- name: Delete Default Sysconfig File
file: dest=/usr/lib/systemd/system/elasticsearch.service state=absent

- name: Delete Default Configuration File
file: dest=/etc/elasticsearch/elasticsearch.yml state=absent

- name: Delete Default Logging File
file: dest=/etc/elasticsearch/logging.yml state=absent

- debug: msg="Data Dirs {{data_dirs}}"
26 changes: 18 additions & 8 deletions tasks/elasticsearch-plugins.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
---

#es_plugins_reinstall will be set to true if elasticsearch_install.changed i.e. we have changed ES version
#es_plugins_reinstall will be set to true if elasticsearch_install.changed i.e. we have changed ES version, or if no plugins listed. Otherwise it is false and requires explicitly setting.
- set_fact: es_plugins_reinstall=true
when: elasticsearch_install.changed
when: elasticsearch_install.changed or es_plugins is not defined or es_plugins is none

#List currently installed plugins
- set_fact: list_command="list"

- set_fact: list_command="--list"
when: es_version | version_compare('2.0', '<')

#List currently installed plugins
- shell: "{{es_home}}/bin/plugin {{list_command}} | sed -n '1!p' | cut -d '-' -f2-"
register: installed_plugins
changed_when: False
environment:
CONF_DIR: "{{ conf_dir }}"
ES_INCLUDE: "{{ instance_default_file }}"

#This needs to removed installed plugins not those listed
#This needs to removes any currently installed plugins
- name: Remove elasticsearch plugins
command: "{{es_home}}/bin/plugin remove {{ item.plugin }} --silent"
command: "{{es_home}}/bin/plugin remove {{item}} --silent"
ignore_errors: yes
with_items: es_plugins
when: es_plugins_reinstall
with_items: installed_plugins.stdout_lines
when: es_plugins_reinstall and installed_plugins.stdout_lines | length > 0 and not 'No plugin detected' in installed_plugins.stdout_lines[0]
notify: restart elasticsearch
environment:
CONF_DIR: "{{ conf_dir }}"
ES_INCLUDE: "{{ instance_default_file }}"

- name: Install elasticsearch plugins
#debug: var=item
Expand All @@ -25,7 +35,7 @@
failed_when: "'Failed to install' in plugin_installed.stderr"
changed_when: plugin_installed.rc == 0
with_items: es_plugins
when: ansible_os_family == 'RedHat' or ansible_os_family == 'Debian'
when: es_plugins is defined and not es_plugins is none
notify: restart elasticsearch
environment:
CONF_DIR: "{{ conf_dir }}"
Expand Down
7 changes: 5 additions & 2 deletions tasks/elasticsearch-scripts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@

- name: Create script dir
file: state=directory path={{ es_script_dir }} owner={{ es_user }} group={{ es_group }}
when: es_config['path.scripts'] is defined

- name: Copy default scripts to elasticsearch
copy: src=scripts dest={{ es_script_dir }} owner={{ es_user }} group={{ es_group }}
when: es_scripts_fileglob is not defined

- name: Copy scripts to elasticsearch
copy: src={{ item }} dest={{ es_script_dir }} owner={{ es_user }} group={{ es_group }}
with_fileglob: es_scripts_fileglob | default("scripts")
with_fileglob: es_scripts_fileglob
8 changes: 7 additions & 1 deletion tasks/elasticsearch-templates.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
---

- file: path=/etc/elasticsearch/templates state=directory owner={{ es_user }} group={{ es_group }}

- name: Copy default templates to elasticsearch
copy: src=templates dest=/etc/elasticsearch/ owner={{ es_user }} group={{ es_group }}
when: es_templates_fileglob is not defined

- name: Copy templates to elasticsearch
copy: src={{ item }} dest=/etc/elasticsearch/ owner={{ es_user }} group={{ es_group }}
with_fileglob: es_templates_fileglob | default("templates")
with_fileglob: es_templates_fileglob

- set_fact: http_port=9200

Expand Down
2 changes: 1 addition & 1 deletion tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
- include: elasticsearch-scripts.yml
when: es_scripts
- include: elasticsearch-plugins.yml
when: es_plugins is defined
when: es_plugins is defined or es_plugins_reinstall
- include: elasticsearch-service.yml
- include: elasticsearch-templates.yml
when: es_templates
Expand Down
2 changes: 1 addition & 1 deletion templates/elasticsearch.j2
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ES_HOME={{es_home}}
CONF_DIR={{conf_dir}}

# Elasticsearch data directory
DATA_DIR={{data_dir}}
DATA_DIR={{ data_dirs | array_to_str }}

# Elasticsearch logs directory
LOG_DIR={{log_dir}}
Expand Down
2 changes: 1 addition & 1 deletion templates/elasticsearch.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ node.name: {{inventory_hostname}}-{{es_instance_name}}
# Path to directory containing configuration (this file and logging.yml):
path.conf: {{ conf_dir }}

path.data: {{ data_dir }}
path.data: {{ data_dirs | array_to_str }}

path.work: {{ work_dir }}

Expand Down
2 changes: 1 addition & 1 deletion templates/init/debian/elasticsearch.j2
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# Modified by Nicolas Huray for Elasticsearch <nicolas.huray@gmail.com>.
#
### BEGIN INIT INFO
# Provides: elasticsearch
# Provides: {{es_instance_name}}_{{default_file | basename}}
# Required-Start: $network $remote_fs $named
# Required-Stop: $network $remote_fs $named
# Default-Start: 2 3 4 5
Expand Down
Loading

0 comments on commit b7d8300

Please sign in to comment.