ansible-Role角色批量按照node_export节点,并追加信息到Prometheus文件中

2024-06-13 01:12

本文主要是介绍ansible-Role角色批量按照node_export节点,并追加信息到Prometheus文件中,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

      • 剧本功能
    • inventory.yaml文件定义
    • deploy.yaml角色定义
    • node_exporter_lock角色定义
    • 任务角色main.yaml
      • node_exporter_tasks.yml角色
      • 触发任务notify
      • extra_tasks.yml角色
      • prometheus_node_config.j2模板文件
    • 执行命令
    • 查看变量

剧本功能

功能1: 批量执行node_export节点,并判断操作系统,有俩份文件
功能2: 获取当前组并追加到ROle角色通用变量里面提供全部角色使用
功能3: 任务角色里面又分任务执行一个安装一个收集信息
功能:其他功能懒得总结了,大家看文章吧

inventory.yaml文件定义

[equipment:vars]  
group = "equipment" 
[equipment]
WA510-生产-设备管理09  ansible_internal_ip=xxxx  ansible_host=xxxx
WA485-生产-设备管理08  ansible_internal_ip=xxxx  ansible_host=xxxx

image.png

deploy.yaml角色定义

- name: 任务总线become: truegather_facts: yeshosts: oa tasks:- name: 加载变量include_vars:file: ./vars/vars.yaml- name: 获取当前组名set_fact:group_name: "{{ group_names[0] }}"run_once: true- name: 生成 main.yml 文件lineinfile:path: "/root/ansible_role_k8s/init_roles/defaults/main.yml"regexp: '^group:'line: "group: {{ group_name }}"delegate_to: localhost  # 指定任务在本地执行run_once: true- name: 加载更新后的默认变量include_vars:file: "/root/ansible_role_k8s/init_roles/defaults/main.yml"  # 使用绝对路径delegate_to: localhost  # 指定任务在本地执行run_once: true- name: 执行 node_exporter 角色include_role:name: init_roles/node_exporter_lockwhen: "'node' in ansible_run_tags"tags: ['node']- name: 执行 init 角色include_role:name: init_roles/initwhen: "'ls' in ansible_run_tags"tags: ['ls']

/root/ansible_role_k8s/init_roles/defaults/main.yml文件如下

root@iZbp1bh3oeew2pt9bwrs4rZ:~/ansible_role_k8s/init_roles# cat ./defaults/main.yml
group: oa

node_exporter_lock角色定义

root@iZbp1bh3oeew2pt9bwrs4rZ:~/ansible_role_k8s/init_roles/node_exporter_lock# tree
.
├── files
│   ├── node_exporter
│   ├── node_exporter-1.8.1.linux-amd64.tar.gz
│   ├── node_exporter.service
│   └── 参数详解
├── handlers
│   └── main.yaml
├── tasks
│   ├── extra_tasks.yml
│   ├── main.6.5.yaml
│   ├── main.yaml
│   ├── main.yaml.bak
│   └── node_exporter_tasks.yml
└── templates├── main.yml.j2└── prometheus_node_config.j24 directories, 12 files

任务角色main.yaml

- name: 执行 node_exporter 任务include_tasks: node_exporter_tasks.ymltags: ['node']- name: 执行附加任务include_tasks: extra_tasks.ymltags: ['node']

image.png

node_exporter_tasks.yml角色

