From fb2871fccabff746831c7d1e403d613d29beed46 Mon Sep 17 00:00:00 2001 From: Jon Date: Mon, 19 Feb 2024 03:46:23 +0930 Subject: [PATCH] feat(agent): process scanned subnet using cached subnet details !1 --- playbooks/agent.yaml | 1 + playbooks/tasks/scan_subnet.yaml | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/playbooks/agent.yaml b/playbooks/agent.yaml index e7b0960..f1c1aaa 100644 --- a/playbooks/agent.yaml +++ b/playbooks/agent.yaml @@ -29,6 +29,7 @@ vars: # ToDo: remove the below t4est vars + api_address: addresses api_subnets: subnets api_scanagents: tools/scanagents nfc_c_path_cache: "{{ playbook_dir }}/../cache" diff --git a/playbooks/tasks/scan_subnet.yaml b/playbooks/tasks/scan_subnet.yaml index eac1305..bd3fe60 100644 --- a/playbooks/tasks/scan_subnet.yaml +++ b/playbooks/tasks/scan_subnet.yaml @@ -13,3 +13,35 @@ api_token: "{{ client_token }}" api_path: "{{ api_address }}" api_query_string: "filter_by=subnetId&filter_value={{ subnet.id }}" + + +- name: Load Subnet + ansible.builtin.set_fact: + cached_subnet: "{{ lookup('file', cache_filepath) }}" + cacheable: false + + +- name: Process Scan Results + ansible.builtin.set_fact: + subnet_scan_results: |- + [ + {% for scanned_host in ((nmap_scan.stdout | ansible.utils.from_xml) | from_yaml).nmaprun.host %} + {% if scanned_host.address[0]['@addrtype'] | default('') == 'ipv4' %} + { + {% for cached_host in cached_subnet -%} + {%- if cached_host.ip == scanned_host.address[0]['@addr'] -%} + "id": {{ cached_host.id }}, + {%- endif -%} + {%- endfor %} + "subnetId": "{{ subnet.id }}", + "ip": "{{ scanned_host.address[0]['@addr'] }}", + "lastSeen": "{{ nmap_scan.start }}", + "mac": "{{ scanned_host.address[1]['@addr'] | upper }}" + }, + {% endif %} + {% endfor %} + ] + +- name: To JSON + ansible.builtin.set_fact: + subnet_scan_results: "{{ subnet_scan_results | from_yaml }}"