regluit/vagrant/run_playbook.py

42 lines
1.2 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python
from __future__ import print_function
import sh
import argparse
2015-05-14 14:22:45 +00:00
import vagrant
# compute: inventory, private key, user and pass along all the other parameters to ansible-playbook
parser = argparse.ArgumentParser(description='Run the ansible playbook, using vagrant ssh-config parameters')
2015-05-14 14:22:45 +00:00
parser.add_argument('node', metavar='n', type=str, nargs='?',
help='node to run playbook on')
parser.add_argument('playbook_path', metavar='f', type=str, nargs='?',
help='path for the ansible playbook')
(args, unknown) = parser.parse_known_args()
2015-05-14 14:22:45 +00:00
print (args.node, args.playbook_path)
# for now if not multimachine, ignore node parameter
v = vagrant.Vagrant()
2015-05-14 14:22:45 +00:00
if len(v.status()) > 1:
multimachine = True
private_key = v.keyfile(args.node)
user = v.user(args.node)
else:
multimachine = False
private_key = v.keyfile()
user = v.user()
params = ["=".join(p) for p in (
2015-05-14 14:22:45 +00:00
("--private-key", private_key),
("--user", user),
("--inventory-file",".vagrant/provisioners/ansible/inventory")
)] + unknown + [args.playbook_path]
for line in sh.ansible_playbook(*params, _cwd=".", _iter=True):
print(line, end="")