avatarMuhammad Badawy

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1537

Abstract

,password and become password)</figcaption></figure><p id="9baf"><b>ansible_ssh_user</b> — -> The user that will be used to connect to the target machine <b>ansible_ssh_pass </b>— -> The password that will be used to connect to the target machine <b>ansible_become_pass</b> — -> The password that will be used to escalate the privilidges while running certain tasks</p><p id="77cf">ansible_ssh_pass & ansible_become_pass can be replaced by passing “-- ask-pass --ask-become-pass” from the terminal as a better practice than hardcoding them in a file</p><p id="06e5">Then let’s review <b>src Directory ‘./playbooks/src’</b> : Should contain the src file of elasticsearch that will be used This script has been tested against elasticsearch version 7.6.2 Also if the communication between elsticsearch nodes is secured through a certificate, it should be placed inside cert directory ‘<b>./playbooks/src/cert’</b></p><p id="914e"><b>Inside elastic role, we have handlers, tasks and templates</b></p><p id="47b5">Let’s start with the main task, the below <b>modules </b>will be used:</p><p id="68d6"><b>debug:</b> to log the IP of the target machine that script is running on <b>copy:</b> to copy elasticsearch src file and certificate file <b>yum:</b> to install elsticsearch package <b>file:</b> to create the needed paths with the needed permissions <b>blockinfile:</b> to edit configuration files <b>service:</b> to restart elasticsearch and firewalld services <b>replace:</b> to disable SWAP in fstab <b>template:</b> to de

Options

fine jvm.options and elastisearch.yml</p><p id="0111">Then a <b>handler </b>is used to enable and restart elasticsearch service</p><figure id="67cd"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*l2jxVTQq0mRRbcaENbMv6Q.png"><figcaption></figcaption></figure><p id="413c"><b>To use the playbook:</b>

Edit elastic.hosts file to add the target machines, usernames and passwords then if all of your servers are configured with name-based resolution: edit main.yaml file under elastic role/elastic/tasks:</p><ul><li>Go to “Add ELK stack machines” task.</li><li>Add elasticsearch stack machines here.</li></ul><figure id="ddd0"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*9N1NppPZUBYD_1-_5CwMig.png"><figcaption>Add IPs and Domain names</figcaption></figure><p id="29bf">Then run the below command:</p><div id="ebdb"><pre><span class="hljs-variable">$ansible</span>-playbook -i .<span class="hljs-regexp">/hosts/</span>elastic.hosts .<span class="hljs-regexp">/playbooks/</span>elastic-setup.yml</pre></div><p id="aeb3"><b>Conclusion:</b></p><p id="e287">This script has been tested against redhat OS 7.3 and elasticsearch version 7.6.2 so you may run into simple issues related to the environment.</p><p id="55bb">I hope this could help in adding new nodes to elasticsearch clusters, and you enjoyed reading this. Feel free to add your comments, thoughts or feedback and don’t forget to get in touch on <a href="https://www.linkedin.com/in/muhammad-badawy-linked1n/">linkedin</a>.</p></article></body>

Add new nodes to your ElasticSearch cluster through Ansible playbook

In this article, I will demonstrate how to add a new node to elasticsearch cluster using Ansible playbook.

Prerequisites for the tutorial: - OS version should be Redhat or CentOS 7+ - The target machine with sudo user should be ready - Basic knowledge of ansible

For more info about elasticsearch, Thanks to visit https://www.elastic.co/what-is/elasticsearch

Ansible files for this tutorial can be found in my github repo here

Main structure:

Roles structure will be used with tasks, handlers, templates and vars if needed

├───hosts
└───playbooks
 ├───roles
 │ └───elastic
 │ ├───handlers
 │ ├───tasks
 │ ├───templates
 │ └───vars
 └───src
 └───cert

Let’s start with hosts file which will contain the target host/s (that needs to be added to the cluster) as IP or DNS, Also the vars section to define the needed variables:

Modify ssh_host and vars (user,password and become password)

ansible_ssh_user — -> The user that will be used to connect to the target machine ansible_ssh_pass — -> The password that will be used to connect to the target machine ansible_become_pass — -> The password that will be used to escalate the privilidges while running certain tasks

ansible_ssh_pass & ansible_become_pass can be replaced by passing “-- ask-pass --ask-become-pass” from the terminal as a better practice than hardcoding them in a file

Then let’s review src Directory ‘./playbooks/src’ : Should contain the src file of elasticsearch that will be used This script has been tested against elasticsearch version 7.6.2 Also if the communication between elsticsearch nodes is secured through a certificate, it should be placed inside cert directory ‘./playbooks/src/cert’

Inside elastic role, we have handlers, tasks and templates

Let’s start with the main task, the below modules will be used:

debug: to log the IP of the target machine that script is running on copy: to copy elasticsearch src file and certificate file yum: to install elsticsearch package file: to create the needed paths with the needed permissions blockinfile: to edit configuration files service: to restart elasticsearch and firewalld services replace: to disable SWAP in fstab template: to define jvm.options and elastisearch.yml

Then a handler is used to enable and restart elasticsearch service

To use the playbook: Edit elastic.hosts file to add the target machines, usernames and passwords then if all of your servers are configured with name-based resolution: edit main.yaml file under elastic role/elastic/tasks:

  • Go to “Add ELK stack machines” task.
  • Add elasticsearch stack machines here.
Add IPs and Domain names

Then run the below command:

$ansible-playbook -i ./hosts/elastic.hosts ./playbooks/elastic-setup.yml

Conclusion:

This script has been tested against redhat OS 7.3 and elasticsearch version 7.6.2 so you may run into simple issues related to the environment.

I hope this could help in adding new nodes to elasticsearch clusters, and you enjoyed reading this. Feel free to add your comments, thoughts or feedback and don’t forget to get in touch on linkedin.

Elasticsearch
Ansible
Elk
Ansible Playbook
Ansible Tutorial
Recommended from ReadMedium