test0820

Young-Kyoo Kim·2025년 8월 20일
---
- name: Play 1: 각 노드의 H/W 사양 수집
  hosts: all  # 인벤토리의 모든 호스트를 대상으로 실행
  gather_facts: yes
  tasks:
    - name: 필요한 H/W 정보만 추출하여 'hw_info' 변수로 저장
      ansible.builtin.set_fact:
        hw_info:
          cpu_cores: "{{ ansible_processor_vcpus }}"
          memory_gb: "{{ (ansible_memtotal_mb / 1024) | round(0, 'ceil') }}GB"
          disks: >-
            {% set disk_list = [] %}
            {% for device, details in ansible_devices.items() %}
            {% if device.startswith(('sd', 'vd', 'hd', 'xvd', 'nvme')) and not device[-1].isdigit() and 'loop' not in device %}
            {%   set _ = disk_list.append(details.size) %}
            {% endif %}
            {% endfor %}
            {{ disk_list | sort }}
          unmounted_partitions: >-
            {% set unmounted_list = [] %}
            {% set mounted_devices = ansible_mounts | map(attribute='device') | list %}
            {% for device, details in ansible_devices.items() %}
            {% if (device[-1].isdigit() or 'p' in device) and details.partitions and '/dev/' + device not in mounted_devices %}
            {%   set _ = unmounted_list.append({'device': '/dev/' + device, 'size': details.size}) %}
            {% endif %}
            {% endfor %}
            {{ unmounted_list }}
      tags:
        - collect

- name: Play 2: 수집된 H/W 사양을 그룹화하여 요약 보고
  hosts: localhost
  gather_facts: no
  connection: local
  tasks:
    - name: 사양별로 호스트를 그룹화할 딕셔너리 초기화
      ansible.builtin.set_fact:
        grouped_specs: {}

    - name: 각 호스트의 H/W 정보를 기반으로 그룹화 진행
      ansible.builtin.set_fact:
        grouped_specs: "{{ grouped_specs | combine({ spec_key: grouped_specs.get(spec_key, []) + [item] }) }}"
      loop: "{{ query('inventory_hostnames', 'all') }}"
      vars:
        current_host_info: "{{ hostvars[item].hw_info }}"
        spec_key: >-
          CPU: {{ current_host_info.cpu_cores }} Cores |
          Memory: {{ current_host_info.memory_gb }} |
          Disks: {{ current_host_info.disks | join(', ') }}

    - name: 최종 요약 결과 출력
      ansible.builtin.debug:
        msg: |
          ================================================================================
          H/W 사양 그룹: {{ item.key }}
          (총 {{ item.value | length }} 대)
          --------------------------------------------------------------------------------
          {% for host in item.value %}
          - 호스트: {{ hostvars[host].inventory_hostname }}
            {% set unmounted = hostvars[host].hw_info.unmounted_partitions %}
            마운트되지 않은 파티션: {% if unmounted %}{{ unmounted | map(attribute='device') | join(', ') }} (사이즈: {{ unmounted | map(attribute='size') | join(', ') }}){% else %}없음{% endif %}
          {% endfor %}
      loop: "{{ grouped_specs | dict2items }}"
      loop_control:
        label: "H/W 사양 그룹 요약"

---
- name: Play 1: 각 노드의 H/W 사양 수집
  hosts: all
  gather_facts: yes
  tasks:
    - name: 필요한 H/W 정보만 추출하여 'hw_info' 변수로 저장
      ansible.builtin.set_fact:
        hw_info:
          cpu_cores: "{{ ansible_processor_vcpus }}"
          memory_gb: "{{ (ansible_memtotal_mb / 1024) | round(0, 'ceil') }}GB"
          disks: >-
            {{
              ansible_devices.values()
              | selectattr('type', 'equalto', 'disk')
              | map(attribute='size')
              | sort
            }}
          unmounted_partitions: >-
            {{
              (
                {%- set unmounted_list = [] -%}
                {%- set mounted_devices = ansible_mounts | map(attribute='device') | list -%}
                {%- for device, details in ansible_devices.items() -%}
                {%- if details.type == 'partition' and '/dev/' + device not in mounted_devices -%}
                {%-   set _ = unmounted_list.append({'device': '/dev/' + device, 'size': details.size}) -%}
                {%- endif -%}
                {%- endfor -%}
                {{ unmounted_list }}
              ) | from_yaml
            }}
      tags:
        - collect

