Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
cweibel committed Jul 5, 2022
1 parent 9d8c5ff commit 30c2997
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
44 changes: 43 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,44 @@
# terraform-module-remote-exec-template-run-once
Terraform module to render a template, scp it to a bastion and execute the script only once
Terraform module to render a template, scp it to a bastion and execute the script only once.

This is basically the same thing as `terraform-module-remote-exec-template-run` but without the UUID trigger


Inputs - Required:

- `template_name` - Relative path and file name to the template file to render
- `template_vars` - Map of variables which will be substituted into the template
- `rendered_file_name` - Name of the rendered template file which will be copied onto the vm
- `host` - IP address or DNS resolveable hostname to the bastion/taget vm
- `ssh_private_key` - SSH Private Key File


Inputs - Optional:

- `bastion_user_name` - default = "ubuntu", Name of the user on the bastion/target vm to be used for ssh
- `rendered_file_destination` - default = "/home/ubuntu", Absolute folder of where to copy the rendered file to

Outputs:

- `rendered_file_contents` - Outputs the contents of the rendered file
- `rendered_file_location` - Path to the folder and rendered file name


Example Usage:

```hcl
module "stuff" {
source = "github.com/cweibel/terraform-module-remote-exec-template-run-once.git?ref=0.0.1"
template_name = "templates/configure-mgmt-bosh-rds.tpl"
template_vars = {
pickles = "mypickles"
}
rendered_file_name = "hijason.sh"
host = module.bastion.box-bastion-public
ssh_private_key = file(var.aws_key_file)
}
output "stuff" {value = module.stuff.rendered_file_contents}
```
47 changes: 47 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

variable template_name {} # Relative path and file name to the template file to render
variable template_vars {} # Map of variables which will be substituted into the template
variable rendered_file_name {} # Name of the rendered template file which will be copied onto the vm
variable host {} # IP address or DNS resolveable hostname to the bastion/taget vm
variable ssh_private_key {} # SSH Private Key File

variable bastion_user_name {default = "ubuntu"} # Name of the user on the bastion/target vm to be used for ssh
variable rendered_file_destination {default = "/home/ubuntu"} # Absolute folder name of where to copy the rendered file to


data "template_file" "myfile" {
template = file("./${var.template_name}")
vars = var.template_vars
}

resource "null_resource" "configure_bosh_rds" {

# Copy the script to the bastion
provisioner "file" {
content = "${data.template_file.myfile.rendered}"
destination = "${var.rendered_file_destination}/${var.rendered_file_name}"
connection {
type = "ssh"
user = var.bastion_user_name
private_key = var.ssh_private_key
host = var.host
}
}

# Run the script on the bastion
provisioner "remote-exec" {
inline = [
"chmod +x ${var.rendered_file_destination}/${var.rendered_file_name}",
"${var.rendered_file_destination}/${var.rendered_file_name}"
]
connection {
type = "ssh"
user = var.bastion_user_name
private_key = var.ssh_private_key
host = var.host
}
}
}

output "rendered_file_contents" {value = data.template_file.myfile.rendered }
output "rendered_file_location" {value = "${var.rendered_file_destination}/${var.rendered_file_name}" }

0 comments on commit 30c2997

Please sign in to comment.