Deploying Elastic Stack With Ansible Playbook

1 minute read

As a devops consultant, I have worked with ansible quite a bit. Following the principles of infrastructure as code and with the motivation to automate as much infrastructure work as possible, many of our configuration are automated and manage as normal code, we commit them with git and even run yaml syntax check in our ci/cd pipeline.

One of the configuration scripts I did was for automating the configuration of the elastic stack.

This script is availabe at my github repo,

This script configure:

  • elasticsearch cluster with N number of nodes.
  • kibana
  • curator
  • metricbeats on all compute nodes.
  • import default dashboard for metricbeat

Below is the main playbook file site.yml

- hosts: all
  gather_facts: false
  become: true
    - python

- hosts: elasticsearch
  become: true
    cluster_name: openstack_test
    es_heap_size: 2g
    - java8
    - elastic5x
    - elasticsearch

- hosts: kibana
  become: true
    kibana_port: 5601
    - elastic5x
    - kibana

- hosts: compute
  become: true
    - elastic5x
    - metricbeat
    - filebeat

- hosts: curator
  become: true
    delete_older_than_days: 1
    - curator

- hosts: compute[0]
  - name: import metricbeats dashboards to kibana
    shell: /usr/share/metricbeat/scripts/import_dashboards
  - name: import filebeat dashboards to kibana
    shell: /usr/share/filebeat/scripts/import_dashboards


Playbook contains plays that will run when executed, plays maps to inventory. Plays contains Roles. Roles are basically recommended encapsulation of a series of steps that invoke ansible modules. Modules are built-in idempontent functions that configure the target servers. Servers are grouped and defined in inventories, they are defined in an inventory file.

Below is an example inventory file:


ansible_user = ubuntu
ansible_ssh_private_key_file = xxx




Running the playbook

To run the playbook, it is as simple as:

ansible-playbook -i inventory-sample site.yml


In conclusion, ansible is a simple yet awesome tools for automating configuration of softwares/services. Get started with installation here.