Skip to content

Instantly share code, notes, and snippets.

@tomwwright
Created March 19, 2018 09:42
Show Gist options
  • Save tomwwright/a676a93c0d772487877d5a0359e4c04d to your computer and use it in GitHub Desktop.
Save tomwwright/a676a93c0d772487877d5a0359e4c04d to your computer and use it in GitHub Desktop.
ansibled : elasticsearch : tasks : setup cluster
# tasks/elasticsearch/setup.cluster.yml
# ---
# look up our Elasticsearch cluster, create it if necessary, wait for it to be
# available, then update the DNS record for it in Route 53
# use the AWS CLI to query for details about this cluster, if it exists
- name: check for existing Elasticsearch cluster
command: aws es describe-elasticsearch-domains --region {{ aws_region }} --domain-names {{ elasticsearch_name }}
changed_when: false
register: elasticsearch_cluster_query
# handle the output from the CLI
- name: parse Elasticsearch cluster query
set_fact:
elasticsearch_cluster: "{{ (elasticsearch_cluster_query.stdout | from_json).DomainStatusList[0] }}"
when: (elasticsearch_cluster_query.stdout | from_json).DomainStatusList[0] is defined
# if the cluster doesn't exist (`when` clause), call the CLI to create it -- passing an evaluated template as the configuration
- name: create Elasticsearch cluster
command: aws es create-elasticsearch-domain --region {{ aws_region }} --cli-input-json '{{ lookup('template', 'files/create-elasticsearch-domain.json.j2') | to_json }}'
when: elasticsearch_cluster is not defined
register: elasticsearch_cluster_create
# poll the AWS CLI using `until` to wait until the output shows our Elasticsearch endpoint has appeared
- name: wait for Elasticsearch endpoint to be availabile
command: aws es describe-elasticsearch-domains --region {{ aws_region }} --domain-names {{ elasticsearch_name }}
changed_when: false
register: elasticsearch_cluster_query
until: (elasticsearch_cluster_query.stdout | from_json).DomainStatusList[0].Endpoints is defined
retries: 15
delay: 60
# handle the output from the CLI a bit
- name: parse Elasticsearch cluster query
set_fact:
elasticsearch_cluster: "{{ (elasticsearch_cluster_query.stdout | from_json).DomainStatusList[0] }}"
# use an Ansible module to update the Route 53 record for our Elasticsearch host
- name: update VPC DNS for Elasticsearch cluster
route53:
state: present
zone: "{{ vpc_dns_zone }}"
private_zone: true
record: "{{ elasticsearch_domain_name }}"
type: CNAME
overwrite: true
ttl: 300
value: "{{ elasticsearch_cluster.Endpoints.vpc }}"
when: elasticsearch_cluster.Endpoints is defined and elasticsearch_domain_name is defined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment