Using Ansible to install and configure Elasticsearch on Ubuntu 20.04

In this guide, we will learn how to install and configure Elasticsearch on Ubuntu 20.04 using Ansible.

Elasticsearch is a distributed search and analytics engine built on Apache Lucene. It provides a distributed, multitenant-capable full-text search engine with an HTTP web interface and schema-free JSON documents. Elasticsearch has quickly become the most popular search engine and is commonly used for log analytics, full-text search, security intelligence, business analytics, and operational intelligence use cases.

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows.

Related Content

Prerequisites

To follow along, ensure that you have:

  • An updated Ubuntu 20.04 server with at least 2 GB of RAM and 2 cores
  • Root access to the server or user with sudo access
  • Access to the internet from the server
  • Ansible installed on the local system

Table of Content

  1. Ensuring that the server is up to date
  2. Installing some packages necessary for the set up
  3. Import the Elasticsearch PGP Key
  4. Install Elasticsearch from APT repository
  5. Setup handlers
  6. Create hosts file
  7. The whole playbook
  8. Running the Ansible playbook

1. Ensuring that the server is up to date

Before proceeding, ensure that the server is up to date using these tasks. First we refresh the repos then we upgrade all packages.

- name: Update apt repo and cache on Ubuntu box
  apt:
    update_cache: yes
    force_apt_get: yes
    cache_valid_time: 3600

- name: Upgrade all packages on servers
  apt:
    upgrade: dist
    force_apt_get: yes

2. Installing necessary packages

Next, let us install some common packages that we will need in our playbook. The wget package will be used to download some files.

- name: Install required packages
  dnf:
    name:
      - vim
      - wget
    state: present

3. Import the Elasticsearch PGP Key

Elasticsearch signs all of its packages with the Elasticsearch Signing Key. Download and install the public signing key using this role:

- name: Import the Elasticsearch PGP Key
  shell: |
    wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
  args:
    warn: no

4. Install Elasticsearch from the APT repository

We need to create a repository definition file since Elasticsearch is not available in the default. We can then install elastic search after updating the apt cache.

- name: Add repository defiition
  copy:
    dest: /etc/apt/sources.list.d/elastic-7.x.list
    content: |
      deb https://artifacts.elastic.co/packages/7.x/apt stable main

- name: Install the Elasticsearch package
  apt:
    name: elasticsearch
    state: present
    update_cache: yes
  notify:
    - Start Elasticsearch
    - Enable Elasticsearch

In the above, we are also notifying handlers to

Start Elasticsearch and Enable Elasticsearch.

5. Set up handlers

Handlers are just like normal tasks in an Ansible playbook but they run only when if the Task contains a “notify” directive. It also indicates that it changed something. 

Let us set up handlers for out Elasticsearch installation and set up:

handlers:
  - name: Start Elasticsearch
    systemd:
      name: elasticsearch
      state: started

  - name: Enable Elasticsearch
    systemd:
      name: elasticsearch
      enabled: yes

6. Creating the hosts file

Ansible will execute the tasks against some inventory. The server inventory will be added as a hosts.yaml file that defines how the servers will be reached. This is my hosts file:

all:
  hosts:
    elastsrv:
      ansible_ssh_host: 10.2.11.10
      ansible_ssh_user: ubuntu

7. The whole playbook

This is the whole playbook. Save it as <meta charset="utf-8">elasticsearch.yaml

---
- name: Install Elasticsearch on Ubuntu
  hosts: elastsrv
  become: yes
  gather_facts: False
  tasks:
    - name: Update apt repo and cache on all Ubuntu box
      apt:
        update_cache: yes
        force_apt_get: yes
        cache_valid_time: 3600

    - name: Upgrade all packages on servers
      apt:
        upgrade: dist
        force_apt_get: yes

    - name: Set hostname
      hostname:
        name: elastsrv.citizix.com

    - name: Install Common packages
      apt:
        name:
          - vim
          - wget
        state: latest

    - name: Import the Elasticsearch PGP Key
      shell: |
        wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
      args:
        warn: no

    - name: Add repository defiition
      copy:
        dest: /etc/apt/sources.list.d/elastic-7.x.list
        content: |
          deb https://artifacts.elastic.co/packages/7.x/apt stable main

    - name: Install the Elasticsearch package
      apt:
        name: elasticsearch
        state: present
        update_cache: yes
      notify:
        - Start Elasticsearch
        - Enable Elasticsearch

  handlers:
    - name: Start Elasticsearch
      systemd:
        name: elasticsearch
        state: started

    - name: Enable Elasticsearch
      systemd:
        name: elasticsearch
        enabled: yes

8. Running the ansible playbook

To run the ansible playbook, you need ansible installed locally. You can install ansible using OS package manager or if you have python pip you can use it:

sudo pip install ansible

You also need to have ssh access to the server. Ensure that you set up connection to the server. I am using ssh key authentication and ssh works for me using this command:

ssh ubuntu@10.2.11.10

Now you can run the playbook using this command:

ansible-playbook -i hosts.yaml elasticsearch.yaml -vv

Once the playbook finish executing you can access the Elasticsearch server installed on the server.

comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy