Skip to content
This repository was archived by the owner on Jan 10, 2025. It is now read-only.

Commit d351c04

Browse files
authored
Merge pull request #292 from Kushmaro/terraform-ansible-example
Updated example to install ece using ansible
2 parents 5d3a4a1 + 8de73fd commit d351c04

File tree

10 files changed

+259
-109
lines changed

10 files changed

+259
-109
lines changed

Cloud Enterprise/Getting Started Examples/aws/terraform/README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
## Details
44

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

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

99
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.
1010

11+
## Prereqs on your machine
12+
- Terraform v0.11.X
13+
- Ansible
14+
1115
## Usage
1216

1317
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.
@@ -17,14 +21,20 @@ Then to start it up, run the following:
1721
```console
1822
> terraform init
1923
> terraform apply
24+
```
2025

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

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

2636
To tear it down run:
2737

2838
```console
2939
> terraform destroy
30-
```
40+
```
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#!/usr/bin/env bash
2+
3+
# Short form: set -u
4+
set -o nounset
5+
# Short form: set -e
6+
set -o errexit
7+
8+
# Print a helpful message if a pipeline with non-zero exit code causes the
9+
# script to exit as described above.
10+
trap 'echo "Aborting due to errexit on line $LINENO. Exit code: $?" >&2' ERR
11+
12+
# Allow the above trap be inherited by all functions in the script.
13+
#
14+
# Short form: set -E
15+
set -o errtrace
16+
17+
# Return value of a pipeline is the value of the last (rightmost) command to
18+
# exit with a non-zero status, or zero if all commands in the pipeline exit
19+
# successfully.
20+
set -o pipefail
21+
22+
# Set $IFS to only newline and tab.
23+
#
24+
# http://www.dwheeler.com/essays/filenames-in-shell.html
25+
IFS=$'\n\t'
26+
27+
###############################################################################
28+
# Program Functions
29+
###############################################################################
30+
31+
_verify_ansible() {
32+
if [ -x "$(command -v ansible-galaxy)" ]; then
33+
# install role
34+
ansible-galaxy install git+https://github.com/elastic/ansible-elastic-cloud-enterprise.git
35+
else
36+
echo "ERROR: Ansible isn't installed on this machine, aborting ece installation"
37+
exit 1
38+
fi
39+
}
40+
41+
_write_ansible_playbook() {
42+
cat << PLAYBOOK > ./ece.yml
43+
---
44+
- hosts: primary
45+
gather_facts: true
46+
roles:
47+
- ansible-elastic-cloud-enterprise
48+
vars:
49+
ece_primary: true
50+
ece_version: ${ece-version}
51+
52+
- hosts: secondary
53+
gather_facts: true
54+
roles:
55+
- ansible-elastic-cloud-enterprise
56+
vars:
57+
ece_roles: [director, coordinator, proxy, allocator]
58+
ece_version: ${ece-version}
59+
60+
- hosts: tertiary
61+
gather_facts: true
62+
roles:
63+
- ansible-elastic-cloud-enterprise
64+
vars:
65+
ece_roles: [director, coordinator, proxy, allocator]
66+
ece_version: ${ece-version}
67+
PLAYBOOK
68+
}
69+
70+
_write_ansible_hosts() {
71+
cat << HOSTS_FILE > ./hosts
72+
[primary]
73+
${ece-server0}
74+
75+
[primary:vars]
76+
availability_zone=${ece-server0-zone}
77+
78+
[secondary]
79+
${ece-server1}
80+
81+
[secondary:vars]
82+
availability_zone=${ece-server1-zone}
83+
84+
[tertiary]
85+
${ece-server2}
86+
87+
[tertiary:vars]
88+
availability_zone=${ece-server2-zone}
89+
90+
[aws:children]
91+
primary
92+
secondary
93+
tertiary
94+
95+
[aws:vars]
96+
ansible_ssh_private_key_file=${key}
97+
ansible_user=${user}
98+
ansible_become=yes
99+
device_name=${device}
100+
HOSTS_FILE
101+
}
102+
103+
_run_ansible() {
104+
export ANSIBLE_HOST_KEY_CHECKING=False
105+
ansible-playbook -i hosts ece.yml
106+
}
107+
108+
###############################################################################
109+
# Main
110+
###############################################################################
111+
112+
# _main()
113+
#
114+
# Usage:
115+
# _main [<options>] [<arguments>]
116+
#
117+
# Description:
118+
# Entry point for the program, handling basic option parsing and dispatching.
119+
_main() {
120+
_verify_ansible
121+
_write_ansible_playbook
122+
_write_ansible_hosts
123+
sleep 30
124+
_run_ansible
125+
}
126+
127+
# Call `_main` after everything has been defined.
128+
_main "$@"

Cloud Enterprise/Getting Started Examples/aws/terraform/install.sh

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Initiate ece installation through ansible playbook
2+
resource "null_resource" "run-ansible" {
3+
provisioner "local-exec" {
4+
command = "${data.template_file.ansible-install.rendered}"
5+
}
6+
}
7+
8+
output "ece-instances" {
9+
description = "The public dns of created server instances."
10+
value = ["${aws_instance.server.*.public_dns}"]
11+
}
12+
13+
output "installed-ece-url" {
14+
value = "${format("https://%s:12443","${aws_instance.server.0.public_dns}")}"
15+
}

Cloud Enterprise/Getting Started Examples/aws/terraform/networking.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,4 @@ resource "aws_security_group" "internal" {
113113
to_port = 0
114114
self = true
115115
}
116-
}
116+
}
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
provider "aws" {
22
region = "${var.region}"
33
profile = "${var.profile}"
4-
version = "1.3.0"
5-
}
6-
7-
provider "template" {
8-
version = "1.0.0"
94
}
105

116
terraform {
12-
required_version = "0.11.1"
13-
}
7+
required_version = "0.11.14"
8+
}

Cloud Enterprise/Getting Started Examples/aws/terraform/servers.tf

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,44 @@ resource "aws_instance" "server" {
3535
key_name = "${aws_key_pair.server.key_name}"
3636

3737
root_block_device {
38-
volume_size = 100
38+
volume_size = "${var.root_device_size}"
3939
}
4040

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

46-
user_data = "${file(var.user_data)}"
46+
ebs_block_device {
47+
delete_on_termination = true
48+
device_name = "${var.secondary_device_name}"
49+
volume_type = "gp2"
50+
volume_size = "${var.secondary_device_size}"
51+
}
4752
}
53+
54+
data "template_file" "ansible-install" {
55+
template = "${file("ansible-install.sh")}"
56+
depends_on = ["aws_instance.server"]
57+
vars = {
58+
# Created servers and appropriate AZs
59+
ece-server0 = "${aws_instance.server.0.public_dns}"
60+
ece-server0-zone = "${aws_instance.server.0.availability_zone}"
61+
ece-server1 = "${aws_instance.server.1.public_dns}"
62+
ece-server1-zone = "${aws_instance.server.1.availability_zone}"
63+
ece-server2 = "${aws_instance.server.2.public_dns}"
64+
ece-server2-zone = "${aws_instance.server.2.availability_zone}"
65+
66+
# Keys to server
67+
key = "${var.private_key}"
68+
69+
# Server Device Name
70+
device = "${var.secondary_device_name}"
71+
72+
# User to login
73+
user = "${var.remote_user}"
74+
75+
# Ece version to install
76+
ece-version = "${var.ece-version}"
77+
}
78+
}

Cloud Enterprise/Getting Started Examples/aws/terraform/terraform.tfvars.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ profile = "YOUR-AWS-PROFILE"
44

55
trusted_network = "YOUR-IP/32"
66

7-
name = "YOUR-NAME"
7+
name = "YOUR-NAME"

Cloud Enterprise/Getting Started Examples/aws/terraform/user_data.sh

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)