From 7e61c99797c737cdf3e909ed3e3dc2019751dada Mon Sep 17 00:00:00 2001 From: Erik Stambaugh Date: Sun, 2 Jan 2022 08:37:22 -0800 Subject: [PATCH] Pass instance public IP to ansible. Fix CTRL-C with timeout loop. --- Makefile | 19 +++++++++++-------- inventory.tmpl.ini | 2 +- nextcloud.tf | 3 +++ 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index fc5625c..6dfde5e 100644 --- a/Makefile +++ b/Makefile @@ -4,23 +4,26 @@ SSH := ssh -o "StrictHostKeyChecking=no" -o UserKnownHostsFile=/dev/null -o Prox default: terraform ansible # I can't be relied on to remember the command to connect to the instance -ssh: - $(eval INSTANCE := $(shell terraform output instance_id | sed -e 's/"//g')) +ssh: setup $(SSH) $(INSTANCE) -ssh_setup: +setup: + $(eval INSTANCE := $(shell terraform output instance_id | sed -e 's/"//g')) + $(eval PUBLIC_IP := $(shell terraform output public_ip | sed -e 's/"//g')) chmod 600 privkey.pem - $(eval INSTANCE := $(shell terraform output instance_id | sed -e 's/"//g')) - timeout 300 bash -c -- 'until $(SSH) $(INSTANCE) "/bin/true"; do sleep 0.5; done' + timeout --foreground 300 bash -c -- 'until $(SSH) $(INSTANCE) "/bin/true"; do sleep 0.5; done' -ansible: ssh_setup +ansible: setup $(SSH) $(INSTANCE) "which -a ansible || (sudo apt-get update && sudo apt-get -y install ansible)" - sed -e 's/{{INSTANCE}}/$(INSTANCE)/' inventory.tmpl.ini > inventory.ini + sed \ + -e 's/{{INSTANCE}}/$(INSTANCE)/' \ + -e 's/{{PUBLIC_IP}}/$(PUBLIC_IP)/' \ + inventory.tmpl.ini > inventory.ini ansible-playbook -i inventory.ini --private-key privkey.pem -l nextcloud site.yaml terraform: terraform init terraform apply -.PHONY: ssh_setup ansible terraform +.PHONY: setup ansible terraform diff --git a/inventory.tmpl.ini b/inventory.tmpl.ini index 72ca8c1..11fb7e6 100644 --- a/inventory.tmpl.ini +++ b/inventory.tmpl.ini @@ -1,4 +1,4 @@ [nextcloud] -nextcloud ansible_host={{INSTANCE}} ansible_user=ubuntu hostname=nextcloud +nextcloud ansible_host={{INSTANCE}} ansible_user=ubuntu hostname=nextcloud public_ip={{PUBLIC_IP}} [nextcloud:vars] ansible_ssh_common_args=-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ProxyCommand="sh -c \"aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters portNumber=%p\"" diff --git a/nextcloud.tf b/nextcloud.tf index 863621b..a147a99 100644 --- a/nextcloud.tf +++ b/nextcloud.tf @@ -140,4 +140,7 @@ data "aws_iam_policy" "nextcloud" { output "instance_id" { value = aws_instance.nextcloud.id } +output "public_ip" { + value = aws_instance.nextcloud.public_ip +}