# Elastic.elasticsearch role and Ansible Tower Dynamic Inventory

**URL:** <https://discuss.elastic.co/t/elastic-elasticsearch-role-and-ansible-tower-dynamic-inventory/218269>\
**Category:** Elasticsearch\
**Created:** [February 7, 2020, 3:45am UTC](https://discuss.elastic.co/t/elastic-elasticsearch-role-and-ansible-tower-dynamic-inventory/218269 "2020-02-07T03:45:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mudricd](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@mudricd](https://discuss.elastic.co/u/mudricd)\
**Post date:** [February 7, 2020, 3:45am UTC](https://discuss.elastic.co/t/elastic-elasticsearch-role-and-ansible-tower-dynamic-inventory/218269/1 "2020-02-07T03:45:37Z")

</div>

Hi,

I am trying to configure Elasticsearch cluster over Ansible using official Elastic Ansible role elastic.elasticsearch. I set up Ansible tower and Dynamic Inventory for my AWS instances. I tagged my instances and have them all grouped in tag\_Group\_Elasticsearch. Here is the Ansible code:

```auto
  hosts: tag_Group_Elasticsearch
  roles:
    - role: elastic.elasticsearch
  vars:
    es_heap_size: "2g"
    es_config:
      network.host: 0.0.0.0
      cluster.name: "prod-cluster"
      cluster.initial_master_nodes: "tag_Group_Elasticsearch"
      discovery.seed_hosts: "tag_Group_Elasticsearch"
      http.port: 9200
      node.master: true
      bootstrap.memory_lock: false
    es_plugins:
     - plugin: ingest-attachment

```

So when I want to use the group in the role it works well when it come to hosts part of the play. It resolves well and apply to all hosts in the group. But when I want to use it in es\_config variable it doesn't resolve to hosts names it just pass it as a string and therefore, elasticsearch cluster fails.

This is what I get in elasticsearch.yml on the cluster members:

```auto
cluster.initial_master_nodes: tag_Group_Elasticsearch
discovery.seed_hosts: tag_Group_Elasticsearch

```

What I need to have here is:

```auto
cluster.initial_master_nodes: "192.168.x.x, 192.168.x.x, 192.168.x.x "
discovery.seed_hosts: "192.168.x.x, 192.168.x.x, 192.168.x.x"

```

I can't add hostnames in cluster.initial\_master\_nodes and discovery.seed\_hosts manually as my Elasticsearch cluster is built over AWS autoscaling group and my hosts are dynamic...

Cheers,  
Dragan

---

<div class="post-metadata">

**Author:** ![mudricd](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@mudricd](https://discuss.elastic.co/u/mudricd)\
**Post date:** [February 10, 2020, 5:55am UTC](https://discuss.elastic.co/t/elastic-elasticsearch-role-and-ansible-tower-dynamic-inventory/218269/2 "2020-02-10T05:55:13Z")

</div>

Here is the way I fixed this. It might help someone.

First I created new variable using set\_fact:

```auto
- name: Determine nodes to join
  hosts: tag_Group_Elasticsearch
  become: true
  tasks:
  - name: Setting nodes IPs
    set_fact:
      join_list: "{{ groups['tag_Group_Elasticsearch'] | map('extract', hostvars, ['ansible_host']) | list }}" # instead 'ansible_host' you can use the folloving variables as well: ansible_fqdn, ec2_private_ip_address or ansible_nodename

```

Then I configured cluster.initial\_master\_nodes and discovery.seed\_hosts in elastic.elasticsearch role as follows:

```auto
cluster.initial_master_nodes: "{{ join_list | join(',') }}"
discovery.seed_hosts: "{{ join_list | join(',') }}"

```

As a result elasticsearch.yaml picked up the IP addresses of the nodes:

```auto
cluster.initial_master_nodes: 10.174.x.x,10.174.x.x,10.174.x.x
discovery.seed_hosts: 10.174.x.x,10.174.x.x,10.174.x.x

```

Now whenever new node is added into the group, in this case tag\_Group\_Elasticsearch, that node will be added to cluster.initial\_master\_nodes and discovery.seed\_hosts automatically.

This configuration is very useful when you have cluster nodes in AWS (EC2) and when you use AWS auto scaling and Ansible elastic.elasticsearch role for setting up cluster.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 9, 2020, 5:55am UTC](https://discuss.elastic.co/t/elastic-elasticsearch-role-and-ansible-tower-dynamic-inventory/218269/3 "2020-03-09T05:55:16Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
