Skip to content

Commit

Permalink
Homework 8.fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Vasiliy Motchenko committed Nov 16, 2023
1 parent fa39904 commit 44cd307
Showing 1 changed file with 31 additions and 25 deletions.
56 changes: 31 additions & 25 deletions ansible/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 44cd307

Please sign in to comment.