From: Lorin Hochstein Date: Thu, 5 Feb 2015 03:09:03 +0000 (-0500) Subject: add vagrant.py and production vars X-Git-Url: https://git.halfball.org/?a=commitdiff_plain;h=509885a6c08bc47096c9ab3436bc726fb07f182a;p=ansiblebook.git add vagrant.py and production vars --- diff --git a/ch03/group_vars/production b/ch03/group_vars/production new file mode 100644 index 0000000..9a8e4b0 --- /dev/null +++ b/ch03/group_vars/production @@ -0,0 +1,7 @@ +--- +db_primary_host: rhodeisland.example.com +db_replica_host: virginia.example.com +db_name: widget_production +db_user: widgetuser +db_password: pFmMxcyD;Fc6)6 +redis_host: pennsylvania.example.com diff --git a/ch03/inventory b/ch03/inventory deleted file mode 100644 index d99e195..0000000 --- a/ch03/inventory +++ /dev/null @@ -1,12 +0,0 @@ -ontario.example.com -newhampshire.example.com -maryland.example.com -virginia.example.com -newyork.example.com -quebec.example.com -rhodeisland.example.com - -[vagrant] -vagrant1 ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222 -vagrant2 ansible_ssh_host=127.0.0.1 ansible_ssh_port=2200 -vagrant3 ansible_ssh_host=127.0.0.1 ansible_ssh_port=2201 diff --git a/ch03/inventory/hosts b/ch03/inventory/hosts new file mode 100644 index 0000000..80dedfa --- /dev/null +++ b/ch03/inventory/hosts @@ -0,0 +1,48 @@ +[production] +delaware.example.com +georgia.example.com +maryland.example.com +newhampshire.example.com +newjersey.example.com +newyork.example.com +northcarolina.example.com +pennsylvania.example.com +rhodeisland.example.com +virginia.example.com + +[staging] +ontario.example.com +quebec.example.com + +[vagrant] +vagrant1 ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222 +vagrant2 ansible_ssh_host=127.0.0.1 ansible_ssh_port=2200 +vagrant3 ansible_ssh_host=127.0.0.1 ansible_ssh_port=2201 + +[lb] +delaware.example.com + +[web] +georgia.example.com +newhampshire.example.com +newjersey.example.com +ontario.example.com +vagrant1 + +[task] +newyork.example.com +northcarolina.example.com +maryland.example.com +ontario.example.com +vagrant2 + +[redis] +pennsylvania.example.com +quebec.example.com +vagrant3 + +[db] +rhodeisland.example.com +virginia.example.com +quebec.example.com +vagrant3 diff --git a/ch03/inventory/vagrant.py b/ch03/inventory/vagrant.py new file mode 100755 index 0000000..5a2b69a --- /dev/null +++ b/ch03/inventory/vagrant.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python +# Adapted from Mark Mandel's implementation +# https://github.com/ansible/ansible/blob/devel/plugins/inventory/vagrant.py +import argparse +import json +import paramiko +import subprocess +import sys + + +def parse_args(): + parser = argparse.ArgumentParser(description="Vagrant inventory script") + group = parser.add_mutually_exclusive_group(required=True) + group.add_argument('--list', action='store_true') + group.add_argument('--host') + return parser.parse_args() + + +def list_running_hosts(): + cmd = "vagrant status --machine-readable" + status = subprocess.check_output(cmd.split()).rstrip() + hosts = [] + for line in status.split('\n'): + (_, host, key, value) = line.split(',') + if key == 'state' and value == 'running': + hosts.append(host) + return hosts + + +def get_host_details(host): + cmd = "vagrant ssh-config {}".format(host) + p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE) + config = paramiko.SSHConfig() + config.parse(p.stdout) + c = config.lookup(host) + return {'ansible_ssh_host': c['hostname'], + 'ansible_ssh_port': c['port'], + 'ansible_ssh_user': c['user'], + 'ansible_ssh_private_key_file': c['identityfile'][0]} + + +def main(): + args = parse_args() + if args.list: + hosts = list_running_hosts() + json.dump({'vagrant': hosts}, sys.stdout) + else: + details = get_host_details(args.host) + json.dump(details, sys.stdout) + +if __name__ == '__main__': + main() +