4.1 C

Install ElasticSearch via Ansible

The whole idea of this post is obvious. I have already covered the same objective in a standard fashion but this is about perform the installation via ansible automation. If you don’t have an understanding on Ansible Directory Structure – I really recommend to check my other post –  where I highlighted each key areas that needed to get familiar.

Inventory File

In the example, I am going to deploy Three nodes of ElasticSearch – one represent MASTER role, while other two as DATA role.

es-0001 ansible_host=

es-0002 ansible_host=
es-0003 ansible_host=



- hosts: elastic_master <------------ following actions will be performed against any host listed in elastic_master alias which found to be in inventory file
  remote_user: root <---------------- to execute the command as root
  become: true
    - name: "Installing basic packages"
      action: yum <---------------- calls the yum module and any key that goes with with_items will be installed
              name={{ item }}
        - unzip
      when: ansible_os_family == "RedHat" <---------------- a condition such that instruct the ansible pre_tasks should only suppose to be executed on a Fedora based distribution
   - { role: elastic_master_install }

- hosts: elastic_data
  remote_user: root
  become: true
    - name: "Installing basic packages"
      action: yum
              name={{ item }}
        - unzip
      when: ansible_os_family == "RedHat"
   - { role: elastic_data_install }


Default File

As we already covered this variables will be used when files which are in Jinga format are being copied under the Template DIrectory.

# vim roles/elastic_master_install/defaults
cluster_name: clusterName
node_master_true: "true"
node_data_true: "false"
node_ingest_true: "false"
path_to_log: /data/elk/logs
path_to_data: /data/elk/data
http_port: 9200
transport_tcp_port: 9300
discovery_zen_ping_unicast_hosts: '[""]'

### - jvm config
init_heap_size: "-Xms8g"
max_heap_size: "-Xmx8g"



Template file Example

This is how a basic elasticsearch.yaml looks like in Jinja fromat.

# vim roles/elastic_master_install/templates/elasticsearch.yml.j2
cluster.name: {{cluster_name}}
node.name: {{inventory_hostname}}
node.master: {{node_master_true}}
node.data: {{node_data_true}}
node.ingest: {{node_ingest_true}}
path.data: {{path_to_data}}
path.logs: {{path_to_log}}
network.host: {{ansible_host}}


Task File

This is where we can define all the task that are part of the respective role, in this case task that needed to execute setting up Elasticsearch

# vim roles/elastic_master_install/tasks/main.yml
- name: Creating elk user...
    name: elk
    comment: "elk User"
    createopt: yes
    opt: /opt/elk/
    uid: 1999
    shell: /bin/bash
  become: true

- name: Copying & untar ElasticSearch5.5..
    src: /root/Ansible/ElasticSearch5/roles/elastic_master_install/Files/elasticsearch-5.5.0.tar.gz
    dest: /opt/elk/
    owner: elk
    group: elk
    mode: 0755
  become: true

- name: Creating necessary directories..
    path: /data/elk/{{ item }}
    state: directory
    owner: elk
    group: elk
    mode: 0775
    recurse: yes
     - [data, logs, run]
  become: true

- name: Copying the main config file...
  template: src=elasticsearch.yml.j2 dest={{elasticsearch_config_dir}}/elasticsearch.yml owner=elk group=elk mode=0644 
  become: true


Please note that I have only added files for Role “elastic_master_install” => Task/Template/Default. However, as in the prod.yaml there is another role called “elastic_data_install” which you also need to work on as did in above last three steps.

When you have the Directory Structure ready, you can initiate the Ansible by;

# ansible-playbook -i inventory prod.yaml


“I hope this has been informative for you”

- Advertisement -


Please enter your comment!
Please enter your name here

Donate to Osradar

Latest article