0

I wrote an ansible role to install elasticsearch multi node cluster, the problème is that i have a configuration files for each type of node (Master, Data).I have a problem to specify the host in each playbook.

setup_elastic.yml

---
- hosts: all
  become: yes
  become_user: root
  roles:
  - elasticsearch

and here is the config file for masters:

---
    - name: config elasticsearch master
      blockinfile:
          path: /etc/elasticsearch/elasticsearch.yml
          block: |
                    cluster.name: lamedicale4
                    node.master: true
                    node.data: false
                    node.name: ${HOSTNAME}
                    bootstrap.mlockall: true
                    path.data: /data/elasticsearch
                    path.logs: /logs/elasticsearch
                    discovery.zen.minimum_master_nodes: 2
                    discovery.zen.ping.multicast.enabled: false
                    discovery.zen.ping.unicast.hosts : ["172.31.36.229","172.31.44.124"]

config file for data nodes:

---
    - name: config elasticsearch data nodes
      blockinfile:
          path: /etc/elasticsearch/elasticsearch.yml
          block: |
                    cluster.name: lamedicale4
                    node.master: false
                    node.data: true
                    node.name: ${HOSTNAME}
                    bootstrap.mlockall: true
                    path.data: /data/elasticsearch
                    path.logs: /logs/elasticsearch
                    discovery.zen.minimum_master_nodes: 2
                    discovery.zen.ping.multicast.enabled: false
                    discovery.zen.ping.unicast.hosts : ["172.31.36.229","172.31.44.124"]

and here is the main tasks:

---
# tasks file for /etc/ansible/roles/elastissearch

- import_tasks: install_java_jdk.yml
- import_tasks: install_elasticsearch.yml
- import_tasks: clear_file.yml
- import_tasks: directory_data_log_elastic.yml
- include_tasks: config_elasticsearch_master.yml
- include_tasks: config_elasticsearch_data.yml
- import_tasks: reload_restart_elasticsearch.yml

what should I do ?

Aymen
  • 11
  • 4

1 Answers1

0

Answering your specific question, replace ${HOSTNAME} with either:

  • {{ inventory_hostname }}
  • {{ ansible_fqdn }}
  • {{ ansible_hostname }}

Have a look at this part of the docs to understand where these come from.

More generally, I would suggest that a more flexible approach to 'blockinfile', would be to convert the whole config file to an Ansible template, and manage it with the 'template' module. I have done this in the past with Elasticsearch config files and it has worked well.

clockworknet
  • 2,736
  • 1
  • 15
  • 19