From 6eca7eca68648c217c5cc290ac4cbf8be3162335 Mon Sep 17 00:00:00 2001 From: Christopher Hornberger Date: Thu, 7 Mar 2024 18:12:27 +0100 Subject: [PATCH] chore: refactor functions --- vagrant_inventory.py | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/vagrant_inventory.py b/vagrant_inventory.py index 43213fb..10dc22a 100755 --- a/vagrant_inventory.py +++ b/vagrant_inventory.py @@ -36,22 +36,9 @@ def main(): global MetaClass MetaClass = type - global _GROUP - _GROUP = 'vagrant' # a default group - - global _ssh_to_ansible - _ssh_to_ansible = [('user', 'ansible_user'), - ('hostname', 'ansible_host'), - ('identityfile', 'ansible_private_key_file'), - ('port', 'ansible_port')] - - parse_options() - -def parse_options(): """ Parse command line options """ - parser = argparse.ArgumentParser(description="") parser.add_argument('--list', default=False, dest="list", action="store_true", help="Produce a JSON consumable grouping of Vagrant servers for Ansible") @@ -62,6 +49,8 @@ def parse_options(): global mapping mapping = {} + _GROUP = 'vagrant' # a default group + if options.list: list_running_boxes() ssh_config = get_ssh_config() @@ -119,6 +108,11 @@ def get_a_ssh_config(box_id,box_name): Gives back a map of all the machine's ssh configurations """ + _ssh_to_ansible = [('user', 'ansible_user'), + ('hostname', 'ansible_host'), + ('identityfile', 'ansible_private_key_file'), + ('port', 'ansible_port')] + output = to_text(subprocess.check_output(["vagrant", "ssh-config", box_id])) config = SSHConfig() config.parse(StringIO(output))