
이 input.csv가 있습니다.
Server, Check-Server, Firewall-Port,
Server1,Server2,80,
,Server2,9999,
,server8,8443,
Server2,,,
server3,Server6,8443,
,Server9,8888,
server.yml을 생성하겠습니다.
---
Server1:
name: Server2
- 80
- 9999
name: server8
- 8443
Server2:
server3:
name: Server6
- 8443
name: Server9
- 8888
아래에 ansible을 만들었지만 다른 서버 포트에 대한 서버를 테스트하기 위해 이를 필터링하는 방법을 모르겠습니다. 이 검사는 모든 서버에 적용됩니다.
---
- name: Include file
include_vars: server.yml
- name: Check if remote host port
wait_for: host={{ item.name }} port={{ item.port }} timeout=1
ignore_errors: True
register: out
with_items: "{{ servers }}"
- debug: var=out
- name: Save remote port
shell: printf "\n,{{ item.item.name }} port {{ item.item.port }} is {% if item.failed %}closed{% else %}open{% endif %}" >> /tmp/{{ ansible_hostname }}_output.csv
args:
executable: /bin/bash
with_items: "{{ out.results }}"
- name: delete local /tmp/fetched
shell: rm -fr /tmp/fetched/
args:
executable: /bin/bash
warn: False
ignore_errors: True
delegate_to: localhost
- name: Fetching file
fetch:
src: /tmp/{{ ansible_hostname }}_output.csv
dest: /tmp/fetched
- name: Run once on localhost
shell: echo 'Hostname,IP,CPU-Core,Ram-GB,Disk-GB' > /tmp/fetched/main_output.csv
run_once: True
delegate_to: localhost
- name: Combined to one file
shell: cat /tmp/fetched/{{ ansible_hostname }}/tmp/{{ ansible_hostname }}_output.csv >> /tmp/fetched/main_output.csv && printf '\r\n' >> /tmp/fetched/main_output.csv
args:
executable: /bin/bash
delegate_to: localhost
도와주세요!
답변1
server.yml은 다음과 같아야 합니다.
서버: - server1: centos 서버2: vm1 포트: 22 - server1: centos 서버2: vm2 포트: 22 - server1: vm2 server2: vm1 포트: 22 - server1: vm1 server2: vm2 포트: 80
Ansible 스크립트는 다음과 같아야 합니다.
이름: 확인을 위해 서버 방화벽 포함 include_vars: server.yml
이름: 호스트 이름 가져오기 셸: echo {{ ansible_hostname }} > /tmp/{{ ansible_hostname }}_data.csv
no_log: 사실
인수: 실행 파일: /bin/bash
이름: IP 가져오기 셸: 호스트 이름 -I | cut -d' ' -f1 >> /tmp/{{ ansible_hostname }}_data.csv 인수: 실행 파일: /bin/bash
이름: CPU 코어 가져오기 셸: nproc >> /tmp/{{ ansible_hostname }}_data.csv 인수: 실행 파일: /bin/bash
이름: 메모리 가져오기기억 계산 쉘: awk '/MemTotal/ {print $2}' /proc/meminfo > 메모리 인수: 실행 파일: /bin/bash
이름: GB 단위로 메모리 가져오기기억 계산 쉘: awk '{ print $1/1000000}' 메모리 >> /tmp/{{ ansible_hostname }}_data.csv 인수: 실행 파일: /bin/bash
이름: 디스크 정보 가져오기디스크 계산
MB가 포함된 디스크가 있으면 계산이 잘못됩니다.
/dev/sd 대신 /dev/vd가 사용됩니다.
쉘: fdisk -l|grep /dev/sd > disk && awk '/Disk /dev/sd/ {print $3}' 디스크 > disk2 인수: 실행 파일: /bin/bash
이름: 디스크 수를 가져옵니다.디스크 계산 쉘: cat disk2 | wc -l 인수: 실행 파일: /bin/bash 레지스터: countline
이름: 디스크 합계를 계산합니다.디스크 계산 쉘: awk '{sum+=$1} (NR%{{countline.stdout}})==0{print sum}' disk2 >> /tmp/{{ ansible_hostname }}_data.csv 인수: 실행 파일: /bin/bash
이름: csv 삭제 셸: rm -f /tmp/{{ ansible_hostname }}_output.csv 인수: 실행 파일: /bin/bash 경고: falseignore_errors: True
name: CSV에 한 줄로 입력하세요. shell: cat /tmp/{{ ansible_hostname }}_data.csv | awk '{print}' ORS=',' > /tmp/{{ ansible_hostname }}_output.csv 인수: 실행 파일: /bin/bash
name: 원격 호스트 포트인지 확인하세요. wait_for: 호스트={{ item.server2 }} 포트={{ item.port }} timeout=1ignore_errors: True 레지스터: out when: item.server1 == ansible_hostname with_items: "{{ 서버 }}"
디버그: var=out
이름: 원격 포트
저장 셸: printf "\n,{{ item.item.server2 }} 포트 {{ item.item.port }}는 {% if item.failed %}closed{% else %}open{% endif %}" >> /tmp/{{ ansible_hostname }}_output.csv 인수: 실행 파일: /bin/bash 언제: item.item.server1 == ansible_hostname with_items: "{{ out.results }}"이름: 로컬 /tmp/fetched 삭제 쉘: rm -fr /tmp/fetched/ args: 실행 파일: /bin/bash 경고: Falseignore_errors: True 대리자_to: localhost
name: 파일 가져오는 중 fetch: src: /tmp/{{ ansible_hostname }}_output.csv dest: /tmp/fetched
name: localhost 쉘에서 한 번 실행: echo 'Hostname,IP,CPU-Core,Ram-GB,Disk-GB' > /tmp/fetched/main_output.csv run_once: True 대리자_to: localhost
이름: 하나의 파일로 결합됨 쉘: cat /tmp/fetched/{{ ansible_hostname }}/tmp/{{ ansible_hostname }}_output.csv >> /tmp/fetched/main_output.csv && printf '\r\n' >> /tmp/fetched/main_output.csv 인수: 실행 파일: /bin/bash 대리자_to: 로컬 호스트
그리고 output.csv는 다음과 같아야 합니다.
호스트 이름,IP,CPU-Core,Ram-GB,Disk-GB vm1,192.168.2.70,1,0.999696,21.5, ,vm2 포트 80은 열려 있습니다. centos,192.168.2.100,2,1.86599,85.9, ,vm1 포트 22는 열려 있습니다. open,vm2 포트 22가 열려 있습니다. vm2,192.168.2.71,2,1.86705,86, ,vm1 포트 22가 열려 있습니다.