diff --git a/ansible/generate.py b/ansible/generate.py index be1c9e1..eb79822 100755 --- a/ansible/generate.py +++ b/ansible/generate.py @@ -6,30 +6,36 @@ import sys def main(): - if len(sys.argv) > 1 and sys.argv[1] == "--list": - data = { - "_meta": { - "hostvars": {} - }, - "all": { - "children": ["app", "db"] - }, - "app": { - "hosts": [] - }, - "db": { - "hosts": [] - } - } - - os.chdir("../terraform/stage") - output = subprocess.check_output(["terraform", "output", "-json"]).decode() - output_dict = json.loads(output) - - data["app"]["hosts"].append(output_dict.get("external_ip_address_app", {"value": "1.2.3.4"})["value"]) - data["db"]["hosts"].append(output_dict.get("external_ip_address_db", {"value": "1.2.3.4"})["value"]) - - print(json.dumps(data, indent=4)) + if len(sys.argv) > 1 and sys.argv[1] == "--list": + data = { + "_meta": { + "hostvars": {} + }, + "all": { + "children": ["app", "db"] + }, + "app": { + "hosts": [] + }, + "db": { + "hosts": [] + } + } + + try: + os.chdir("../terraform/stage") + output = subprocess.check_output(["terraform", "output", "-json"]).decode() + output_dict = json.loads(output) + + data["app"]["hosts"].append(output_dict["external_ip_address_app"]["value"]) + data["db"]["hosts"].append(output_dict["external_ip_address_db"]["value"]) + + except Exception as e: + data["app"]["hosts"].append("1.2.3.4") + data["db"]["hosts"].append("4.3.2.1") + + + print(json.dumps(data, indent=4)) if __name__ == "__main__": - main() + main()