Обновить 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
connection: local
gather_facts: no
# become: yes <--- УБРАЛИ ОТСЮДА (Глобальный sudo вызывает ошибку прав)
# become: yes <--- УБРАНО, ЭТО ВЫЗЫВАЛО ОШИБКУ
vars:
# --- SEMAPHORE API ---
semaphore_url: "http://192.168.0.198:9999"
@@ -37,11 +37,19 @@
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 (быстрый поиск)
command: "nmap -sn -n --min-rate 1000 -T4 -oG - {{ subnets | join(' ') }}"
become: yes # <--- ДОБАВИЛИ СЮДА (Точечно)
# ИСПОЛЬЗУЕМ sudo ПРЯМО В КОМАНДЕ
command: "sudo nmap -sn -n --min-rate 1000 -T4 -oG - {{ subnets | join(' ') }}"
register: ping_scan
changed_when: false
@@ -55,15 +63,15 @@
when: active_ips | length == 0
# ----------------------------------------------------------------
# ШАГ 2: Опрос через SNMP
# ШАГ 2: Опрос через SNMP + Проверка портов
# ----------------------------------------------------------------
- name: SNMP Discovery
# ИСПОЛЬЗУЕМ sudo ПРЯМО В КОМАНДЕ
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-args snmpcommunity={{ snmp_community }} \
-Pn -n -T4 {{ item }}
become: yes # <--- ДОБАВИЛИ СЮДА (Точечно, т.к. UDP скан требует root)
loop: "{{ active_ips }}"
register: scan_results
changed_when: false
@@ -78,12 +86,14 @@
out: "{{ item.stdout }}"
ip: "{{ item.item }}"
snmp_desc: "{{ out | regex_search('sysDescr\\.0: ([^\\n]+)', '\\1') | first | default('') }}"
detected_type: >-
{% 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 '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
{% else %}other{% endif %}
host_data:
ip: "{{ ip }}"
type: "{{ detected_type }}"