From 32e9d47ce40da71c31fd39192ba97597598d6ad2 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 4 Oct 2020 17:34:44 +0300 Subject: create-vm ansible --- create-vm | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/create-vm b/create-vm index e085598..b65095e 100755 --- a/create-vm +++ b/create-vm @@ -83,6 +83,36 @@ def wait_for_ssh(hostname): break +def provision(vm, pubkey): + ssh_opts = [ + "ControlMaster=auto", + "ControlPersist=60s", + "StrictHostKeyChecking=accept-new", + "UserKnownHostsFile=/dev/null", + ] + + env = dict(os.environ) + env["ANSIBLE_SSH_ARGS"] = " ".join(f"-o{opt}" for opt in ssh_opts) + + vars_file = {"user_pub": pubkey} + + fd, filename = tempfile.mkstemp() + os.write(fd, yaml.safe_dump(vars_file).encode("UTF-8")) + os.close(fd) + + argv = [ + "ansible-playbook", + "-i", + "hosts", + "manager.yml", + f"-eansible_ssh_host={vm}", + f"-e@{filename}", + ] + subprocess.check_output(argv, env=env) + + os.remove(filename) + + def main(): config = yaml.safe_load(open(sys.argv[1])) @@ -96,9 +126,9 @@ def main(): memory = int(memory) cpus = int(cpus) - pubkey = open(pubkey).read() - + pubkey = open(pubkey).read().rstrip() iso = f"{vm}.iso" + cloud_init_iso(iso, vm, pubkey) if os.path.exists(img): @@ -111,5 +141,7 @@ def main(): wait_for_ssh(vm) os.remove(iso) + provision(vm, pubkey) + main() -- cgit v1.2.1