From 960d4032999f233740a24e98dc4b6d97a2ab0a62 Mon Sep 17 00:00:00 2001 From: Andrea Dell'Amico Date: Mon, 9 May 2022 19:07:14 +0200 Subject: [PATCH] Install and configure opensearch. --- README.md | 26 ++----- defaults/main.yml | 56 +++++++++++++- handlers/main.yml | 5 +- meta/main.yml | 54 +++++-------- tasks/main.yml | 100 +++++++++++++++++++++++- templates/jvm.options.j2 | 131 ++++++++++++++++++++++++++++++++ templates/opensearch.service.j2 | 30 ++++++++ templates/opensearch.yml.j2 | 117 ++++++++++++++++++++++++++++ 8 files changed, 464 insertions(+), 55 deletions(-) create mode 100644 templates/jvm.options.j2 create mode 100644 templates/opensearch.service.j2 create mode 100644 templates/opensearch.yml.j2 diff --git a/README.md b/README.md index 3637db8..ed7efa8 100644 --- a/README.md +++ b/README.md @@ -1,31 +1,21 @@ Role Name ========= -A brief description of the role goes here. - -Requirements ------------- - -Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required. +A role that installs OpenSearch, . +OpenSearch is a fork of ElasticSearch Role Variables -------------- -A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well. +The most important variables are listed below: + +``` yaml +``` Dependencies ------------ -A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles. - -Example Playbook ----------------- - -Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: - - - hosts: servers - roles: - - { role: username.rolename, x: 42 } +None License ------- @@ -35,4 +25,4 @@ EUPL-1.2 Author Information ------------------ -An optional section for the role authors to include contact information, or a website (HTML is not allowed). +Andrea Dell'Amico, diff --git a/defaults/main.yml b/defaults/main.yml index 95d3c70..17867e1 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,2 +1,56 @@ --- -# defaults file for ansible-role-template \ No newline at end of file +opensearch_install: true +opensearch_enabled: true +opensearch_major_version: 1 +opensearch_minor_versionn: 3 +opensearch_patch_version: 2 +opensearch_version: '{{ opensearch_major_version }}.{{ opensearch_minor_version }}. {{ opensearch_patch_version }}' +opensearch_filename: 'opensearch-{{ opensearch_version }}-linux-x64.tar.gz' +opensearch_download_url: 'https://artifacts.opensearch.org/releases/bundle/opensearch/{{ opensearch_version }}/{{ opensearch_filename }}' +opensearch_cli_tools_file: 'opensearch-cli-1.1.0-linux-x64.zip' +opensearch_cli_tools_url: 'https://artifacts.opensearch.org/opensearch-clients/opensearch-cli/{{ opensearch_cli_tools_file }}' +opensearch_user: opensearch +opensearch_base_install_dir: /opt/opensearch +opensearch_config_dir: '{{ opensearch_base_install_dir }}/config' +opensearch_bin_dir: '{{ opensearch_base_install_dir }}/bin' + +opensearch_cluster_name: 'Opensearch Cluster' +opensearch_http_port: 9200 +opensearch_transport_min_port: 9300 +opensearch_transport_max_port: 9400 +opensearch_data_dirs: + - /var/lib/opensearch +opensearch_log_dir: /var/log/opensearch +opensearch_directories: + - '{{ opensearch_data_dir }}' + - '{{ opensearch_log_dir }}' +opensearch_bind_ip: 0.0.0.0 +opensearch_single_node: true +opensearch_discovery_host_list: '["127.0.0.1", "[::1]"]' +opensearch_define_majority_of_nodes: true +opensearch_majority_of_nodes: 1 +opensearch_bootstrap_known_masters: + - '{{ ansible_fqdn }}' +opensearch_real_cluster: false +opensearch_recover_after_nodes: 3 +opensearch_max_local_storage_nodes: 1 +opensearch_destructive_requires_name: true +opensearch_define_heap_size: false +opensearch_heap_size: 2g +opensearch_additional_java_opts: '-server -Djava.awt.headless=true -Dfile.encoding=UTF-8' +opensearch_java_io_tmpdir: '/var/tmp' +opensearch_additional_conf: + - {name: 'search.max_buckets', value: '65535'} + +opensearch_max_open_files: 65535 +opensearch_max_processes: 8192 +opensearch_cluster_routing_allocation_disk_threshold_enabled: 'true' +opensearch_cluster_routing_allocation_disk_watermark_low: '85%' +opensearch_cluster_routing_allocation_disk_watermark_high: '90%' +# Compatibility with kernels <= 3.5. Set to False if you are using a newer kernel +opensearch_disable_bootstrap_syscall_filter: true + +# Only name and value are mandatory. The others have defaults +# systemctl_custom_options: +# - { name: 'fs.file-max', value: '{{ opensearch_max_open_files }}', sysctlfile: '{{ sysctl_custom_file }}', sysctl_reload: '{{ sysctl_opts_reload }}', sysctlfile_state: '{{ sysctl_custom_file_state }}' } +# - { name: 'vm.max_map_count', value: '262144', sysctlfile: '{{ sysctl_custom_file }}', sysctl_reload: '{{ sysctl_opts_reload }}', sysctlfile_state: '{{ sysctl_custom_file_state }}' } diff --git a/handlers/main.yml b/handlers/main.yml index 27474e0..7ba2079 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -1,2 +1,5 @@ --- -# handlers file for ansible-role-template \ No newline at end of file +- name: Restart opensearch + service: + name: opensearch + state: restarted diff --git a/meta/main.yml b/meta/main.yml index 81bda14..624e245 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -1,46 +1,32 @@ +--- galaxy_info: - author: your name - description: your description - company: your company (optional) + author: Andrea Dell'Amico + description: Systems Architect + company: ISTI-CNR - # If the issue tracker for your role is not on github, uncomment the - # next line and provide a value - issue_tracker_url: https://support.d4science.org/projects/automatic-provisioning/issues + issue_tracker_url: https://redmine-s2i2s.isti.cnr.it/projects/provisioning - license: EUPL-1.2 + license: EUPL 1.2+ min_ansible_version: 2.8 - # If this a Container Enabled role, provide the minimum Ansible Container version. - # min_ansible_container_version: - - # Optionally specify the branch Galaxy will use when accessing the GitHub - # repo for this role. During role install, if no tags are available, - # Galaxy will use this branch. During import Galaxy will access files on - # this branch. If Travis integration is configured, only notifications for this - # branch will be accepted. Otherwise, in all cases, the repo's default branch - # (usually master) will be used. - #github_branch: - - # - # Provide a list of supported platforms, and for each platform a list of versions. - # If you don't wish to enumerate all versions for a particular platform, use 'all'. # To view available platforms and versions (or releases), visit: # https://galaxy.ansible.com/api/v1/platforms/ # platforms: - - name: Ubuntu - versions: - - bionic + - name: Ubuntu + versions: + - bionic + - name: EL + versions: + - 7 + - 8 - galaxy_tags: [] - # List tags for your role here, one per line. A tag is a keyword that describes - # and categorizes the role. Users find roles by searching for tags. Be sure to - # remove the '[]' above, if you add tags to this list. - # - # NOTE: A tag is limited to a single word comprised of alphanumeric characters. - # Maximum 20 tags per role. + galaxy_tags: + - opensearch -dependencies: [] - # List your role dependencies here, one per line. Be sure to remove the '[]' above, - # if you add dependencies to this list. \ No newline at end of file +dependencies: + - src: git+https://gitea-s2i2s.isti.cnr.it/ISTI-ansible-roles/ansible-role-openjdk.git + version: master + name: openjdk + state: latest diff --git a/tasks/main.yml b/tasks/main.yml index 53c6cae..9db25dc 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,2 +1,100 @@ --- -# tasks file for ansible-role-template \ No newline at end of file +- name: Opensearch installation + block: + - name: Create the opensearch user, {{ opensearch_user }} + user: + name: '{{ opensearch_user }}' + home: '{{ opensearch_base_install_dir }}' + createhome: false + shell: /sbin/nologin + system: true + + - name: Create the opensearch file system + file: + dest: '{{ item }}' + state: directory + owner: '{{ opensearch_user }}' + group: '{{ opensearch_user }}' + mode: 0750 + loop: '{{ opensearch_directories }}' + + - name: Get the opensearch distribution + get_url: + url: '{{ opensearch_download_url }}' + dest: /srv + register: opensearch_download + + - name: Get the opensearch cli tools + get_url: + url: '{{ opensearch_cli_tools_url }}' + dest: /srv + + - name: Unpack the opensearch distribution + unarchive: + src: '/srv/{{ opensearch_filename }}' + remote_src: true + dest: /opt + owner: root + group: root + when: opensearch_download is changed + + - name: Create a link to the correct place + file: + src: '/srv/{{ opensearch_filename }}' + dest: '{{ opensearch_base_install_dir }}' + state: link + + tags: ['opensearch'] + +- name: Opensearch configuration + block: + - name: Install the opensearch JVM options + template: + src: jvm.options.j2 + dest: '{{ opensearch_config_dir }}/jvm.options' + owner: root + group: '{{ opensearch_user }}' + mode: 0640 + notify: Restart opensearch + + - name: Install the opensearch configuration + template: + src: opensearch.yml.j2 + dest: '{{ opensearch_config_dir }}/opensearch.yml' + owner: root + group: '{{ opensearch_user }}' + mode: 0640 + notify: Restart opensearch + + tags: ['opensearch', 'opensearch_conf'] + +- name: Opensearch service status + block: + - name: Install the opensearch systemd unit + template: + src: opensearch.service.j2 + dest: /etc/systemd/system/opensearch.service + owner: root + group: root + mode: 0644 + register: opensearch_unit_update + notify: Restart opensearch + + - name: Reload systemd + systemd: + daemon_reload: true + when: opensearch_unit_update is changed + + - name: Ensure that opensearch is running and enabled + service: opensearch + status: started + enabled: true + when: opensearch_enabled + + - name: Ensure that opensearch is stopped and disabled + service: opensearch + status: stopped + enabled: false + when: not opensearch_enabled + + tags: ['opensearch', 'opensearch_service'] diff --git a/templates/jvm.options.j2 b/templates/jvm.options.j2 new file mode 100644 index 0000000..bf6ef53 --- /dev/null +++ b/templates/jvm.options.j2 @@ -0,0 +1,131 @@ +## JVM configuration + +################################################################ +## IMPORTANT: JVM heap size +################################################################ +## +## You should always set the min and max JVM heap +## size to the same value. For example, to set +## the heap to 4 GB, set: +## +## -Xms4g +## -Xmx4g +## +## See https://www.elastic.co/guide/en/elasticsearch/reference/current/heap-size.html +## for more information +## +################################################################ + +# Xms represents the initial size of total heap space +# Xmx represents the maximum size of total heap space + +-Xms{{ elasticsearch_heap_size }} +-Xmx{{ elasticsearch_heap_size }} + +################################################################ +## Expert settings +################################################################ +## +## All settings below this section are considered +## expert settings. Don't tamper with them unless +## you understand what you are doing +## +################################################################ + +## GC configuration +{% if openjdk_default < 8 %} +-XX:+UseConcMarkSweepGC +-XX:CMSInitiatingOccupancyFraction=75 +-XX:+UseCMSInitiatingOccupancyOnly +{% endif %} + +## optimizations + +# disable calls to System#gc +-XX:+DisableExplicitGC + +# pre-touch memory pages used by the JVM during initialization +-XX:+AlwaysPreTouch + +## basic + +# force the server VM +-server + +# set to headless, just in case +-Djava.awt.headless=true + +# ensure UTF-8 encoding by default (e.g. filenames) +-Dfile.encoding=UTF-8 + +# use our provided JNA always versus the system one +-Djna.nosys=true + +# turn off a JDK optimization that throws away stack traces for common +# exceptions because stack traces are important for debugging +-XX:-OmitStackTraceInFastThrow + +# flag to explicitly tell Netty to not use unsafe +-Dio.netty.noUnsafe=true +-Dio.netty.noKeySetOptimization=true +-Dio.netty.recycler.maxCapacityPerThread=0 + +-Dlog4j.shutdownHookEnabled=false +-Dlog4j2.disable.jmx=true + +{% if elasticsearch_major_version >= 6 %} +-Djava.io.tmpdir=${ES_TMPDIR} +{% endif %} + +## heap dumps + +# generate a heap dump when an allocation from the Java heap fails +# heap dumps are created in the working directory of the JVM +-XX:+HeapDumpOnOutOfMemoryError + +# specify an alternative path for heap dumps +# ensure the directory exists and has sufficient space +#-XX:HeapDumpPath=${heap.dump.path} + +## GC logging + +#-XX:+PrintGCDetails +#-XX:+PrintGCTimeStamps + +#-XX:+PrintGCDateStamps +#-XX:+PrintClassHistogram +#-XX:+PrintTenuringDistribution +#-XX:+PrintGCApplicationStoppedTime + +# log GC status to a file with time stamps +# ensure the directory exists +#-Xloggc:${loggc} + +# Elasticsearch 5.0.0 will throw an exception on unquoted field names in JSON. +# If documents were already indexed with unquoted fields in a previous version +# of Elasticsearch, some operations may throw errors. +# +# WARNING: This option will be removed in Elasticsearch 6.0.0 and is provided +# only for migration purposes. +#-Delasticsearch.json.allow_unquoted_field_names=true +# specify an alternative path for heap dumps +# ensure the directory exists and has sufficient space +-XX:HeapDumpPath={{ elasticsearch_data_dir }} + +## JDK 8 GC logging + +8:-XX:+PrintGCDetails +8:-XX:+PrintGCDateStamps +8:-XX:+PrintTenuringDistribution +8:-XX:+PrintGCApplicationStoppedTime +8:-Xloggc:/var/log/elasticsearch/gc.log +8:-XX:+UseGCLogFileRotation +8:-XX:NumberOfGCLogFiles=32 +8:-XX:GCLogFileSize=64m + +# JDK 9+ GC logging +9-:-Xlog:gc*,gc+age=trace,safepoint:file=/var/log/elasticsearch/gc.log:utctime,pid,tags:filecount=32,filesize=64m +# due to internationalization enhancements in JDK 9 Elasticsearch need to set the provider to COMPAT otherwise +# time/date parsing will break in an incompatible way for some date patterns and locals +9-:-Djava.locale.providers=COMPAT + diff --git a/templates/opensearch.service.j2 b/templates/opensearch.service.j2 new file mode 100644 index 0000000..3ec3dad --- /dev/null +++ b/templates/opensearch.service.j2 @@ -0,0 +1,30 @@ +# +# Systemd unit that manages the OpenSearch service status +# +[Unit] +Description=OpenSearch https://www.opensearch.org +Wants=network-online.target +After=network-online.target + +[Service] +RuntimeDirectory=opensearch +PrivateTmp=true +WorkingDirectory={{ opensearch_base_install_dir }} +User={{ opensearch_user }} +Group={{ opensearch_user }} +ExecStart={{ opensearch_bin_dir }}/opensearch -p {{ opensearch_log_dir }}/opensearch.pid -q +StandardOutput=journal +StandardError=inherit +LimitNOFILE={{ opensearch_max_open_files }} +LimitMEMLOCK=infinity +LimitNPROC={{ opensearch_max_processes }} +LimitAS=infinity +LimitFSIZE=infinity +TimeoutStopSec=0 +KillSignal=SIGTERM +KillMode=process +SendSIGKILL=no +SuccessExitStatus=143 + +[Install] +WantedBy=multi-user.target diff --git a/templates/opensearch.yml.j2 b/templates/opensearch.yml.j2 new file mode 100644 index 0000000..a55f0aa --- /dev/null +++ b/templates/opensearch.yml.j2 @@ -0,0 +1,117 @@ +# ======================== OpenSearch Configuration ========================= +# +# NOTE: OpenSearch comes with reasonable defaults for most settings. +# Before you set out to tweak and tune the configuration, make sure you +# understand what are you trying to accomplish and the consequences. +# +# The primary way of configuring a node is via this file. This template lists +# the most important settings you may want to configure for a production cluster. +# +# Please consult the documentation for further information on configuration options: +# https://www.opensearch.org +# ---------------------------------- Cluster ----------------------------------- +# +# Use a descriptive name for your cluster: +# +cluster.name: {{ opensearch_cluster_name }} +# +# ------------------------------------ Node ------------------------------------ +# +# Use a descriptive name for the node: +# +node.name: {{ ansible_fqdn }} + +cluster.initial_master_nodes: +{% for n in opensearch_bootstrap_known_masters %} + - {{ n }} +{% endfor %} +# +# Add custom attributes to the node: +# +# node.rack: r1 +# +# ----------------------------------- Paths ------------------------------------ +# +# Path to directory where to store the data (separate multiple locations by comma): +# +path.data: {% for data_dir in opensearch_data_dirs %}{{ data_dir }}{% if not loop.last %},{% endif %}{% endfor %} +# +# Path to log files: +# +path.logs: {{ opensearch_log_dir }} +# +# ----------------------------------- Memory ----------------------------------- +# +# Lock the memory on startup: +# +#bootstrap.memory_lock: true +# +# Make sure that the heap size is set to about half the memory available +# on the system and that the owner of the process is allowed to use this +# limit. +# +# OpenSearch performs poorly when the system is swapping the memory. +# +# ---------------------------------- Network ----------------------------------- +# +# Set the bind address to a specific IP (IPv4 or IPv6): +# +network.host: {{ opensearch_bind_ip }} +# +# Set a custom port for HTTP: +# +http.port: {{ opensearch_http_port }} + +# by default transport.host refers to network.host +transport.host: {{ opensearch_bind_ip }} +transport.tcp.port: {{ opensearch_transport_min_port }}-{{ opensearch_transport_max_port }} +# +# --------------------------------- Discovery ---------------------------------- +# +{% if opensearch_single_node %} +discovery.type: single-node +{% else %} +# Pass an initial list of hosts to perform discovery when new node is started: +# The default list of hosts is ["127.0.0.1", "[::1]"] +# +discovery.zen.ping.unicast.hosts: {{ opensearch_discovery_host_list }} +# +{% if opensearch_define_majority_of_nodes %} +# Prevent the "split brain" by configuring the majority of nodes (total number of nodes / 2 + 1): +# +discovery.zen.minimum_master_nodes: {{ opensearch_majority_of_nodes }} +# +# +{% if opensearch_real_cluster %} +# ---------------------------------- Gateway ----------------------------------- +# +# Block initial recovery after a full cluster restart until N nodes are started: +# +gateway.recover_after_nodes: {{ opensearch_recover_after_nodes }} +# +{% endif %} +{% endif %} +{% endif %} +# ---------------------------------- Various ----------------------------------- +# +# Disable starting multiple nodes on a single system: +# +node.max_local_storage_nodes: {{ opensearch_max_local_storage_nodes }} +# +# Require explicit names when deleting indices: +# +action.destructive_requires_name: {{ opensearch_destructive_requires_name | lower }} + +# +cluster.routing.allocation.disk.threshold_enabled: {{ opensearch_cluster_routing_allocation_disk_threshold_enabled }} +cluster.routing.allocation.disk.watermark.low: {{ opensearch_cluster_routing_allocation_disk_watermark_low }} +cluster.routing.allocation.disk.watermark.high: {{ opensearch_cluster_routing_allocation_disk_watermark_high }} + +{% for conf in opensearch_additional_conf %} +{{ conf.name }}: {{ conf.value }} +{% endfor %} + +{% if opensearch_disable_bootstrap_syscall_filter %} +# When using an old kernel +bootstrap.system_call_filter: false +{% endif %}