summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2020-10-04 17:34:44 +0300
committerLars Wirzenius <liw@liw.fi>2020-10-04 17:34:44 +0300
commit32e9d47ce40da71c31fd39192ba97597598d6ad2 (patch)
treeeda1cf73966ccbac44fb18aa11741cb5d8377db0
parentdc0adcc9bcd65bd001fc94bf4bd3eb61df71c079 (diff)
downloadick-contractor-32e9d47ce40da71c31fd39192ba97597598d6ad2.tar.gz
create-vm ansible
-rwxr-xr-xcreate-vm36
1 files 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()