diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index f24afbb..0d759ff 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -29,6 +29,11 @@ include: - template/mkdocs-documentation.gitlab-ci.yaml # ToDo: update gitlabCI jobs for collections workflow - git_push_mirror/.gitlab-ci.yml + - automation/.gitlab-ci-ansible.yaml + + +Update Git Submodules: + extends: .ansible_playbook_git_submodule Github (Push --mirror): diff --git a/dockerfile b/dockerfile index db63184..43a8cb6 100644 --- a/dockerfile +++ b/dockerfile @@ -94,7 +94,7 @@ RUN mkdir -p /tmp/collection; \ if [ "$COLLECTION_PACKAGE" != "dev" ]; then \ echo "specified"; \ ansible-galaxy collection install --force-with-deps --pre \ - $COLLECTION; \ + $COLLECTION_PACKAGE; \ elif [ "$COLLECTION_PACKAGE" == "dev" ]; then \ git clone \ --depth=1 \ diff --git a/gitlab-ci b/gitlab-ci index d29064f..34c81c9 160000 --- a/gitlab-ci +++ b/gitlab-ci @@ -1 +1 @@ -Subproject commit d29064f1490073599518b629c7bf6585b48c8736 +Subproject commit 34c81c98494b5ce448f4da4e645952439c897906 diff --git a/playbooks/agent.yaml b/playbooks/agent.yaml index 1307424..7c33f2c 100644 --- a/playbooks/agent.yaml +++ b/playbooks/agent.yaml @@ -84,7 +84,7 @@ - name: Scan Subnet ansible.builtin.include_tasks: file: tasks/scan_subnet.yaml - loop: "{{ nfc_c_scan_agent_subnets }}" + loop: "{{ nfc_c_scan_agent_subnets | default([]) }}" loop_control: loop_var: subnet diff --git a/playbooks/tasks/api_call.yaml b/playbooks/tasks/api_call.yaml index 99e5cac..3a537b8 100644 --- a/playbooks/tasks/api_call.yaml +++ b/playbooks/tasks/api_call.yaml @@ -26,34 +26,34 @@ - name: check Cache Files ansible.builtin.stat: path: "{{ cache_filepath }}" - register: cache_files + register: cached_file - name: Expire ansible.builtin.set_fact: - expired: "{{ ((epoch | int + (nfc_c_epoch_time_offset | default(0)) | int) >= ((cache_files.stat.mtime | int) + nfc_c_cache_expire_time | int) | int ) | bool }}" - when: cache_files.stat.exists + expired: "{{ ((epoch | int + (nfc_c_epoch_time_offset | default(0)) | int) >= ((cached_file.stat.mtime | int) + nfc_c_cache_expire_time | int) | int ) | bool }}" + when: cached_file.stat.exists - name: TRACE - Cached file ansible.builtin.debug: msg: - - "exists: {{ cache_files.stat.exists | default('') }}" - - "mtime: {{ cache_files.stat.mtime | default(0) | int }}" - - "expire: {{ (cache_files.stat.mtime | int) + nfc_c_cache_expire_time | int }}" + - "exists: {{ cached_file.stat.exists | default('') }}" + - "mtime: {{ cached_file.stat.mtime | default(0) | int }}" + - "expire: {{ (cached_file.stat.mtime | int) + nfc_c_cache_expire_time | int }}" - "epoch: {{ (epoch | int + (nfc_c_epoch_time_offset | default(0)) | int) | int }} [{{ nfc_c_cache_expire_time }}]" - "epoch: {{ epoch }}" - "expired: {{ expired }}" - when: cache_files.stat.exists + when: cached_file.stat.exists - name: Expire Cache ansible.builtin.file: - path: "{{ cache_files.stat.path }}" + path: "{{ cached_file.stat.path }}" state: absent when: > expired and - cache_files.stat.exists + cached_file.stat.exists - name: > @@ -73,17 +73,17 @@ - 200 - 404 validate_certs: false - changed_when: api_call.json | length | int > 0 + changed_when: api_call.json | default([]) | length | int > 0 no_log: true register: api_call when: > ( expired and - cache_files.stat.exists + cached_file.stat.exists ) or - not cache_files.stat.exists + not cached_file.stat.exists - name: Create Cache DIR @@ -104,9 +104,14 @@ ( expired and - cache_files.stat.exists + cached_file.stat.exists ) or - not cache_files.stat.exists + not cached_file.stat.exists and api_call.status | default(0) | int != 404 + + - name: check Cache Files + ansible.builtin.stat: + path: "{{ cache_filepath }}" + register: cached_file diff --git a/playbooks/tasks/scan_subnet.yaml b/playbooks/tasks/scan_subnet.yaml index 5847c43..25ebcd8 100644 --- a/playbooks/tasks/scan_subnet.yaml +++ b/playbooks/tasks/scan_subnet.yaml @@ -1,6 +1,6 @@ --- -- name: Scan subnet +- name: Scan subnet - {{ subnet.address }} ansible.builtin.command: cmd: nmap -sn "{{ subnet.address }}" -oX - become: true @@ -15,7 +15,7 @@ api_query_string: "filter_by=subnetId&filter_value={{ subnet.id }}" -- name: Load Subnet +- name: Load Subnet - {{ subnet.address }} ansible.builtin.set_fact: cached_subnet: "{{ lookup('file', cache_filepath) }}" cacheable: false @@ -24,7 +24,7 @@ api_call.status | default(0) | int != 404 -- name: Process Scan Results +- name: Process Scan Results - {{ subnet.address }} ansible.builtin.set_fact: subnet_scan_results: |- [ @@ -43,6 +43,13 @@ "subnetId": "{{ subnet.id }}", "ip": "{{ scanned_host.address['@addr'] | default(scanned_host.address[0]['@addr']) }}", "lastSeen": "{{ nmap_scan.start }}", + {% if scanned_host.hostnames.hostname is defined %} + {% if '.' in scanned_host.hostnames.hostname['@name'] | string %} + "hostname": "{{ (scanned_host.hostnames.hostname['@name'] | split('.'))[0] }}", + {% else %} + "hostname": "{{ scanned_host.hostnames.hostname['@name'] }}", + {% endif %} + {% endif %} {% if scanned_host.address['@addrtype'] | default(scanned_host.address[1]['@addrtype']) == 'mac' %} "mac": "{{ scanned_host.address['@addr'] | default(scanned_host.address[1]['@addr']) | upper }}" {% endif %} @@ -51,12 +58,12 @@ {% endfor %} ] -- name: To JSON +- name: To JSON - {{ subnet.address }} ansible.builtin.set_fact: subnet_scan_results: "{{ subnet_scan_results | from_yaml }}" -- name: Upload Scan Results +- name: Upload Scan Results - {{ subnet.address }} ansible.builtin.uri: url: "{{ nfc_c_http_server }}:{{ nfc_c_http_port }}/" method: POST diff --git a/playbooks/tasks/server/ipaddress.yaml b/playbooks/tasks/server/ipaddress.yaml index 52ceb04..633ae0c 100644 --- a/playbooks/tasks/server/ipaddress.yaml +++ b/playbooks/tasks/server/ipaddress.yaml @@ -1,7 +1,7 @@ --- -- name: Update IP Address' found +- name: "Update IP Address' found - {{ scan_address.ipaddress.ip }}" community.mysql.mysql_query: login_host: "{{ nfc_c_mysql_host }}" login_port: "{{ nfc_c_mysql_port | default(3306) | int }}" @@ -18,6 +18,12 @@ SET lastSeen = '{{ scan_address.ipaddress.lastSeen }}' + {% if scan_address.ipaddress.hostname | default('') != '' %}, + + hostname = '{{ scan_address.ipaddress.hostname }}' + + {% endif %} + {% if scan_address.ipaddress.mac | default('') != '' %}, mac = '{{ scan_address.ipaddress.mac }}' @@ -36,6 +42,7 @@ subnetId, ip_addr, description, + {% if scan_address.ipaddress.hostname | default('') != '' %}hostname,{% endif %} {% if scan_address.ipaddress.mac | default('') != '' %}mac,{% endif %} note, lastSeen @@ -46,6 +53,12 @@ '{{ scan_address.ipaddress.ip | ip2ipam }}', '-- autodiscovered --', + {% if scan_address.ipaddress.hostname | default('') != '' %} + + '{{ scan_address.ipaddress.hostname }}', + + {% endif %} + {% if scan_address.ipaddress.mac | default('') != '' %} '{{ scan_address.ipaddress.mac }}', diff --git a/playbooks/tasks/subnets.yaml b/playbooks/tasks/subnets.yaml index b907daf..923632e 100644 --- a/playbooks/tasks/subnets.yaml +++ b/playbooks/tasks/subnets.yaml @@ -15,15 +15,27 @@ api_query_string: "filter_by=scanAgent&filter_value={{ nfc_c_scan_agent_id }}" -- name: Update Subnets List - ansible.builtin.set_fact: - nfc_c_scan_agent_subnets: "{{ nfc_c_scan_agent_subnets + [{ - 'id': network.id, - 'address': network.subnet + '/' + network.mask - }] }}" - loop: "{{ data | default ([]) }}" - loop_control: - loop_var: network - vars: - data: "{{ lookup('file', cache_filepath) }}" - when: network.discoverSubnet | int == 1 +- name: Try/Catch + block: + + + - name: Update Subnets List + ansible.builtin.set_fact: + nfc_c_scan_agent_subnets: "{{ nfc_c_scan_agent_subnets + [{ + 'id': network.id, + 'address': network.subnet + '/' + network.mask + }] }}" + loop: "{{ lookup('file', cache_filepath) | default ([]) }}" + loop_control: + loop_var: network + when: > + network.discoverSubnet | int == 1 + + rescue: + + - name: Confirm 'Subnets List' Error is Expected + ansible.builtin.assert: + that: + - not cached_file.stat.exists + success_msg: "OK. Success. The error occured as there is no cache file. This normally means there are no subnets assigned to the agent." + fail_msg: "ERROR. Something went wrong, Cache file exists."