def handle_a(subdomain, ip, domain): zone = dnszone.zone_from_file(domain, '/etc/bind/zones/db.mrt.' + domain) zone.add_name(subdomain + '.' + domain + '.') zone.names[subdomain + '.' + domain + '.'].records('A', create=True).add(ip) zone.save(autoserial=True)
def handle_cname(alias, host_domain): domain = '.'.join(host_domain.split('.')[1:-1]) + '.be' zone = dnszone.zone_from_file(domain, '/etc/bind/zones/db.mrt.' + domain) zone.add_name(alias + '.' + domain + '.') zone.names[alias + '.' + domain + '.'].records( 'CNAME', create=True).add(host_domain + '.') zone.save(autoserial=True)
def parse_zonefile(self): """Parse the zonefile for the domain. Returns: {bool} -- True when parsed """ logger.info("Domain name: %s" % self.get_domain_from_file()) logger.info("Filename to open: %s" % self.filename) self.domain = self.get_domain_from_file() self.zonefile = dnszone.zone_from_file(self.domain, self.filename) self.names = list(self.zonefile.names) return True
def run_module(): # define available arguments/parameters a user can pass to the module module_args = dict( name=dict(type='str', required=True), path=dict(type='str', required=True), ) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict(changed=False, zone=dict()) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule(argument_spec=module_args, supports_check_mode=True) # if the user is working with this module in only check mode we do not # want to make any changes to the environment, just return the current # state with no modifications if module.check_mode: module.exit_json(**result) zone = dnszone.zone_from_file(module.params['name'], module.params['path']) result['zone']['name'] = module.params['name'] result['zone']['data'] = dict() for name, name_value in zone.get_names().items(): result['zone']['data'][name] = [] for rtype in TF_ALLOWED_RECORD_TYPES: if name_value.records(rtype): result['zone']['data'][name].append({ 'type': rtype, 'ttl': name_value.ttl, 'value': name_value.records(rtype).items }) # during the execution of the module, if there is an exception or a # conditional state that effectively causes a failure, run # AnsibleModule.fail_json() to pass in the message and the result if module.params['name'] == 'fail me': module.fail_json(msg='You requested this to fail', **result) # in the event of a successful module execution, you will want to # simple AnsibleModule.exit_json(), passing the key/value results module.exit_json(**result)
def setUp(self): zone_file = os.path.join(os.path.dirname(__file__), 'files', 'example.com') self.zone = zone_from_file('example.com', zone_file)
def setUp(self): zone_file = os.path.join(os.path.dirname(__file__), "files", "example.com") self.zone = zone_from_file("example.com", zone_file)
def handle_mx(mx_name, ip, domain): zone = dnszone.zone_from_file(domain, '/etc/bind/zones/db.mrt.' + domain) zone.names[domain + '.'].records('MX', create=True).add((10, mx_name)) zone.save(autoserial=True)