
この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
servers.ymlは次のようになります。
サーバー: - server1: centos server2: vm1 ポート: 22 - server1: centos server2: vm2 ポート: 22 - server1: vm2 server2: vm1 ポート: 22 - server1: vm1 server2: vm2 ポート: 80
Ansibleスクリプトは次のようになります。
name: チェックするサーバーのファイアウォールを含める include_vars: server.yml
名前: ホスト名を取得 シェル: echo {{ ansible_hostname }} > /tmp/{{ ansible_hostname }}_data.csv
ログなし: 真
引数: 実行可能ファイル: /bin/bash
名前: IP を取得 シェル: hostname -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}' disk > disk2 引数: 実行可能ファイル: /bin/bash
名前: ディスクの数を取得するディスク計算 シェル: cat disk2 | wc -l 引数: 実行可能ファイル: /bin/bash レジスタ: countline
名前: ディスクの合計を計算するディスク計算 シェル: awk '{sum+=$1} (NR%{{countline.stdout}})==0{print sum}' ディスク2 >> /tmp/{{ ansible_hostname }}_data.csv 引数: 実行可能ファイル: /bin/bash
名前: csv を削除 シェル: rm -f /tmp/{{ ansible_hostname }}_output.csv 引数: 実行可能ファイル: /bin/bash 警告: false ignore_errors: True
名前: CSV に 1 行で入力します シェル: cat /tmp/{{ ansible_hostname }}_data.csv | awk '{print}' ORS=',' > /tmp/{{ ansible_hostname }}_output.csv 引数: 実行可能ファイル: /bin/bash
name: リモート ホストのポートを確認します wait_for: host={{ item.server2 }} port={{ item.port }} timeout=1 ignore_errors: True register: out when: item.server1 == ansible_hostname with_items: "{{ servers }}"
デバッグ: var=out
name: リモート ポートを保存
shell: printf "\n、{{ item.item.server2 }} ポート {{ item.item.port }} は {% if item.failed %}closed{% else %}open{% endif %} です" >> /tmp/{{ ansible_hostname }}_output.csv args: 実行可能ファイル: /bin/bash when: item.item.server1 == ansible_hostname with_items: "{{ out.results }}"名前: ローカルの /tmp/fetched を削除します。 シェル: rm -fr /tmp/fetched/ 引数: 実行可能ファイル: /bin/bash 警告: False エラーを無視します: True 委任先: localhost
name: ファイルを取得しています fetch: src: /tmp/{{ ansible_hostname }}_output.csv dest: /tmp/fetched
name: ローカルホストで 1 回実行 shell: echo 'ホスト名、IP、CPU コア、メモリ (GB)、ディスク (GB)' > /tmp/fetched/main_output.csv run_once: True delegate_to: localhost
名前: 1 つのファイルに結合 シェル: 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 delegate_to: localhost
そしてoutput.csvは次のようになります。
ホスト名、IP、CPU コア、Ram GB、ディスク 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 が開いています、vm2 ポート 22 が開いています vm2、192.168.2.71、2、1.86705、86、vm1 ポート 22 が開いています