Pass instance public IP to ansible. Fix CTRL-C with timeout loop.

This commit is contained in:
Erik Stambaugh 2022-01-02 08:37:22 -08:00
parent c035ce9b54
commit 7e61c99797
3 changed files with 15 additions and 9 deletions

View file

@ -4,23 +4,26 @@ SSH := ssh -o "StrictHostKeyChecking=no" -o UserKnownHostsFile=/dev/null -o Prox
default: terraform ansible default: terraform ansible
# I can't be relied on to remember the command to connect to the instance # I can't be relied on to remember the command to connect to the instance
ssh: ssh: setup
$(eval INSTANCE := $(shell terraform output instance_id | sed -e 's/"//g'))
$(SSH) $(INSTANCE) $(SSH) $(INSTANCE)
ssh_setup: setup:
chmod 600 privkey.pem
$(eval INSTANCE := $(shell terraform output instance_id | sed -e 's/"//g')) $(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' $(eval PUBLIC_IP := $(shell terraform output public_ip | sed -e 's/"//g'))
chmod 600 privkey.pem
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)" $(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 ansible-playbook -i inventory.ini --private-key privkey.pem -l nextcloud site.yaml
terraform: terraform:
terraform init terraform init
terraform apply terraform apply
.PHONY: ssh_setup ansible terraform .PHONY: setup ansible terraform

View file

@ -1,4 +1,4 @@
[nextcloud] [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] [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\"" 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\""

View file

@ -140,4 +140,7 @@ data "aws_iam_policy" "nextcloud" {
output "instance_id" { output "instance_id" {
value = aws_instance.nextcloud.id value = aws_instance.nextcloud.id
} }
output "public_ip" {
value = aws_instance.nextcloud.public_ip
}