From 4a0f0fa6680303f2b246b0f5a01427bbffc1e18c Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Mon, 18 Jun 2018 13:09:56 +0300 Subject: Fix: inventory file writing --- hetznertool | 62 ++++++++++++++++++++++++++++++------------------------------- 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/hetznertool b/hetznertool index ed1fe5c..806bbf5 100755 --- a/hetznertool +++ b/hetznertool @@ -124,7 +124,7 @@ def create_func(config, args): domain = get_domain_for_context(config, context) style = get_style_for_context(config, context) update_zone_file(style, domain, kick, args, zonedir, zonefile) - write_inventory_files(config, context) + write_inventory_files(config) def list_func(config, args): @@ -173,7 +173,6 @@ def get_style_for_context(config, context): def get_inventory_style_for_context(config, context): - print('context:', repr(context)) cc = get_config_for_context(config, context) return cc.get('inventory-style') @@ -196,7 +195,7 @@ def delete_func(config, args): kick = get_kick_for_context(config, context) style = get_style_for_context(config, context) update_zone_file(style, domain, kick, args, zonedir, zonefile) - write_inventory_files(config, context) + write_inventory_files(config) def update_zone_file(style, domain, kick, args, dirname, basename): @@ -261,41 +260,40 @@ def write_dnsapi_zone(stream, domain): context, info['name'], domain, info['ipv4'])) -def write_inventory_files(config, context): - style = get_inventory_style_for_context(config, context) - if style == 'ipv4': - write_inventory_files_with_ipv4(config) - elif style == 'dns': - write_inventory_files_with_dns_names(config, context) - else: - assert 0 +def write_inventory_files(config): + for context in list_contexts(): + style = get_inventory_style_for_context(config, context) + if style == 'ipv4': + write_inventory_files_with_ipv4(config, context) + elif style == 'dns': + write_inventory_files_with_dns_names(config, context) + else: + assert 0 -def write_inventory_files_with_ipv4(config): - for context in list_contexts(): - use_context(context) - dirname = get_inventorydir_for_context(config, context) - filename = os.path.join(dirname, 'hosts.{}'.format(context)) - print('Writing Ansible inventory file {}'.format(filename)) - with open(filename, 'w') as f: - for info in list_servers(): - f.write( - '{} ansible_ssh_host={}\n'.format( - info['name'], info['ipv4'])) +def write_inventory_files_with_ipv4(config, context): + use_context(context) + dirname = get_inventorydir_for_context(config, context) + filename = os.path.join(dirname, 'hosts.{}'.format(context)) + print('Writing Ansible inventory file {}'.format(filename)) + with open(filename, 'w') as f: + for info in list_servers(): + f.write( + '{} ansible_ssh_host={}\n'.format( + info['name'], info['ipv4'])) def write_inventory_files_with_dns_names(config, context): domain = get_domain_for_context(config, context) - for context in list_contexts(): - use_context(context) - dirname = get_inventorydir_for_context(config, context) - filename = os.path.join(dirname, 'hosts.{}'.format(context)) - print('Writing Ansible inventory file {}'.format(filename)) - with open(filename, 'w') as f: - for info in list_servers(): - name = dns_name(context, info['name'], domain) - f.write( - '{} ansible_ssh_host={}\n'.format(info['name'], name)) + use_context(context) + dirname = get_inventorydir_for_context(config, context) + filename = os.path.join(dirname, 'hosts.{}'.format(context)) + print('Writing Ansible inventory file {}'.format(filename)) + with open(filename, 'w') as f: + for info in list_servers(): + name = dns_name(context, info['name'], domain) + f.write( + '{} ansible_ssh_host={}\n'.format(info['name'], name)) def kick_bind9(ssh_target, filename, basename): -- cgit v1.2.1