diff --git a/galaxy.yml b/galaxy.yml index d4cd646..d99f7d2 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -2,7 +2,7 @@ namespace: 'niceshopsorg' name: 'ascio' -version: 1.0.2 +version: 1.0.3 readme: 'README.md' authors: - 'Rene Rath ' diff --git a/playbook_get.yml b/playbook_get.yml index acd3abe..bbc1cc7 100644 --- a/playbook_get.yml +++ b/playbook_get.yml @@ -1,5 +1,7 @@ --- +# NOTE: some custom 'filter_plugins' are used: https://github.com/niceshops/ansible-module-ascio/tree/main/plugins/filter + - name: ASCIO Get Information hosts: localhost vars_prompt: @@ -30,6 +32,9 @@ private: no default: "1" + vars: + remove_fields_from_results: ['PrivacyProxy', 'DnsSecKeys', 'Trademark', 'Reseller'] + tasks: - name: ASCIO | Get domain details if we registered it niceshopsOrg.ascio.get: @@ -44,23 +49,26 @@ # filter_expire_to: '2021-10-12T16:08:56.956+02:00' # results: 5000 results_page: "{{ ascio_get_page }}" - register: result + register: results delegate_to: localhost - ignore_errors: true + + - name: ASCIO | Filter resultes + ansible.builtin.set_fact: + filtered_results: "{{ results | ascio_filter_results(remove_fields_from_results) }}" - name: ASCIO | Output to Console ansible.builtin.debug: msg: [ "filter domains: {{ filter_names.split(',') }}", "filter tld's: {{ filter_tld }}", - "{{ result }}" + "{{ filtered_results }}" ] when: output_console | bool ignore_errors: true - name: ASCIO | Writing output to json-file ansible.builtin.copy: - content: "{{ result | filter_results | to_json }}" + content: "{{ filtered_results | to_json }}" dest: "/tmp/ascio_get_{{ filter_tld }}_{{ ansible_date_time.iso8601_basic }}.json" mode: 0640 when: output_file | bool @@ -68,6 +76,6 @@ - name: ASCIO | Writing output to csv-file ansible.builtin.debug: - msg: "{{ result | filter_results | write_domain_csv('/tmp/ascio_get_' + filter_tld + '_' + ansible_date_time.iso8601_basic + '.csv') }}" + msg: "{{ filtered_results | ascio_write_domain_csv('/tmp/ascio_get_' + filter_tld + '_' + ansible_date_time.iso8601_basic + '.csv') }}" when: output_file | bool ignore_errors: true diff --git a/plugins/filter/ascio_get.py b/plugins/filter/ascio_get.py new file mode 100644 index 0000000..c377d07 --- /dev/null +++ b/plugins/filter/ascio_get.py @@ -0,0 +1,60 @@ +from csv import DictWriter + + +class FilterModule(object): + + def filters(self): + return { + "ascio_filter_results": self.filter_results, + "ascio_write_domain_csv": self.write_domain_csv, + } + + @staticmethod + def filter_results(result: dict, remove_fields: list = None) -> dict: + # will only output domain and its nameservers (cleaned) + domains = {} + if remove_fields is None: + remove_fields = [] + + remove_fields.append('DomainName') + + if 'data' not in result: + return domains + + for domain_raw in result['data']['DomainInfo']: + domain_simple = {**domain_raw, 'NameServers': []} + for field in remove_fields: + try: + domain_simple.pop(field) + + except KeyError: + pass + + for ns in domain_raw['NameServers'].values(): + for ns_field in ['HostName', 'IpAddress', 'IpV6Address']: + if ns[ns_field] is not None: + domain_simple['NameServers'].append(ns[ns_field]) + + domains[domain_raw['DomainName']] = domain_simple + + return domains + + @staticmethod + def write_domain_csv(data: dict, file: str) -> bool: + data_list = [] + for domain, values in data.items(): + data_list.append({'DomainName': domain, **values}) + + try: + columns = list(data_list[0].keys()) + + with open(file, 'w', encoding='uft-8') as target: + writer = DictWriter(target, fieldnames=columns) + writer.writeheader() + for entry in data_list: + writer.writerow(entry) + + return True + + except IOError: + return False diff --git a/plugins/filter_plugins/ascio_get.py b/plugins/filter_plugins/ascio_get.py deleted file mode 100644 index a50c568..0000000 --- a/plugins/filter_plugins/ascio_get.py +++ /dev/null @@ -1,54 +0,0 @@ -from csv import DictWriter - - -class FilterModule(object): - - def filters(self): - return { - "filter_results": self.filter_results, - "write_domain_csv": self.write_domain_csv, - } - - @staticmethod - def filter_results(result: dict) -> dict: - # will only output domain and its nameservers (cleaned) - domains = {} - - if 'data' not in result: - return domains - - for domain in result['data']['DomainInfo']: - _namesservers = [] - - for ns in domain['NameServers'].values(): - if ns['HostName'] is not None: - _namesservers.append(ns['HostName']) - - domains[domain['DomainName']] = { - 'ns': _namesservers, - 'owner': f"{domain['Owner']['FirstName']} {domain['Owner']['LastName']} {domain['Owner']['OrgName']} {domain['Owner']['Email']}", - 'admin': f"{domain['Admin']['FirstName']} {domain['Admin']['LastName']} {domain['Admin']['OrgName']} {domain['Admin']['Email']}", - 'tech': f"{domain['Tech']['FirstName']} {domain['Tech']['LastName']} {domain['Tech']['OrgName']} {domain['Tech']['Email']}", - } - - return domains - - @staticmethod - def write_domain_csv(data: dict, file: str) -> bool: - columns = ['Domain', 'NameServers', 'Owner', 'Admin', 'Tech'] - - data_list = [] - for key, value in data.items(): - data_list.append({columns[0]: key, columns[1]: value['ns'], columns[2]: value['owner'], columns[3]: value['admin'], columns[4]: value['tech']}) - - try: - with open(file, 'w', encoding='uft-8') as target: - writer = DictWriter(target, fieldnames=columns) - writer.writeheader() - for entry in data_list: - writer.writerow(entry) - - return True - - except IOError: - return False