# Playbook 部署Node\_exporter

### **目录结构**

```bash
[root@localhost src]# tree
.
├── hosts
├── install.yml
└── roles
    └── node
        ├── files
        │   └── node_exporter-1.3.1.linux-amd64.tar.gz
        ├── handlers
        │   └── main.yml
        ├── README.md
        ├── tasks
        │   └── main.yml
        ├── templates
        │   ├── node_exporter.service.j2
        │   └── node.yaml.j2
        └── vars
            └── main.yml
```

### **主机文件**

```bash
[root@localhost src]# cat hosts 
[node_exporter]
10.10.181.17
```

### **入口文件**

```bash
[root@localhost src]# cat install.yml 
---
- name: "Deployment to node"
  gather_facts: false
  hosts: node_exporter
  roles:
    - { role: 'node', tags: 'install_node'}
```

### **变量文件**

```bash
[root@localhost src]# more roles/node/vars/main.yml 
---
# vars file for prom_node
NODE_EXPORTER_VERSION: 1.3.1
NODE_PACKAGE: node_exporter-{{ NODE_EXPORTER_VERSION }}.linux-amd64.tar.gz
INSTALL_DIR: /usr/local/src

# prometheus service password is Ye5zHuxUQANGj6Bczq
BASIC_AUTH_USER: "prometheus"
BASIC_AUTH_PASS: "$2y$12$v0NACvxbfJ/3KeyhQOSyjOkdLOXh26qBQEo5VqqTlAvwSDUVcO9yS"
```

### **触发动作**

```bash
[root@localhost src]# more roles/node/handlers/main.yml 
- name: restart node_exporter
  systemd:
     name: node_exporter
     state: restarted
     daemon_reload: yes
     enabled: yes
```

### **任务文件**

```bash
[root@localhost src]# more roles/node/tasks/main.yml 
---
# tasks file for prom_node
- name: "拷贝 node 压缩包"
  unarchive:
    src: "{{ NODE_PACKAGE }}"
    dest: "{{ INSTALL_DIR }}"

- name: "修改包名"
  shell: cd {{ INSTALL_DIR }} && mv node_exporter-{{ NODE_EXPORTER_VERSION }}.linux-amd64 node_exporter


- name: "启动文件"
  template:
    src: "{{ item.src }}"
    dest: "{{ item.dest }}"
  with_items:
    - { src: "node_exporter.service.j2", dest: "/usr/lib/systemd/system/node_exporter.service" }
    - { src: "node.yaml.j2", dest: "{{ INSTALL_DIR }}/node_exporter/node.yaml" }
  notify: 
    - restart node_exporter



#- name: "status for node_exporter"
#  shell: systemctl status node_exporter;ss -tnl |grep 9100


#- name: "启动Faild"
#  debug:
#    msg: "node_exporter is not running"
#  when: _check.rc != 0


#- debug:
#    msg: "{{ _check.stdout_lines }}"
#  when: _check.rc == 0
```

### **摸版文件**

```bash
[root@localhost src]# more roles/node/templates/node_exporter.service.j2 
[Unit]
Description=node_exporter

[Service]
User=root
ExecStart={{ INSTALL_DIR }}/node_exporter/node_exporter --web.config={{ INSTALL_DIR }}/node_exporter/node.yaml
[Install]
WantedBy=default.target
```

```bash
[root@localhost src]# more roles/node/templates/node.yaml.j2 
basic_auth_users:
  {{ BASIC_AUTH_USER }}: {{ BASIC_AUTH_PASS}}
```

### **执行结果**

```bash
[root@localhost src]# ansible-playbook -i hosts install.yml -k
SSH password: 

PLAY [Deployment to node] ******************************************************************************************************************************************************************************************************************

TASK [拷贝 node 压缩包] *************************************************************************************************************************************************************************************************************************
changed: [10.10.181.17]

TASK [node : 设置为systemd服务，开机自启动] ***********************************************************************************************************************************************************************************************************
changed: [10.10.181.17]

TASK [restart node] ************************************************************************************************************************************************************************************************************************
changed: [10.10.181.17]

TASK [status for node_exporter] ************************************************************************************************************************************************************************************************************
changed: [10.10.181.17]

TASK [node : 启动Faild] **********************************************************************************************************************************************************************************************************************
skipping: [10.10.181.17]

TASK [node : debug] ************************************************************************************************************************************************************************************************************************
ok: [10.10.181.17] => {
    "msg": [
        "● node_exporter.service - node_exporter", 
        "   Loaded: loaded (/usr/lib/systemd/system/node_exporter.service; enabled; vendor preset: disabled)", 
        "   Active: active (running) since Tue 2020-06-29 18:44:35 CST; 646ms ago", 
        " Main PID: 14658 (node_exporter)", 
        "    Tasks: 3", 
        "   Memory: 1.1M", 
        "   CGroup: /system.slice/node_exporter.service", 
        "           └─14658 /usr/local/src/node_exporter/node_exporter", 
        "", 
        "Jun 29 18:44:35 localhost.localdomain node_exporter[14658]: time=\"2021-06-29T18:44:35+08:00\" level=info msg=\" - sockstat\" source=\"node_exporter.go:104\"", 
        "Jun 29 18:44:35 localhost.localdomain node_exporter[14658]: time=\"2021-06-29T18:44:35+08:00\" level=info msg=\" - stat\" source=\"node_exporter.go:104\"", 
        "Jun 29 18:44:35 localhost.localdomain node_exporter[14658]: time=\"2021-06-29T18:44:35+08:00\" level=info msg=\" - textfile\" source=\"node_exporter.go:104\"", 
        "Jun 29 18:44:35 localhost.localdomain node_exporter[14658]: time=\"2021-06-29T18:44:35+08:00\" level=info msg=\" - time\" source=\"node_exporter.go:104\"", 
        "Jun 29 18:44:35 localhost.localdomain node_exporter[14658]: time=\"2021-06-29T18:44:35+08:00\" level=info msg=\" - timex\" source=\"node_exporter.go:104\"", 
        "Jun 29 18:44:35 localhost.localdomain node_exporter[14658]: time=\"2021-06-29T18:44:35+08:00\" level=info msg=\" - uname\" source=\"node_exporter.go:104\"", 
        "Jun 29 18:44:35 localhost.localdomain node_exporter[14658]: time=\"2021-06-29T18:44:35+08:00\" level=info msg=\" - vmstat\" source=\"node_exporter.go:104\"", 
        "Jun 29 18:44:35 localhost.localdomain node_exporter[14658]: time=\"2021-06-29T18:44:35+08:00\" level=info msg=\" - xfs\" source=\"node_exporter.go:104\"", 
        "Jun 29 18:44:35 localhost.localdomain node_exporter[14658]: time=\"2021-06-29T18:44:35+08:00\" level=info msg=\" - zfs\" source=\"node_exporter.go:104\"", 
        "Jun 29 18:44:35 localhost.localdomain node_exporter[14658]: time=\"2021-06-29T18:44:35+08:00\" level=info msg=\"Listening on :9100\" source=\"node_exporter.go:170\"", 
        "LISTEN     0      128       [::]:9100                  [::]:*                  "
    ]
}

PLAY RECAP *********************************************************************************************************************************************************************************************************************************
10.10.181.17               : ok=5    changed=4    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0  
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://close.gitbook.io/yun-wei-bi-ji/centos/ansible/playbook-bu-shu-nodeexporter.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
