Skip to content

Commit

Permalink
Merge pull request #292 from Kushmaro/terraform-ansible-example
Browse files Browse the repository at this point in the history
Updated example to install ece using ansible
  • Loading branch information
Kushmaro authored Dec 17, 2019
2 parents 5d3a4a1 + 8de73fd commit d351c04
Show file tree
Hide file tree
Showing 10 changed files with 259 additions and 109 deletions.
18 changes: 14 additions & 4 deletions Cloud Enterprise/Getting Started Examples/aws/terraform/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

## Details

This tree provides a basic test bed for an installation of [Elastic Cloud Enterprise](https://www.elastic.co/cloud/enterprise) on 2 servers.
This tree provides a basic example installation of [Elastic Cloud Enterprise](https://www.elastic.co/cloud/enterprise) on 3 servers using terraform & ansible.

Note that the terraform files and installation script are intentionally basic.

A full production deployment should make use of auto scaling groups, load balancers and other high-availability constructs which have been left out of this setup. See [the Elastic Cloud Enterprise planning docs](https://www.elastic.co/guide/en/cloud-enterprise/1.1/ece-planning.html) for additional details regarding production planning and deployment.

## Prereqs on your machine
- Terraform v0.11.X
- Ansible

## Usage

First set up a few variables for your own environment by copying `terraform.tfvars.example` to `terraform.tfvars` and replacing the placeholders with values for your own AWS account and location.
Expand All @@ -17,14 +21,20 @@ Then to start it up, run the following:
```console
> terraform init
> terraform apply
```

# wait ~60s for instance to finish installing prerequisites
Wait for the installation to complete, and hurah! you now have a 3-instance ece installation ready on AWS!

> ./install.sh
You'll be presented with the URL to log in to ece.
The admin password will be presented above as part of the running ansible flow like this:
```
null_resource.run-ansible (local-exec): ok: [some-instance-public-dns-address] => {
null_resource.run-ansible (local-exec): "msg": "Adminconsole password is: <PASSWORD> "
null_resource.run-ansible (local-exec): }
```

To tear it down run:

```console
> terraform destroy
```
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#!/usr/bin/env bash

# Short form: set -u
set -o nounset
# Short form: set -e
set -o errexit

# Print a helpful message if a pipeline with non-zero exit code causes the
# script to exit as described above.
trap 'echo "Aborting due to errexit on line $LINENO. Exit code: $?" >&2' ERR

# Allow the above trap be inherited by all functions in the script.
#
# Short form: set -E
set -o errtrace

# Return value of a pipeline is the value of the last (rightmost) command to
# exit with a non-zero status, or zero if all commands in the pipeline exit
# successfully.
set -o pipefail

# Set $IFS to only newline and tab.
#
# http://www.dwheeler.com/essays/filenames-in-shell.html
IFS=$'\n\t'

###############################################################################
# Program Functions
###############################################################################

_verify_ansible() {
if [ -x "$(command -v ansible-galaxy)" ]; then
# install role
ansible-galaxy install git+https://github.com/elastic/ansible-elastic-cloud-enterprise.git
else
echo "ERROR: Ansible isn't installed on this machine, aborting ece installation"
exit 1
fi
}

_write_ansible_playbook() {
cat << PLAYBOOK > ./ece.yml
---
- hosts: primary
gather_facts: true
roles:
- ansible-elastic-cloud-enterprise
vars:
ece_primary: true
ece_version: ${ece-version}
- hosts: secondary
gather_facts: true
roles:
- ansible-elastic-cloud-enterprise
vars:
ece_roles: [director, coordinator, proxy, allocator]
ece_version: ${ece-version}
- hosts: tertiary
gather_facts: true
roles:
- ansible-elastic-cloud-enterprise
vars:
ece_roles: [director, coordinator, proxy, allocator]
ece_version: ${ece-version}
PLAYBOOK
}

_write_ansible_hosts() {
cat << HOSTS_FILE > ./hosts
[primary]
${ece-server0}
[primary:vars]
availability_zone=${ece-server0-zone}
[secondary]
${ece-server1}
[secondary:vars]
availability_zone=${ece-server1-zone}
[tertiary]
${ece-server2}
[tertiary:vars]
availability_zone=${ece-server2-zone}
[aws:children]
primary
secondary
tertiary
[aws:vars]
ansible_ssh_private_key_file=${key}
ansible_user=${user}
ansible_become=yes
device_name=${device}
HOSTS_FILE
}

_run_ansible() {
export ANSIBLE_HOST_KEY_CHECKING=False
ansible-playbook -i hosts ece.yml
}

###############################################################################
# Main
###############################################################################

# _main()
#
# Usage:
# _main [<options>] [<arguments>]
#
# Description:
# Entry point for the program, handling basic option parsing and dispatching.
_main() {
_verify_ansible
_write_ansible_playbook
_write_ansible_hosts
sleep 30
_run_ansible
}

# Call `_main` after everything has been defined.
_main "$@"
55 changes: 0 additions & 55 deletions Cloud Enterprise/Getting Started Examples/aws/terraform/install.sh

This file was deleted.

15 changes: 15 additions & 0 deletions Cloud Enterprise/Getting Started Examples/aws/terraform/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Initiate ece installation through ansible playbook
resource "null_resource" "run-ansible" {
provisioner "local-exec" {
command = "${data.template_file.ansible-install.rendered}"
}
}

output "ece-instances" {
description = "The public dns of created server instances."
value = ["${aws_instance.server.*.public_dns}"]
}

output "installed-ece-url" {
value = "${format("https://%s:12443","${aws_instance.server.0.public_dns}")}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,4 @@ resource "aws_security_group" "internal" {
to_port = 0
self = true
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
provider "aws" {
region = "${var.region}"
profile = "${var.profile}"
version = "1.3.0"
}

provider "template" {
version = "1.0.0"
}

terraform {
required_version = "0.11.1"
}
required_version = "0.11.14"
}
35 changes: 33 additions & 2 deletions Cloud Enterprise/Getting Started Examples/aws/terraform/servers.tf
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,44 @@ resource "aws_instance" "server" {
key_name = "${aws_key_pair.server.key_name}"

root_block_device {
volume_size = 100
volume_size = "${var.root_device_size}"
}

tags {
Name = "${var.name}-${element(var.zones, count.index)}"
managed-by = "terraform"
}

user_data = "${file(var.user_data)}"
ebs_block_device {
delete_on_termination = true
device_name = "${var.secondary_device_name}"
volume_type = "gp2"
volume_size = "${var.secondary_device_size}"
}
}

data "template_file" "ansible-install" {
template = "${file("ansible-install.sh")}"
depends_on = ["aws_instance.server"]
vars = {
# Created servers and appropriate AZs
ece-server0 = "${aws_instance.server.0.public_dns}"
ece-server0-zone = "${aws_instance.server.0.availability_zone}"
ece-server1 = "${aws_instance.server.1.public_dns}"
ece-server1-zone = "${aws_instance.server.1.availability_zone}"
ece-server2 = "${aws_instance.server.2.public_dns}"
ece-server2-zone = "${aws_instance.server.2.availability_zone}"

# Keys to server
key = "${var.private_key}"

# Server Device Name
device = "${var.secondary_device_name}"

# User to login
user = "${var.remote_user}"

# Ece version to install
ece-version = "${var.ece-version}"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ profile = "YOUR-AWS-PROFILE"

trusted_network = "YOUR-IP/32"

name = "YOUR-NAME"
name = "YOUR-NAME"

This file was deleted.

Loading

0 comments on commit d351c04

Please sign in to comment.