- name: Play 2: 수집된 H/W 사양을 그룹화하여 요약 보고
  hosts: localhost
  gather_facts: no
  connection: local
  tasks:
    - name: 사양별로 호스트를 그룹화할 딕셔너리 초기화
      ansible.builtin.set_fact:
        grouped_specs: {}

    - name: 각 호스트의 H/W 정보를 기반으로 그룹화 진행
      ansible.builtin.set_fact:
        grouped_specs: "{{ grouped_specs | combine({ spec_key: grouped_specs.get(spec_key, []) + [item] }) }}"
      loop: "{{ query('inventory_hostnames', 'all') }}"
      vars:
        current_host_info: "{{ hostvars[item].hw_info }}"
        spec_key: >-
          CPU: {{ current_host_info.cpu_cores }} Cores |
          Memory: {{ current_host_info.memory_gb }} |
          Disks: {{ current_host_info.disks | join(', ') }}

    - name: 최종 요약 결과 출력
      ansible.builtin.debug:
        msg: |
          ================================================================================
          H/W 사양 그룹: {{ item.key }}
          (총 {{ item.value | length }} 대)
          --------------------------------------------------------------------------------
          {% for host in item.value %}
          - 호스트: {{ hostvars[host].inventory_hostname }}
            {% set unmounted = hostvars[host].hw_info.unmounted_partitions %}
            마운트되지 않은 파티션: {% if unmounted %}{{ unmounted | map(attribute='device') | join(', ') }} (사이즈: {{ unmounted | map(attribute='size') | join(', ') }}){% else %}없음{% endif %}
          {% endfor %}
      loop: "{{ grouped_specs | dict2items }}"
      loop_control:
        label: "H/W 사양 그룹 요약"

---
- name: Play 1: 각 노드의 H/W 사양 수집
  hosts: all
  gather_facts: yes
  tasks:
    - name: 필요한 H/W 정보만 추출하여 'hw_info' 변수로 저장
      ansible.builtin.set_fact:
        hw_info:
          cpu_cores: "{{ ansible_processor_vcpus }}"
          memory_gb: "{{ (ansible_memtotal_mb / 1024) | round(0, 'ceil') }}GB"
          disks: >-
            {{
              (
                {%- set disk_list = [] -%}
                {%- for device, details in ansible_devices.items() -%}
                {%- if device.startswith(('sd', 'vd', 'hd', 'xvd', 'nvme')) and not device[-1].isdigit() and 'loop' not in device and 'dm-' not in device -%}
                {%-   set _ = disk_list.append(details.size) -%}
                {%- endif -%}
                {%- endfor -%}
                {{ disk_list | sort }}
              ) | from_yaml
            }}
          unmounted_partitions: >-
            {{
              (
                {%- set unmounted_list = [] -%}
                {%- set mounted_devices = ansible_mounts | map(attribute='device') | list -%}
                {%- for device, details in ansible_devices.items() -%}
                {%- if details.get('type') == 'partition' and '/dev/' + device not in mounted_devices -%}
                {%-   set _ = unmounted_list.append({'device': '/dev/' + device, 'size': details.size}) -%}
                {%- endif -%}
                {%- endfor -%}
                {{ unmounted_list }}
              ) | from_yaml
            }}
      tags:
        - collect

- name: Play 2: 수집된 H/W 사양을 그룹화하여 요약 보고
  hosts: localhost
  gather_facts: no
  connection: local
  tasks:
    - name: 사양별로 호스트를 그룹화할 딕셔너리 초기화
      ansible.builtin.set_fact:
        grouped_specs: {}

    - name: 각 호스트의 H/W 정보를 기반으로 그룹화 진행
      ansible.builtin.set_fact:
        grouped_specs: "{{ grouped_specs | combine({ spec_key: grouped_specs.get(spec_key, []) + [item] }) }}"
      loop: "{{ query('inventory_hostnames', 'all') }}"
      vars:
        current_host_info: "{{ hostvars[item].hw_info }}"
        spec_key: >-
          CPU: {{ current_host_info.cpu_cores }} Cores |
          Memory: {{ current_host_info.memory_gb }} |
          Disks: {{ current_host_info.disks | join(', ') }}

    - name: 최종 요약 결과 출력
      ansible.builtin.debug:
        msg: |
          ================================================================================
          H/W 사양 그룹: {{ item.key }}
          (총 {{ item.value | length }} 대)
          --------------------------------------------------------------------------------
          {% for host in item.value %}
          - 호스트: {{ hostvars[host].inventory_hostname }}
            {% set unmounted = hostvars[host].hw_info.unmounted_partitions %}
            마운트되지 않은 파티션: {% if unmounted %}{{ unmounted | map(attribute='device') | join(', ') }} (사이즈: {{ unmounted | map(attribute='size') | join(', ') }}){% else %}없음{% endif %}
          {% endfor %}
      loop: "{{ grouped_specs | dict2items }}"
      loop_control:
        label: "H/W 사양 그룹 요약"


0개의 댓글