- name: 判断远程服务器有没有安装 node_exporter 服务shell: "pgrep -f 'node_exporter'"register: node_exporter_statusignore_errors: truetags: ['node']- name: 判断远程服务器 9100 端口有没有被占用shell: "ss -lntp | grep ':9100'"register: port_statusignore_errors: truetags: ['node']- name: 打印服务和端口占用情况debug:msg: "服务状态: {{ '存在' if node_exporter_status.rc == 0 else '不存在' }}, 端口状态: {{ '被占用' if port_status.rc == 0 else '未被占用' }}"tags: ['node']- block:- name: 传输并解压 node_exporter 包到指定目录unarchive:src: /root/ansible_role_k8s/init_roles/node_exporter_lock/files/node_exporter-1.8.1.linux-amd64.tar.gzdest: /usr/local/bin/mode: '0755'remote_src: no- name: 判断操作系统是 CentOS 6、CentOS 7,或者 Ubuntuset_fact:os_version: "{{ ansible_distribution }} {{ ansible_distribution_major_version }}"- name: 传输并配置 node_exporter 服务文件(适用于 CentOS 6)copy:src: /root/ansible_role_k8s/init_roles/node_exporter_lock/files/node_exporterdest: /etc/init.d/node_exportermode: '0755'when: os_version == 'CentOS 6'notify: - Reload systemd- Enable and start node_exporter (CentOS 6)- name: 传输并配置 node_exporter systemd 服务文件(适用于 CentOS 7 和 Ubuntu)copy:src: /root/ansible_role_k8s/init_roles/node_exporter_lock/files/node_exporter.servicedest: /etc/systemd/system/node_exporter.servicemode: '0755'when: os_version != 'CentOS 6'notify: - Reload systemd- Enable and start node_exporterwhen: port_status.rc == 1  # 仅当 9100 端口未被占用时执行整个 blocktags: ['node']- name: 记录未执行 block 任务的节点信息lineinfile:path: /var/log/ansible_node_exporter.logline: "Host {{ inventory_hostname }}: 服务状态: {{ '存在' if node_exporter_status.rc == 0 else '不存在' }}, 端口状态: {{ '被占用' if port_status.rc == 0 else '未被占用' }}"when: port_status.rc != 1  # 仅当 9100 端口被占用时执行delegate_to: localhost  # 在控制节点上执行,以便集中记录日志tags: ['node']- name: 打印任务执行完成信息debug:msg: "任务执行完成"tags: ['node']

触发任务notify

main.yaml

- name: Reload systemdcommand: systemctl daemon-reloadwhen: os_version != 'CentOS 6'- name: Enable and start node_exportersystemd:name: node_exporter.serviceenabled: yesstate: startedwhen: os_version != 'CentOS 6'- name: Enable and start node_exporter (CentOS 6)shell: chkconfig node_exporter on && service node_exporter startwhen: os_version == 'CentOS 6'

image.png

extra_tasks.yml角色

- name: 确保 prometheus_node 组已定义并且收集成功节点信息并去重assert:that: groups['prometheus_node'] is definedfail_msg: "The prometheus_node group is not defined in the inventory."run_once: truedelegate_to: localhosttags: ['node']- name: 收集成功节点信息set_fact:successful_nodes: "{{ groups['prometheus_node'] | map('extract', hostvars) }}"run_once: truedelegate_to: localhosttags: ['node']- name: 使用模板生成 Prometheus 配置片段template:src: prometheus_node_config.j2dest: "/tmp/prometheus_nodes_{{ lookup('pipe', 'date +%Y%m%d%H%M%S') }}.yml"delegate_to: localhostrun_once: truetags: ['node']- name: 在本地文件中追加成功节点信息shell: "cat {{ item }} >> /opt/prometheus/prometheus1.yml"with_fileglob:- "/tmp/prometheus_nodes_*.yml"delegate_to: localhostrun_once: truetags: ['node']

image.png

prometheus_node_config.j2模板文件

{% for host in successful_nodes %}- targets:- "{{ host.ansible_host }}:9100"labels:instance: "{{ host.inventory_hostname }}-{{ host.ansible_host }}"namespace: '{{ namespace }}'
{% endfor %}

image.png

执行命令

ansible-playbook deploy.yaml --tags node

image.png

image.png

image.png

查看变量

root@iZbp1bh3oeew2pt9bwrs4rZ:~/ansible_role_k8s/init_roles# cat ./defaults/main.yml
group: oa

查看 追加node节点信息

cat /opt/prometheus/prometheus1.yml 

image.png

大概思路就是这样,需要完整Role角色的请私聊我

image.png

这篇关于ansible-Role角色批量按照node_export节点,并追加信息到Prometheus文件中的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1055869

