Обновить playbooks/scan_inventory.yml

This commit is contained in:
2025-12-10 11:15:13 +00:00
parent 13969c9425
commit d6987ac18f

View File

@@ -3,7 +3,7 @@
hosts: localhost hosts: localhost
connection: local connection: local
gather_facts: no gather_facts: no
# become: yes <--- УБРАЛИ ОТСЮДА (Глобальный sudo вызывает ошибку прав) # become: yes <--- УБРАНО, ЭТО ВЫЗЫВАЛО ОШИБКУ
vars: vars:
# --- SEMAPHORE API --- # --- SEMAPHORE API ---
semaphore_url: "http://192.168.0.198:9999" semaphore_url: "http://192.168.0.198:9999"
@@ -37,11 +37,19 @@
tasks: tasks:
# ---------------------------------------------------------------- # ----------------------------------------------------------------
# ШАГ 1: Поиск живых хостов # ПРЕДВАРИТЕЛЬНЫЙ ШАГ: Проверка sudo
# ----------------------------------------------------------------
- name: Убедиться, что sudo установлен (для Alpine)
shell: "apk add --no-cache sudo || apt-get update && apt-get install -y sudo || true"
ignore_errors: yes
# Эта команда попытается поставить sudo, если вы root. Если нет - пропустит.
# ----------------------------------------------------------------
# ШАГ 1: Поиск живых хостов (Ping)
# ---------------------------------------------------------------- # ----------------------------------------------------------------
- name: Ping Sweep (быстрый поиск) - name: Ping Sweep (быстрый поиск)
command: "nmap -sn -n --min-rate 1000 -T4 -oG - {{ subnets | join(' ') }}" # ИСПОЛЬЗУЕМ sudo ПРЯМО В КОМАНДЕ
become: yes # <--- ДОБАВИЛИ СЮДА (Точечно) command: "sudo nmap -sn -n --min-rate 1000 -T4 -oG - {{ subnets | join(' ') }}"
register: ping_scan register: ping_scan
changed_when: false changed_when: false
@@ -55,15 +63,15 @@
when: active_ips | length == 0 when: active_ips | length == 0
# ---------------------------------------------------------------- # ----------------------------------------------------------------
# ШАГ 2: Опрос через SNMP # ШАГ 2: Опрос через SNMP + Проверка портов
# ---------------------------------------------------------------- # ----------------------------------------------------------------
- name: SNMP Discovery - name: SNMP Discovery
# ИСПОЛЬЗУЕМ sudo ПРЯМО В КОМАНДЕ
shell: | shell: |
nmap -sU -sS -p U:161,T:22,T:445,T:8291,T:9100 \ sudo nmap -sU -sS -p U:161,T:22,T:445,T:8291,T:9100 \
--script snmp-sysdescr \ --script snmp-sysdescr \
--script-args snmpcommunity={{ snmp_community }} \ --script-args snmpcommunity={{ snmp_community }} \
-Pn -n -T4 {{ item }} -Pn -n -T4 {{ item }}
become: yes # <--- ДОБАВИЛИ СЮДА (Точечно, т.к. UDP скан требует root)
loop: "{{ active_ips }}" loop: "{{ active_ips }}"
register: scan_results register: scan_results
changed_when: false changed_when: false
@@ -78,12 +86,14 @@
out: "{{ item.stdout }}" out: "{{ item.stdout }}"
ip: "{{ item.item }}" ip: "{{ item.item }}"
snmp_desc: "{{ out | regex_search('sysDescr\\.0: ([^\\n]+)', '\\1') | first | default('') }}" snmp_desc: "{{ out | regex_search('sysDescr\\.0: ([^\\n]+)', '\\1') | first | default('') }}"
detected_type: >- detected_type: >-
{% if 'Windows' in snmp_desc or '445/tcp open' in out %}windows {% if 'Windows' in snmp_desc or '445/tcp open' in out %}windows
{% elif 'RouterOS' in snmp_desc or 'MikroTik' in snmp_desc or '8291/tcp open' in out %}mikrotik {% elif 'RouterOS' in snmp_desc or 'MikroTik' in snmp_desc or '8291/tcp open' in out %}mikrotik
{% elif 'Linux' in snmp_desc or 'Ubuntu' in snmp_desc or '22/tcp open' in out %}linux {% elif 'Linux' in snmp_desc or 'Ubuntu' in snmp_desc or '22/tcp open' in out %}linux
{% elif 'JetDirect' in snmp_desc or 'LaserJet' in snmp_desc or 'Samsung' in snmp_desc or 'Kyocera' in snmp_desc or '9100/tcp open' in out %}printer {% elif 'JetDirect' in snmp_desc or 'LaserJet' in snmp_desc or 'Samsung' in snmp_desc or 'Kyocera' in snmp_desc or '9100/tcp open' in out %}printer
{% else %}other{% endif %} {% else %}other{% endif %}
host_data: host_data:
ip: "{{ ip }}" ip: "{{ ip }}"
type: "{{ detected_type }}" type: "{{ detected_type }}"