Elasticsearch Installation using Ansible [6 Steps]

In this article, We are going to perform Elasticsearch Installation using Ansible on AWS Instance, configuring Elasticsearch, install nginx and configure SSL.

First create a directory:

$ sudo mkdir ansible_workspace

Navigate to ansible directory

$ cd ansible_workspace

Now create a YAML file for ansible playbook.

$ sudo nano deploy_elasticsearch.yaml

Step 1: Create Inventory

---
- name: Stage instance(s)
  hosts: localhost
  connection: local
  user: root
  gather_facts: false
  tags:
      - stage

Step 2: Create EC2 Instance using Ansible

Create an EC2 instance using Ansible, Insert your keypair name, instance type, security group, AMI image, and instance count

  • keypair: EC2KEYPAIRNAME
  • instance_type: t1.micro
  • security_group: EC2SECURITYGROUPNAME
  • image: AMINAME
  • instance_count: 2
  tasks:
    - name: Launch the new EC2 Instance using Ansible
      ec2:
        instance_type: t2.medium # 
        group: mahesh-NG         #security Group
        count: 1
        key_name: awskeypair  # key pair 
        image: ami-0fc20dd1da406jkhy #Image ID
        region: us-east-2                                          # EC2 region
        vpc_subnet_id: subnet-0b67481hg7v82fc2d5a    # subnet ID
        wait: yes
        assign_public_ip: yes 
      register: ec2

Step 3: Adding Instance to Deploy Group

Add the newly created host to deploy group to connect

  - name: Add the newly created host so that we can further contact it
      add_host:
         name: "{{ item.public_ip }}"
         groups: deploy
      with_items: "{{ ec2.instances }}"

Step 4: Connect and Exchange SSH key pair

connect ec2 instance and exchange ssh key pair

    - name: Wait for SSH to come up
      wait_for:
        host: "{{ item.public_ip }}"
        port: 22 
        state: started 
      with_items: "{{ ec2.instances }}"

  - name: accept new ssh fingerprints                                         
      shell: ssh-keyscan -H {{ item.public_ip }} >> ~/.ssh/known_hosts          
      with_items: '{{ ec2.instances }}'  

 - name: Breathing room (Ansible uses python apt, has issues running directly after boot)
      pause: seconds=15

Step 5: Elasticsearch Installation using Ansible

Install OpenJDK 11, set JAVA HOME, install nginx and Elasticsearch installation using ansible.

- name: Configuring ElasticSearch
  hosts: deploy
  user: ubuntu 
  become_method: sudo
  become: true
  gather_facts: false

 tags:
    - config
    - configure 

  tasks:

  - name: update cache and ignore errors in case of problems
      become: yes
      apt: update_cache=yes
      ignore_errors: yes

 - name: install nginx
      become: yes
      apt: 
        name: nginx
        purge: yes
        state: present

 - name: install java 11
      become: yes
      apt: 
        name: openjdk-11-jdk
        purge: yes
        state: present

- name: Set Java home
      shell: sudo echo "JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64" >> /etc/environment

   - name: export Java home
      shell: export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64 

- name: Download ElasticSearch package
      shell: wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -

- name: check package list
      shell: echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list

-name: update cache and ignore errors in case of problems
      become: yes
      apt: update_cache=yes
      ignore_errors: yes

- name: install elasticsearch
      become: yes
      apt: 
        name: elasticsearch
        purge: yes
        state: present
      notify: restart elasticsearch

We have covered, Elasticsearch Installation using Ansible on AWS Instance

Step 6: Configure Elasticsearch and Adding SSL

Make below configuration in Elasticsearch and add SSL certificates

- name: Copy over Elasticsearch settings 
copy: src=./elasticsearch/elasticsearch.yml dest=/etc/elasticsearch/elasticsearch.yml
notify: restart elasticsearch

- name: Copy over nginx defailt file 
copy: src=./elasticsearch/default dest=/etc/nginx/sites-available/default
notify: restart nginx

- name: Copy over chain file 
copy: src=./elasticsearch/fullchain.pem dest=/etc/nginx/sites-available/fullchain.pem
notify: restart nginx

- name: Copy over pvt keypair 
copy: src=./elasticsearch/privkey.pem dest=/etc/nginx/sites-available/privkey.pem
notify: restart nginx 

