-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extend previous setup to support ansible as a second stage
- Loading branch information
Showing
3 changed files
with
41 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,35 @@ | ||
# Infrastructure as Code | ||
|
||
## Preqreqs: | ||
- terraform 0.13.1 on the local deploy machine | ||
|
||
Terraform is used to deploy resources on digital ocean. Then once the resources | ||
are deployed, ansible is used to configure them. | ||
|
||
Most of this was created from this guide: | ||
https://www.digitalocean.com/community/tutorials/how-to-use-terraform-with-digitalocean#step-4-%E2%80%94-using-terraform-to-create-the-nginx-server | ||
|
||
Everything is made to work via setting two environment variables. The DO_PAT | ||
is the digital ocean API token. The pvt_key is set to the key which should be | ||
rolled out to the deployed resources. | ||
|
||
## Commands: | ||
## Terraform Commands: | ||
To plan: | ||
`terraform plan -var "do_token=${DO_PAT}" -var "pvt_key=$HOME/.ssh/id_rsa"` | ||
`terraform plan -var "do_token=${DO_PAT}" -var "pvt_key=$HOME/.ssh/id_rsa" -var "pub_key=$HOME/.ssh/id_rsa.pub"` | ||
|
||
To apply: | ||
`terraform apply -var "do_token=${DO_PAT}" -var "pvt_key=$HOME/.ssh/id_rsa"` | ||
`terraform apply -var "do_token=${DO_PAT}" -var "pvt_key=$HOME/.ssh/id_rsa" -var "pub_key=$HOME/.ssh/id_rsa.pub"` | ||
|
||
To show state: | ||
`terraform show terraform.tfstate` | ||
|
||
To destroy: | ||
`terraform plan -destroy -out=terraform.tfplan \ | ||
-var "do_token=${DO_PAT}" \ | ||
-var "pvt_key=$HOME/.ssh/id_rsa" \` | ||
-var "pvt_key=$HOME/.ssh/id_rsa" \ | ||
-var "pub_key=$HOME/.ssh/id_rsa.pub"` | ||
|
||
to make the destroy plan and `terraform apply terraform.tfplan` | ||
|
||
## Ansible | ||
https://www.digitalocean.com/community/tutorials/how-to-use-ansible-with-terraform-for-configuration-management |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,36 @@ | ||
resource "digitalocean_droplet" "compute-1" { | ||
resource "digitalocean_droplet" "compute" { | ||
count = 1 | ||
image = "ubuntu-20-10-x64" | ||
name = "compute-1" | ||
name = "compute-${count.index}" | ||
region = "sfo2" | ||
size = "s-1vcpu-1gb" | ||
private_networking = true | ||
ssh_keys = [ | ||
data.digitalocean_ssh_key.terraform.id | ||
] | ||
|
||
connection { | ||
host = self.ipv4_address | ||
user = "root" | ||
type = "ssh" | ||
private_key = file(var.pvt_key) | ||
timeout = "2m" | ||
provisioner "remote-exec" { | ||
inline = ["sudo apt update", "sudo apt install python3 -y", "echo Done!"] | ||
|
||
connection { | ||
host = self.ipv4_address | ||
user = "root" | ||
type = "ssh" | ||
private_key = file(var.pvt_key) | ||
timeout = "2m" | ||
} | ||
} | ||
|
||
# todo: enable this when we have local ansible and ansible galaxy setup | ||
# along with the ansible docker role | ||
# provisioner "local-exec" { | ||
# command = "ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -u root -i '${self.ipv4_address},' --private-key ${var.pvt_key} -e 'pub_key=${var.pub_key}' apache-install.yml" | ||
# } | ||
} | ||
|
||
output "droplet_ip_addresses" { | ||
value = { | ||
for droplet in digitalocean_droplet.compute: | ||
droplet.name => droplet.ipv4_address | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters