anisble批量安装node_exporter

一、目录结构

$ tree .
.
|-- hosts
|-- node_exporter
|   |-- files
|   |   |-- node_exporter-1.0.1.linux-amd64.tar.gz
|   |   `-- node_exporter.service
|   `-- tasks
|       `-- main.yml
`-- node_exporter.yml

二、playbook文件

$ cat node_exporter.yml 
#!/usr/bin/env ansible-playbook
- hosts: all
  remote_user: root
  gather_facts: false
  roles:
  - role: node_exporter

三、服务文件

$ cat node_exporter/files/node_exporter.service 
[Unit]
Description=Prometheus node_exporter
Requires=network.target remote-fs.target
After=network.target remote-fs.target

[Service]
Type=simple
User=root
Group=root
ExecStart=/usr/local/node_exporter/node_exporter --web.listen-address=0.0.0.0:9100
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=multi-user.target

四、任务文件

$ cat node_exporter/tasks/main.yml 
- name: 安装node_exporter
  unarchive: 
    src: node_exporter-1.0.1.linux-amd64.tar.gz
    dest: /usr/local/

- name: 创建软链接
  file:
    src: /usr/local/node_exporter-1.0.1.linux-amd64
    dest: /usr/local/node_exporter
    state: link

- name: 添加node_exporter服务
  copy: 
    src: node_exporter.service
    dest: /usr/lib/systemd/system/

- name: daemon-reload
  systemd: 
    daemon_reload: yes

- name: 设置开机自动启动
  systemd: 
    name: node_exporter
    state: started
    enabled: True

- name: 确定端口在监听
  wait_for:
    host: 0.0.0.0
    port: 9100
    delay: 2
posted @ 2021-02-04 18:19  吕振江  阅读(528)  评论(0编辑  收藏  举报
浏览器标题切换
浏览器标题切换end