test0820g4

Young-Kyoo Kim·2025년 8월 20일
---
- name: Simple Clear Service Status Check
  hosts: all
  gather_facts: yes
  vars:
    results: []
  
  tasks:
    - name: Check auditd service file
      stat:
        path: /usr/lib/systemd/system/auditd.service
      register: auditd_file
      
    - name: Check auditd status if exists
      shell: systemctl is-active auditd 2>/dev/null || echo 'inactive'
      register: auditd_active
      when: auditd_file.stat.exists
      
    - name: Check mlocate package
      shell: rpm -q mlocate >/dev/null 2>&1 && echo 'installed' || echo 'not_installed'
      register: mlocate_pkg
      
    - name: Check mlocate timer if package exists
      shell: |
        if systemctl list-unit-files | grep -q mlocate-updatedb.timer; then
          systemctl is-active mlocate-updatedb.timer 2>/dev/null || echo 'inactive'
        elif systemctl list-unit-files | grep -q updatedb.timer; then
          systemctl is-active updatedb.timer 2>/dev/null || echo 'inactive'
        else
          echo 'no_timer'
        fi
      register: mlocate_timer
      when: mlocate_pkg.stdout == 'installed'
      
    - name: Check locate database
      shell: |
        if [ -f /var/lib/mlocate/mlocate.db ]; then
          echo 'exists'
        elif [ -f /var/lib/locate/locatedb ]; then
          echo 'exists'
        else
          echo 'not_found'
        fi
      register: locate_db
      
    - name: Build result for this host
      set_fact:
        host_result:
          hostname: "{{ inventory_hostname }}"
          os: "{{ ansible_distribution }} {{ ansible_distribution_version }}"
          auditd_installed: "{{ auditd_file.stat.exists }}"
          auditd_running: "{{ (auditd_active.stdout == 'active') if auditd_file.stat.exists else false }}"
          auditd_status: "{{ auditd_active.stdout if auditd_file.stat.exists else 'not_installed' }}"
          mlocate_installed: "{{ mlocate_pkg.stdout == 'installed' }}"
          mlocate_timer_ok: "{{ (mlocate_timer.stdout == 'active') if (mlocate_pkg.stdout == 'installed' and mlocate_timer is defined) else false }}"
          mlocate_timer_status: "{{ mlocate_timer.stdout if (mlocate_pkg.stdout == 'installed' and mlocate_timer is defined) else 'not_checked' }}"
          locate_db_exists: "{{ locate_db.stdout == 'exists' }}"
          
    - name: Add to results
      set_fact:
        results: "{{ results + [host_result] }}"
      delegate_to: localhost
      delegate_facts: true
      
    - name: Display host status
      debug:
        msg: |
          
          ========================================
          HOST: {{ inventory_hostname }}
          ========================================
          
          🔍 AUDITD:
          {% if host_result.auditd_installed %}
          ✅ Status: INSTALLED - {{ '🟢 RUNNING' if host_result.auditd_running else '🔴 ' + host_result.auditd_status.upper() }}
          {% else %}
          ❌ Status: NOT INSTALLED
          {% endif %}
          
          📍 MLOCATE:
          {% if host_result.mlocate_installed %}
          ✅ Package: INSTALLED
          ├─ Timer: {{ '🟢 ACTIVE' if host_result.mlocate_timer_ok else '🔴 ' + host_result.mlocate_timer_status.upper() }}
          └─ Database: {{ '✅ EXISTS' if host_result.locate_db_exists else '❌ MISSING' }}
          {% else %}
          ❌ Package: NOT INSTALLED
          {% endif %}
          
          🎯 SUMMARY: 
          auditd={{ 'OK' if host_result.auditd_running else ('INSTALLED' if host_result.auditd_installed else 'MISSING') }} | 
          mlocate={{ 'OK' if (host_result.mlocate_installed and host_result.mlocate_timer_ok and host_result.locate_db_exists) else ('PARTIAL' if host_result.mlocate_installed else 'MISSING') }}

- name: Summary Report
  hosts: localhost
  gather_facts: no
  
  tasks:
    - name: Count statistics
      set_fact:
        total: "{{ results | length }}"
        auditd_ok: "{{ results | selectattr('auditd_running', 'equalto', true) | list | length }}"
        auditd_installed: "{{ results | selectattr('auditd_installed', 'equalto', true) | list | length }}"
        mlocate_installed: "{{ results | selectattr('mlocate_installed', 'equalto', true) | list | length }}"
        
    - name: Count mlocate functional
      set_fact:
        mlocate_ok: 0
        
    - name: Calculate mlocate functional nodes
      set_fact:
        mlocate_ok: "{{ mlocate_ok | int + 1 }}"
      loop: "{{ results }}"
      when: item.mlocate_installed and item.mlocate_timer_ok and item.locate_db_exists
      
    - name: Display summary
      debug:
        msg: |
          
          ================================================
          📊 FINAL SUMMARY
          ================================================
          
          Total Nodes: {{ total }}
          
          🔍 AUDITD:
          ├─ Fully Working: {{ auditd_ok }}/{{ total }}
          ├─ Installed Only: {{ auditd_installed | int - auditd_ok | int }}/{{ total }}
          └─ Missing: {{ total | int - auditd_installed | int }}/{{ total }}
          
          📍 MLOCATE:
          ├─ Fully Working: {{ mlocate_ok }}/{{ total }}
          ├─ Installed Only: {{ mlocate_installed | int - mlocate_ok | int }}/{{ total }}
          └─ Missing: {{ total | int - mlocate_installed | int }}/{{ total }}
          
          {% if auditd_ok | int < total | int %}
          ⚠️  AUDITD ISSUES:
          {% for node in results %}
          {% if not node.auditd_running %}
          - {{ node.hostname }}: {{ 'NOT INSTALLED' if not node.auditd_installed else node.auditd_status.upper() }}
          {% endif %}
          {% endfor %}
          {% endif %}
          
    - name: Generate simple report
      copy:
        content: |
          Hostname,OS,auditd_Status,mlocate_Status,Overall_Status
          {% for node in results -%}
          {{ node.hostname }},{{ node.os }},{{ 'OK' if node.auditd_running else ('INSTALLED' if node.auditd_installed else 'MISSING') }},{{ 'OK' if (node.mlocate_installed and node.mlocate_timer_ok and node.locate_db_exists) else ('PARTIAL' if node.mlocate_installed else 'MISSING') }},{{ 'OK' if (node.auditd_running and node.mlocate_installed and node.mlocate_timer_ok and node.locate_db_exists) else 'ISSUES' }}
          {% endfor %}
        dest: "./simple_service_report.csv"
        
    - name: Show completion
      debug:
        msg: |
          
          ✅ Simple clear check completed!
          📄 Report saved: ./simple_service_report.csv

0개의 댓글