天天看點

Ansible自動化工具,懶鬼首選-百台伺服器node_exporter安裝

作者:街頭角落裡
Ansible自動化工具,懶鬼首選-百台伺服器node_exporter安裝
Ansible自動化工具,懶鬼首選-百台伺服器node_exporter安裝

推薦

Prometheus告警規則(中文告警案例)

一分鐘部署完Prometheus+alertmanager+grafana

prometheus監控alertmanager郵件釘釘告警,配置模闆,拿來就用

一/編寫playbook

---
- name: Install node_exporter
  hosts: sssaa
  become: yes
  
  vars:
    service_name: "node_exporter.service"
    file_path: /etc/systemd/system/node_exporter.service
    file_content: |
      [Unit]
      Description=node_exporter
      After=network.target
      
      [Service]
      User=root
      Group=root
      ExecStart=/usr/local/node_exporter/node_exporter --web.listen-address=:9001 --collector.systemd --collector.systemd.unit-whitelist=(sshd|nginx).service --collector.processes --collector.tcpstat --collector.supervisord
      [Install]
      WantedBy=multi-user.target

  tasks:
    - name: Create node_exporter directory
      file:
        path: /usr/local/node_exporter/
        state: directory

    - name: Download node_exporter binary
      get_url:
        url: https://github.com/prometheus/node_exporter/releases/download/v1.6.0/node_exporter-1.6.0.linux-amd64.tar.gz
        dest: /usr/local/src/

    - name: Extract node_exporter binary
      unarchive:
        src: /usr/local/src/node_exporter-1.6.0.linux-amd64.tar.gz
        dest: /usr/local/node_exporter/
        copy: no
        extra_opts: [--strip-components=1]

    - name:  Create the directory if it does not exist
      file:
        path: "{{ file_path | dirname }}"
        state: directory

    - name: Create the node_exporter.service
      copy:
        content:  "{{ file_content }}"
        dest: "{{ file_path }}"
        mode: '0644'

    - name: Restart node_exporter.service service
      systemd:
        name: "{{ service_name }}"
        state: restarted           

說明:

• playbook的名稱為“Install node_exporter”,hosts為all,表示在所有主機上運作。

• become: yes 表示使用root權限運作任務。

• 使用 vars 部分定義變量,這裡定義了三個變量。 |這個表示轉行的全部内容

• 建立 /usr/local/node_exporter/目錄

• 使用get_url子產品從 node_exporter官網下載下傳二進制源碼包到 /usr/local/src/ 目錄。

• 使用unarchive子產品解壓二進制源碼包到 /usr/local/node_exporter/目錄下,并使用 --strip-components=1 選項删除預設的頂層目錄名稱。這樣可以確定源碼包直接解壓到/usr/local/node_exporter/目錄下,不會多一層檔案夾。

• 使用 file 子產品建立目錄。該子產品将檢查目錄是否存在,如果不存在,則建立它。

• 使用 copy 子產品建立檔案,并将 content 參數設定為 file_content 變量的值,将 dest 參數設定為 file_path 變量的值,mode給權限。

• 使用systemd子產品啟動 node_exporter服務。

二/運作

注:删除了過多的運作條目。

[root@DZ-192-168-1-200 playbook]# ansible-playbook /data/playbook/node_exporter.yml 
PLAY [Install node_exporter] ******************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************
ok: [192.168.1.6]

TASK [Create node_exporter directory] *********************************************************************************************************
changed: [192.168.1.6]

TASK [Download node_exporter binary] **********************************************************************************************************
changed: [192.168.0.6]

TASK [Extract node_exporter binary] ***********************************************************************************************************
changed: [192.168.0.6]

TASK [Create the directory if it does not exist] **********************************************************************************************
ok: [192.168.0.6]

TASK [Create the node_exporter.service] *******************************************************************************************************
changed: [192.168.1.6]

TASK [Restart node_exporter.service service] **************************************************************************************************
changed: [192.168.1.6]

PLAY RECAP ************************************************************************************************************************************
192.168.1.6              : ok=7    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0              
Ansible自動化工具,懶鬼首選-百台伺服器node_exporter安裝

繼續閱讀