無日誌:真

無日誌:真

我有這個輸入.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 應該是這樣的,


伺服器: - 伺服器1:centos 伺服器2:vm1 連接埠:22 - 伺服器1:centos 伺服器2:vm2 連接埠:22 - 伺服器1:vm2 伺服器2:vm1 連接埠:22 - 伺服器1:vm1 伺服器2:vm2 連接埠:80

ansible腳本應該是這樣的,


  • 名稱:包含伺服器防火牆以檢查 include_vars:server.yml

  • name: 取得主機名稱 shell: echo {{ ansible_hostname }} > /tmp/{{ ansible_hostname }}_data.csv

    無日誌:真

    參數:可執行檔:/bin/bash

  • 名稱:取得 IP shell: 主機名稱 -I | cut -d' ' -f1 >> /tmp/{{ ansible_hostname }}_data.csv 參數:執行檔:/bin/bash

  • 名稱:取得 cpu 核心 shell:nproc >> /tmp/{{ ansible_hostname }}_data.csv 參數:執行檔:/bin/bash

  • 名稱:取得記憶體記憶體計算 shell: awk '/MemTotal/ {print $2}' /proc/meminfo > 記憶體參數:執行檔:/bin/bash

  • 名稱:獲取記憶體(GB)記憶體計算 shell: awk '{ print $1/1000000}' 記憶體 >> /tmp/{{ ansible_hostname }}_data.csv 參數:執行檔:/bin/bash

  • 名稱:取得磁碟資訊磁碟計算

    如果有MB的磁碟,計算將會錯誤

    並且使用 /dev/vd 代替 /dev/sd

    shell: fdisk -l|grep /dev/sd > disk && awk '/Disk /dev/sd/ {print $3}' disk > disk2 參數:執行檔:/bin/bash

  • name: 取得幾個磁碟磁碟計算 外殼:cat disk2 | wc -l args:可執行檔:/bin/bash 暫存器:countline

  • name: 計算磁碟總和磁碟計算 shell: awk '{sum+=$1} (NR%{{countline.stdout}})==0{print sum}' disk2 >> /tmp/{{ ansible_hostname }}_data.csv 參數:執行檔:/bin /bash

  • 名稱:刪除 csv shell:rm -f /tmp/{{ ansible_hostname }}_output.csv 參數:可執行檔:/bin/bash 警告:假ignore_errors:True

  • 名稱:將其放入一行 shell 中的 CSV 中: 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=1ignore_errors: True register: out when: item.server1 == ansible_hostname with_items: "{{servers }} "

  • 調試:var=out

  • name: 儲存遠端連接埠
    shell: printf "\n,{{ item.item.server2 }} port {{ item.item.port }} is {% if item.failed %}close{% else %}open{% endif %}" >> /tmp/{{ ansible_hostname }}_output.csv 參數:執行檔:/bin/bash 時:item.item.server1 == ansible_hostname with_items:"{{ out.results }}"

  • 名稱:刪除本地 /tmp/fetched shell:rm -fr /tmp/fetched/ args:可執行檔:/bin/bash warn:Falseignore_errors:Truedelegate_to:localhost

  • 名稱:正在取得檔案 fetch:src:/tmp/{{ ansible_hostname }}_output.csv dest:/tmp/fetched

  • name: 在 localhost shell 上執行一次: echo 'Hostname,IP,CPU-Core,Ram-GB,Disk-GB' > /tmp/fetched/main_output.csv run_once: True delegate_to: localhost

  • name: 合併到一個檔案 shell: 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

    輸出.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.8659,85.已開啟open ,vm2 連接埠 22 已開啟 vm2,192.168.2.71,2,1.86705,86, ,vm1 連接埠 22 已開啟

相關內容