handlers:
- name: restart elasticsearch
action: service name=elasticsearch state=restarted

- name: restart nginx
action: service name=nginx state=restarted

Now Run the Ansible playbook using below command

$ ansible-playbook -i /etc/ansible/ec2.py -vvv deploy_elasticsearch.yml -vv --private-key=/home/mahesh/ansible_workspace/awskp.pem

Make below changes in Elasticsearch configuration file

 

# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 0.0.0.0
#
# Set a custom port for HTTP:
#
http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
   discovery.seed_hosts: []
   discovery.type: single-node
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
#cluster.initial_master_nodes: ["node-1", "node-2"]
#
# For more information, consult the discovery and cluster formation module documentation.

Overview of Ansible Playbook

Below is complete ansible playbook to install Elasticsearch

---
- name: Stage instance(s)
  hosts: localhost
  connection: local
  user: root
  gather_facts: false
  tags:
      - stage

 tasks:
    - name: Launch the new EC2 Instance
      ec2:
        instance_type: t2.medium
        group: mahesh-NG
        count: 1
        key_name: awskeypair
        image: ami-0fc20dd1da406jkhy
        region: us-east-2
        vpc_subnet_id: subnet-0b67481hg7v82fc2d5a
        wait: yes
        assign_public_ip: yes
      register: ec2

  - name: Add the newly created host so that we can further contact it
      add_host:
         name: "{{ item.public_ip }}"
         groups: deploy
      with_items: "{{ ec2.instances }}"


- name: Wait for SSH to come up
      wait_for:
        host: "{{ item.public_ip }}"
        port: 22 
        state: started 
      with_items: "{{ ec2.instances }}"

 - name: accept new ssh fingerprints                                         
      shell: ssh-keyscan -H {{ item.public_ip }} >> ~/.ssh/known_hosts          
      with_items: '{{ ec2.instances }}' 

- name: Breathing room (Ansible uses python apt, has issues running directly after boot)
      pause: seconds=15

- name: Configuring ElasticSearch
  hosts: deploy
  user: ubuntu 
  become_method: sudo
  become: true
  gather_facts: false

tags:
    - config
    - configure 

tasks:
       - name: update cache and ignore errors in case of problems
      become: yes
      apt: update_cache=yes
      ignore_errors: yes

- name: install nginx
      become: yes
      apt: 
        name: nginx
        purge: yes
        state: present

- name: install java 11
      become: yes
      apt: 
        name: openjdk-11-jdk
        purge: yes
        state: present

- name: Set Java home
      shell: sudo echo "JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64" >> /etc/environment

  - name: export Java home
      shell: export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64  

 - name: Download ElasticSearch package
      shell: wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -

- name: check package list
      shell: echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list

- name: update cache and ignore errors in case of problems
      become: yes
      apt: update_cache=yes
      ignore_errors: yes

- name: install elasticsearch
      become: yes
      apt: 
        name: elasticsearch
        purge: yes
        state: present
      notify: restart elasticsearch

- name: Copy over Elasticsearch settings      
      copy: src=./elasticsearch/elasticsearch.yml dest=/etc/elasticsearch/elasticsearch.yml
      notify: restart elasticsearch

- name: Copy over nginx defailt file    
      copy: src=./elasticsearch/default dest=/etc/nginx/sites-available/default
      notify: restart nginx

- name: Copy over chain file      
      copy: src=./elasticsearch/fullchain.pem dest=/etc/nginx/sites-available/fullchain.pem
      notify: restart nginx

  - name: Copy over pvt keypair      
      copy: src=./elasticsearch/privkey.pem dest=/etc/nginx/sites-available/privkey.pem
      notify: restart nginx

 handlers:
    - name: restart elasticsearch
      action: service name=elasticsearch state=restarted

 - name: restart nginx
      action: service name=nginx state=restarted

Conclusion:

We have covered, Elasticsearch Installation using Ansible on AWS Instance, configuring Elasticsearch, install nginx and configure SSL in nginx.

Related Articles

How to Install Ansible on Ubuntu 18.04/16.04 LTS

How to Install Netdata using Ansible Playbook

Reference

Ansible Official Documentation

Mahesh Karale

I am Mahesh Karale working as DevOps Engineer. Likes to Explore and Research on Linux, Cloud and DevOps Tools.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link