--- - name: Install and Configure Elasticsearch 9 hosts: elasticsearch # Target your hosts here (e.g., from your inventory file) become: true vars: # UPDATED: Set to a specific version 9 release for predictable deployments. es_version: "9.0.0" # Change to the specific ES9 version you want to deploy es_cluster_name: "cluster-v9" es_node_name: "{{ ansible_hostname }}" es_network_host: "{{ ansible_default_ipv4.address }}" # Binds to the primary private IP es_heap_size: "{{ (ansible_memtotal_mb * 0.5) | int }}m" # Use 50% of total RAM. For 32GB RAM, this sets ~16GB. # --- For multi-node clusters, override these in your inventory --- es_seed_hosts: ["{{ es_network_host }}"] es_initial_master_nodes: ["{{ es_node_name }}"] tasks: - name: "BLOCK: System Preparation" block: - name: Update APT package cache ansible.builtin.apt: update_cache: true cache_valid_time: 3600 - name: Install prerequisite packages ansible.builtin.apt: name: - apt-transport-https - ca-certificates - gnupg # UPDATED: Elasticsearch 9 requires Java 21+ - openjdk-21-jdk state: present - name: "BLOCK: System Tuning for Elasticsearch" block: - name: Set vm.max_map_count for Elasticsearch ansible.posix.sysctl: name: vm.max_map_count value: "262144" state: present reload: true - name: Set Elasticsearch user memory limit community.general.pam_limits: domain: "elasticsearch" limit_type: "-" limit_item: "memlock" value: "unlimited" comment: "Allow memory locking" - name: Set Elasticsearch user file descriptor limit community.general.pam_limits: domain: "elasticsearch" limit_type: "-" limit_item: "nofile" value: "65536" comment: "Set max open files" - name: "BLOCK: Install Elasticsearch" block: - name: Download Elasticsearch GPG key ansible.builtin.get_url: url: "https://artifacts.elastic.co/GPG-KEY-elasticsearch" dest: "/tmp/GPG-KEY-elasticsearch" mode: '0644' - name: De-armor the GPG key ansible.builtin.command: cmd: "gpg --dearmor -o /etc/apt/trusted.gpg.d/elasticsearch.gpg /tmp/GPG-KEY-elasticsearch" creates: "/etc/apt/trusted.gpg.d/elasticsearch.gpg" - name: Add Elasticsearch 9.x APT repository # UPDATED: Repository path changed from 8.x to 9.x ansible.builtin.apt_repository: repo: "deb https://artifacts.elastic.co/packages/9.x/apt stable main" state: present filename: elastic-9.x - name: Install Elasticsearch package ansible.builtin.apt: name: "elasticsearch={{ es_version }}" state: present update_cache: true - name: "BLOCK: Configure Elasticsearch" block: - name: Configure elasticsearch.yml ansible.builtin.template: src: templates/elasticsearch.yml.j2 dest: /etc/elasticsearch/elasticsearch.yml owner: root group: elasticsearch mode: '0660' notify: Restart Elasticsearch - name: Configure JVM heap size ansible.builtin.template: src: templates/jvm.options.j2 dest: /etc/elasticsearch/jvm.options.d/heap_size.options owner: root group: root mode: '0644' notify: Restart Elasticsearch - name: Flush handlers to apply config changes before starting the service ansible.builtin.meta: flush_handlers - name: Enable and ensure Elasticsearch service is started ansible.builtin.systemd: name: elasticsearch state: started enabled: true daemon_reload: true - name: Wait for Elasticsearch to start up on port 9200 ansible.builtin.wait_for: host: "{{ es_network_host }}" port: 9200 delay: 10 timeout: 120 delegate_to: localhost handlers: - name: Restart Elasticsearch ansible.builtin.systemd: name: elasticsearch state: restarted