diff --git a/bundles/powerdns/items.py b/bundles/powerdns/items.py index ddb8751..04261c0 100644 --- a/bundles/powerdns/items.py +++ b/bundles/powerdns/items.py @@ -175,14 +175,27 @@ if node.metadata.get('powerdns/features/pgsql', node.has_bundle('postgresql')): for hostname, ips in node.metadata.get('powerdns/my_primary_servers', {}).items(): for ip in ips: ip_name = ip.replace(':', '-') - actions[f'powerdns_ensure_{hostname}_{ip_name}_in_autoprimaries'] = { - 'command': 'psql -c "INSERT INTO supermasters (ip, nameserver, account) VALUES ' - f'(\'{ip}\', \'{hostname}\', \'admin\') ON CONFLICT ON CONSTRAINT ' - f'supermasters_pkey DO UPDATE SET nameserver = \'{hostname}\'" powerdns', + + actions[f'powerdns_ensure_{ip_name}_in_autoprimaries'] = { + 'command': f'psql -c "INSERT INTO supermasters (ip, nameserver, account) VALUES (\'{ip}\', \'{hostname}\', \'admin\')" powerdns', + 'unless': f'test -n \"$(psql -tAqc "SELECT nameserver FROM supermasters WHERE ip = \'{ip}\'" powerdns)\"', + 'triggers': { + 'action:powerdns_fix_primaries', + }, + 'after': { + 'action:powerdns_load_pgsql_schema', + }, + } + + actions[f'powerdns_ensure_{hostname}_matches_{ip_name}_in_autoprimaries'] = { + 'command': f'psql -c "UPDATE supermasters SET nameserver = \'{hostname}\' WHERE ip = \'{ip}\'" powerdns', 'unless': f'bash -c "[ \"$(psql -tAqc "SELECT nameserver FROM supermasters WHERE ip = \'{ip}\'" powerdns)\" == \"{hostname}\" ]"', 'triggers': { 'action:powerdns_fix_primaries', }, + 'after': { + f'action:powerdns_ensure_{ip_name}_in_autoprimaries', + }, } actions['powerdns_fix_primaries'] = {