Ansible批量部署Node Exporter实战:Playbook配置与远程主机接入

📅 发布时间:2026/7/31 9:10:35
Ansible批量部署Node Exporter实战:Playbook配置与远程主机接入 前言只有几台服务器时逐台通过SSH安装Node Exporter并不算困难。但随着主机数量增加同样的下载、解压、创建用户、配置systemd服务和启动进程等操作需要不断重复安装版本和目录也容易出现差异。更麻烦的是后续如果需要升级Node Exporter或者调整启动参数还要重新登录每一台服务器进行修改。对于已经搭建Prometheus监控体系的环境来说采集端部署不统一会直接影响主机指标的完整性和后期维护效率。Ansible可以通过SSH对多台Linux主机执行统一任务不需要在目标主机额外安装专用客户端。将Node Exporter的安装过程写入Playbook后就可以按照相同的版本、目录和服务配置批量完成部署。本文将从Ubuntu环境安装Ansible开始配置Inventory、systemd模板和Node Exporter Playbook并在目标主机上验证服务状态。对于无法直接通过局域网连接的异地设备还会使用cpolar映射SSH端口让Ansible能够继续连接并执行部署任务。1.在你的虚拟机上安装ansiable1.1 什么是AnsibleAnsible是一个开源的 自动化运维工具由Red Hat赞助开发用于实现配置管理Configuration Management应用部署Application Deployment任务自动化Task AutomationIT编排Orchestration它最大的特点是简单、无代理、基于SSH、使用YAML语法。1.2 Ansible的核心作用批量执行命令无需逐台登录服务器一条命令可在成百上千台机器上同时运行统一配置管理确保所有服务器的配置一致比如安装指定版本的软件如 Node Exporter、Nginx同步配置文件如 /etc/hosts、prometheus.yml创建用户、设置权限、管理服务状态自动化部署应用通过 PlaybookYAML 文件定义部署流程一键完成复杂应用上线幂等性Idempotency重复执行同一个 Playbook结果始终一致——没装的会装已装的不会重复操作安全可靠。无需安装客户端AgentlessAnsible通过SSH 与目标主机通信只需控制机安装Ansible被控机只要有Python 和SSH即可。支持云和容器编排可管理AWS、Azure、阿里云等云资源也能操作Docker、Kubernetes。1.3 安装ansiale我这里是ubuntu系统如果想使用centos可以看参考cpolar官网的这篇教程《别再手动操作了我用Ansiblecpolar给飞牛OS装了个“遥控器》登录到 Ubuntu系统并在APT命令下运行以应用更新sudoaptupdatesudoaptupgrade-y安装最新版本的Ansibleaptinstall-yansible安装完成后验证ansiable版本ansible--version我们查看一下有没有启动文件ls/etc/ansible我发现我这里没有那我们手动配一下创建ansiable文件mkdir-p/etc/ansible编写hosts文件cat/etc/ansible/hostsEOF [local] localhost ansible_connectionlocal EOF编写ansiable.cfg文件cat/etc/ansible/ansible.cfgEOF [defaults] inventory /etc/ansible/hosts host_key_checking False remote_user root stdout_callback yaml EOF验证是否成功ansible localhost-mping2.配置ansiable文件搞定Node Exporter部署确保你在playbook所在目录假设是 /etc/ansiblecd/etc/ansible创建templates目录mkdir-ptemplates创建服务模板文件cattemplates/node_exporter.service.j2EOF [Unit] DescriptionNode Exporter Afternetwork.target [Service Usernode_exporter Groupnode_exporter Typesimple ExecStart{{ install_dir }}/node_exporter --collector.systemd --collector.processes Restarton-failure RestartSec5 [Install] WantedBymulti-user.target EOF首先我们来过一下安装node_exporter基本步骤mkdir/shancd/shancurl-LOhttps://github.com/prometheus/node_exporter/releases/download/v1.10.2/node_exporter-1.10.2.linux-amd64.tar.gztar-zxvfnode_exporter-1.10.2.linux-amd64.tar.gzrm-rfnode_exporter-1.10.2.linux-amd64.tar.gzmvnode_exporter-1.10.2.linux-amd64/ node_exportersudovi/etc/systemd/system/node_exporter.service[Unit]DescriptionNode ExporterDocumentationhttps://github.com/prometheus/node_exporterAfternetwork.target[Service]Usernode_exporterGroupnode_exporterTypesimpleExecStart/shan/node_exporter/node_exporter[Install]WantedBydefault.targetuseradd--no-create-home--shell/bin/false node_exportersystemctl start node_exporter systemctlenablenode_exporter手动操作还好但是一键部署是不是感觉头都大了别急我这边已经研究好啦只需要你复制粘贴就能操作成功编辑hosts文件加入你所要监控的ip和他的用户名及密码[test] ip ansible_ssh_user用户名 ansible_ssh_pass密码编辑命令yml文件--- - name: 安装node_exporter hosts:testbecome:yesvars: node_exporter_version:1.8.2node_exporter_url:https://ghproxy.net/https://github.com/prometheus/node_exporter/releases/download/v{{ node_exporter_version }}/node_exporter-{{ node_exporter_version }}.linux-amd64.tar.gzinstall_dir:/shantasks: - name: 创建存储目录 file: path:{{ install_dir }}state: directory mode:0755- name: 下载node_exporter get_url: url:{{ node_exporter_url }}dest:{{ install_dir }}/node_exporter.tar.gzmode:0644timeout:30retries:3delay:10- name: 解压node_exporter unarchive: src:{{ install_dir }}/node_exporter.tar.gzdest:{{ install_dir }}remote_src:yes- name: 清理压缩包 file: path:{{ install_dir }}/node_exporter.tar.gzstate: absent - name: 重命名解压目录为 node_exporter command:mv{{ install_dir }}/node_exporter-{{ node_exporter_version }}.linux-amd64{{ install_dir }}/node_exporterargs: creates:{{ install_dir }}/node_exporter- name: 删除旧目录 file: path:{{ install_dir }}/node_exporter-{{ node_exporter_version }}.linux-amd64state: absent - name: 创建专用用户 user: name: node_exporter system:yesshell: /sbin/nologin createhome: no - name: 配置systemd服务 template: src: node_exporter.service.j2 dest: /etc/systemd/system/node_exporter.service owner: root group: root mode:0644- name: 重载systemd配置 systemd: daemon_reload:yes- name: 启动并启用node_exporter服务 systemd: name: node_exporter enabled:yesstate: started然后使用ansible-playbook执行就好啦ansible-playbook node_exporter.yml切换到被安装的IP下验证是否安装成功systemctl status node_exporter至此我们通过ansible配置node_exporter算是成功啦我们后续可以在hosts添加上百台主机统一安装。那么问题来了目前我们只是在统一局域网下可以部署那么我们想部署家里电脑想监控家里电脑的指标怎么办呢别急cpolar来拯救你3.安装cpolar实现随时随地开发cpolar 可以将你本地电脑中的服务如 SSH、Web、数据库映射到公网。即使你在家里或外出时也可以通过公网地址连接回本地运行的开发环境。❤️以下是安装cpolar步骤使用一键脚本安装命令sudocurlhttps://get.cpolar.sh|sh安装完成后执行下方命令查看cpolar服务状态如图所示即为正常启动sudosystemctl status cpolarCpolar安装和成功启动服务后在浏览器上输入虚拟机主机IP加9200端口即:【http://ip:9200】访问Cpolar管理界面使用Cpolar官网注册的账号登录,登录后即可看到cpolar web 配置界面,接下来在web 界面配置即可打开浏览器访问本地9200端口使用cpolar账户密码登录即可,登录后即可对隧道进行管理。4.配置公网地址通过配置你可以在本地 WSL 或 Linux 系统上运行 SSH 服务并通过 Cpolar 将其映射到公网从而实现从任意设备远程连接开发环境的目的。隧道名称可自定义本例使用了:ssh注意不要与已有的隧道名称重复协议tcp本地地址22端口类型随机临时TCP端口地区China Top创建成功后打开左侧在线隧道列表,可以看到刚刚通过创建隧道生成了公网地址接下来就可以在其他电脑或者移动端设备异地上使用任意一个地址在终端中访问即可。tcp 表示使用的协议类型2.tcp.cpolar.top是 Cpolar 提供的域名16198是随机分配的公网端口号通过 Cpolar 提供的公网地址和端口就可以使用ansible进行远程部署啦接下来我们操作一下。修改hosts配置文件[dbservers]2.tcp.cpolar.topansible_userrootansible_port16198ansible_password***接下来我们执行配置node_exporter文件ansible-playbook node_exporter.yml执行成功我们去对应主机查看node_exporter服务是否开启成功我们可以看见目标主机已经成功安装~5.保留固定TCP公网地址使用cpolar为其配置TCP地址该地址为固定地址不会随机变化。选择区域和描述有一个下拉菜单当前选择的是“China VIP”。右侧输入框用于填写描述信息。保留按钮在右侧有一个橙色的“保留”按钮点击该按钮可以保留所选的TCP地址。列表中显示了一条已保留的TCP地址记录。地区显示为“China VIP”。地址显示为“15.tcp.cpolar.top:13633”。登录cpolar web UI管理界面点击左侧仪表盘的隧道管理——隧道列表找到所要配置的隧道ssh点击右侧的编辑。修改隧道信息将保留成功的TCP端口配置到隧道中。端口类型选择固定TCP端口预留的TCP地址填写保留成功的TCP地址点击更新。创建完成后打开在线隧道列表此时可以看到随机的公网地址已经发生变化地址名称也变成了保留和固定的TCP地址。这样我们的ansible操作就没有任何的阻碍啦总结完成以上配置后Node Exporter的下载、解压、用户创建、systemd服务配置和启动过程就已经写入Ansible Playbook。后续添加新服务器时只需要将目标主机信息加入Inventory再执行相同的Playbook即可。相比逐台登录服务器安装这种方式更重要的价值是保持配置一致。Node Exporter使用的版本、安装目录、运行用户和启动参数都由同一份文件控制后续需要升级或调整配置时也可以继续通过Ansible统一执行。对于与Ansible控制端不在同一网络中的主机可以通过cpolar将目标设备的SSH端口映射为公网TCP地址再在Inventory中填写对应域名和端口。配置固定TCP地址后连接入口不会随着临时隧道变化更适合后续重复执行自动化任务。实际用于多台生产服务器前建议先选择少量测试节点验证Playbook并根据目标主机的系统版本、CPU架构、Python环境和权限配置进行调整。同时应优先使用SSH密钥管理连接凭据避免直接在Inventory文件中长期保存明文密码。