相关文章

通过高德api查询所有店铺地址信息

通过高德api查询所有店铺地址电话信息 需求:通过高德api查询所有店铺地址信息需求分析具体实现1、申请高德appkey2、下载types city 字典值3、具体代码调用 需求:通过高德api查询所有店铺地址信息 需求分析 查询现有高德api发现现有接口关键字搜索API服务地址: https://developer.amap.com/api/webservice/gui

chart 完成拓扑图单节点拖拽不影响其他节点位置

就是做这种的功能,箭头原本是可以动态重复移动的,但不知道哪里问题导致没箭头了,然后补了个edgeSymbol: ['','arrow'], 字段,才增加了箭头。 拖拽某个节点,只有关联到的线条会跟着变动其他的节点位置不变。 参考 https://gallery.echartsjs.com/editor.html?c=x8Fgri22P9 https://echarts.baidu.com/exa

黑客帝国终极大Boss的角色是啥?

《黑客帝国》是非常经典的科幻电影,第一部于1999年3月31日上映。时隔20多年,人类正在一步步地朝着电影中描述的矩阵世界发展。今年正好是人工智能大规模发展的一年,再加上最近Open AI的宫斗戏,让一切都变得神秘莫测。 如果还没有看过《黑客帝国》的话,强烈推荐去看看。今天不聊电影赏析方面的事,我也不专业,今天还是借电影聊聊企业经营管理方面的事情。先抛个问题,您知道黑客帝国里终极大Boss的角色

(13)DroneCAN 适配器节点(一)

文章目录 前言 1 特点 2 固件  3 ArduPilot固件DroneCAN设置 4 DroneCAN适配器节点 前言 这些节点允许现有的 ArduPilot 支持的外围设备作为 DroneCAN 或 MSP 设备适应 CAN 总线。这也允许扩展自动驾驶仪硬件的功能。如允许 I2C 设备(如罗盘或空速)距离自动驾驶仪 1m 以上,并实现多达 32 个伺服输出通道。

在CentOS 7上安装和配置Ansible的方法

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。 简介 配置管理系统旨在使管理员和运维团队轻松控制大量服务器。它们允许您从一个中央位置以自动化的方式控制许多不同的系统。虽然针对 Linux 系统有许多流行的配置管理系统,比如 Chef 和 Puppet,但这些系统通常比许多人想要或需要的要复杂得多。Ansible 是这些选项的一个很好的替代方案

ORACLE 、达梦 数据库查询指定库指定表的索引信息

在Oracle数据库中,索引是一种关键的性能优化工具,通过它可以加快数据检索速度。在本文中,我们将深入探讨如何详细查询指定表的索引信息,以及如何利用系统视图和SQL查询来获取这些信息。 索引在数据库中的重要性 索引是一种数据结构,用于加快数据库表中数据的检索速度。它类似于书籍的目录,可以帮助数据库引擎快速定位数据行,特别是在大型数据集合下,其作用尤为显著。 查询指定表的索引信息 在Orac

sublime配置node.js

1、下载Nodejs插件,下载地址为: https://github.com/tanepiper/SublimeText-Nodejs(见本人网盘) 下载zip压缩包后解压,文件名改为Nodejs 2、打开Sublime Text3,点击菜单“Perferences” =>“Browse Packages”打开“Packages”文件夹,并将第1部的Nodejs文件夹剪切进来 3

springboot集成prometheus监控

1 Maven pom.xml引入依赖 <dependency><groupId>io.prometheus</groupId><artifactId>simpleclient_spring_boot</artifactId></dependency> 2 启动类引入注解 import io.prometheus.client.spring.boot.EnablePrometheusEnd

leetcode刷题(36)——24.两交换链表中的节点

给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。 示例: 给定 1->2->3->4, 你应该返回 2->1->4->3 题解: 这个题目有2种解法,一个是比较容易想到的循环求解,另外一个是比较难想到的递归求解 解法1:循环求解 关键点在于设置一个pre节点指向链表的头节点,很多链表题目的技巧都是这样设置一个pre