#!/usr/bin/env python3 # Update hosts file using Terraform output import subprocess import re hosts = subprocess.check_output(['terraform', 'output']).decode("utf-8") lines = hosts.split("\n") master = lines[5].replace(",", "").strip() replica = lines[6].strip() master_internal = lines[1].replace(",", "").strip() replica_internal = lines[2].strip() with open("hosts", "w") as f: f.write("""[all:vars] ansible_ssh_user=ec2-user ansible_ssh_private_key_file=/Users/rcowham/.ssh/id_rsa [p4instances] master ansible_ssh_host=%s replica ansible_ssh_host=%s """ % (master, replica)) with open("sdp/hosts.yaml", "w") as f: f.write("""# Hosts - for convenience of ansible master: %s replica: %s master_internal: %s replica_internal: %s """ % (master, replica, master_internal, replica_internal)) # Accept the r = subprocess.check_output(['bash', '-c', 'ssh-keyscan -H %s >> ~/.ssh/known_hosts' % master]) r = subprocess.check_output(['bash', '-c', 'ssh-keyscan -H %s >> ~/.ssh/known_hosts' % replica]) print("") print("ssh ec2-user@%s" % master) print("ssh ec2-user@%s" % replica)