ansible 批量部署 node_exporter

ansible配置

ansible 机器ip:192.168.8.126

目的机器ip:192.168.173.141 ,远程用户:ubuntu,密码:ubuntu

主机清单文件 /etc/ansible/hosts

cat /etc/ansible/hosts
[node-exporter]
192.168.173.141 ansible_ssh_user="ubuntu" ansible_ssh_pass="ubuntu" ansible_sudo_pass="ubuntu" 

远程普通用户若要使用 sudo 权限,需要在 /etc/ansible/ansible.cfg,若远程是root用户则可跳过。

[privilege_escalation]
become=True
become_method=sudo

node_exporter 配置和 service 的编写

下载 node_exporter 的压缩包以及解压:

wget https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz -o /node-exporter/
tar -xf /node-exporter/node_exporter-1.3.1.linux-amd64.tar.gz -C 、node-exporter/

把 node-exporter 编写成服务,交由 systemctl 管理

vim /node-exporter/node-exporter.service
[Unit]
Description=node_exporter
Requires=network.target remote-fs.target
After=network.target remote-fs.target

[Service]
Type=simple
ExecStart=/root/node-exporter/node_exporter-1.3.1.linux-amd64/node_exporter --web.listen-address=:9100
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=multi-user.target

编写 playbook

vim deploy.yaml
---
- hosts: node-exporter
  tasks:
    - name: copy node-exporter
      copy:
        src: "{{item.src}}"
        dest: "{{item.dest}}"
        mode: 0755
      with_items:
        - {src: "/root/node-exporter",dest: "/root/"}

    - name: copy node-exporter.service
      shell: sudo cp /root/node-exporter/node-exporter.service /usr/lib/systemd/system/
      notify:
        - reload

    - name: start and enable server
      shell: sudo systemctl start node-exporter.service && sudo systemctl enable node-exporter.service

  handlers:
  - name: reload
    shell: sudo systemctl daemon-reload    

执行

ansible-playbook deploy.yaml

你可能感兴趣的:(ansible,ubuntu,服务器)