textfile 收集器与 Ansible facts 结合实现资产标签

解读

在国内中大型互联网、金融、运营商及政务云环境中,CMDB 与监控系统必须“同源同标”:监控系统里的“主机-标签”必须和 CMDB 里的“资产-标签”完全一致,否则告警路由、故障自愈、成本分摊全部失真。
textfile 收集器是 Node Exporter 的“外挂”机制,允许运维把任意 KV 以 Prometheus 格式写进 *.prom 文件,Node Exporter 会原样暴露给 Prometheus;Ansible facts 是 Ansible 在 setup 模块里采集到的 800+ 维度的主机原生信息。
把两者结合,本质上是“用 Ansible 做权威数据源,用 textfile 做监控侧标签注入”,从而做到一次采集、多处消费,既保证标签口径统一,又避免监控 agent 重复采集。

知识点

  1. textfile 收集器

    • 启动参数 --collector.textfile.directory=/var/lib/node_exporter/textfile
    • 文件命名 *.prom,UTF-8,Prometheus 文本格式,以 # HELP # TYPE 开头,支持 TYPE:gauge、counter、untyped
    • 采集原子性:先写 tmp,再 mv,保证 Node Exporter 不会读到半文件
    • 采集周期:Node Exporter 只在做 HTTP pull 时才读磁盘,因此文件更新频率 ≤ Prometheus scrape_interval 即可
  2. Ansible facts 子集

    • 硬件:ansible_product_uuid、ansible_memtotal_mb、ansible_processor_vcpus
    • 网络:ansible_default_ipv4.address、ansible_all_ipv4_addresses
    • 云厂商:ansible_system_vendor、ansible_product_name(可区分 AWS、阿里云、腾讯云、华为云、物理机)
    • 业务:ansible_local(自定义 json,在 /etc/ansible/facts.d/*.fact 里维护)
  3. 标签规范(国内落地常用)

    • 必需维度:env、app、team、region、az、role、owner
    • 云维度:cloud、instance_charge_type(预付费/按量)
    • 合规维度:security_zone、backup_policy、compliance_level
  4. 文件落盘路径与权限

    • /var/lib/node_exporter/textfile 属主 root:node_exporter,权限 755,防止普通用户篡改指标
    • SELinux 需打标签 node_exporter_var_lib_t
  5. 指标命名

    • 资产标签用 node_meta 作为 metric 名,TYPE=gauge,value=1,所有标签放在 label 里;Prometheus 对 value 恒定 1 的 gauge 会自动压缩存储,几乎不占空间
  6. 更新触发方式

    • 方式 A:Ansible 每次执行 playbook 后在 handler 里本地执行 shell 写文件
    • 方式 B:使用 ansible-pull 或 AWX/Tower 定时模板,每 30 min 全量刷新
    • 方式 C:使用 systemd timer,调用 ansible-inventory --host 生成 json,再 jq 渲染模板
  7. 灰度与回滚

    • 文件带时间戳注释 # Generated at 2025-06-05 18:00:00 CST
    • 旧文件保留 5 个版本,Node Exporter 只认 *.prom,回滚只需 mv
  8. 监控自监控

    • 对 node_exporter 本身做 node_textfile_mtime_seconds 指标,Prometheus 配置
      time() - node_textfile_mtime_seconds > 7200 报警“资产标签 2 小时未更新”

答案

  1. 在 Ansible 控制节点创建 roles/node_meta/tasks/main.yml
- name: gather only smart facts
  setup:
    gather_subset:
      - hardware
      - network
      - virtual
      - "!min"

- name: template node_meta.prom
  template:
    src: node_meta.prom.j2
    dest: /tmp/node_meta.prom.{{ ansible_date_time.epoch }}
    mode: 0644
  delegate_to: "{{ inventory_hostname }}"

- name: atomic move
  shell: mv /tmp/node_meta.prom.{{ ansible_date_time.epoch }} /var/lib/node_exporter/textfile/node_meta.prom
  delegate_to: "{{ inventory_hostname }}"
  notify: restart node_exporter
  1. 创建 templates/node_meta.prom.j2
# HELP node_meta Node static metadata from Ansible facts
# TYPE node_meta gauge
node_meta{env="{{ env | default('prod') }}",app="{{ app }}",team="{{ team }}",region="{{ region }}",az="{{ az }}",role="{{ role }}",owner="{{ owner }}",cloud="{{ ansible_system_vendor | lower }}",instance_id="{{ ansible_product_uuid | lower }}",mem_gb="{{ ansible_memtotal_mb // 1024 }}",cpu="{{ ansible_processor_vcpus }}"} 1
  1. playbook 调用
- hosts: all
  vars:
    env:    "{{ hostvars[inventory_hostname]['env'] }}"
    app:    "{{ hostvars[inventory_hostname]['app'] }}"
    team:   "{{ hostvars[inventory_hostname]['team'] }}"
    region: "{{ hostvars[inventory_hostname]['region'] }}"
    az:     "{{ hostvars[inventory_hostname]['az'] }}"
    role:   "{{ hostvars[inventory_hostname]['role'] }}"
    owner:  "{{ hostvars[inventory_hostname]['owner'] }}"
  roles:
    - node_meta
  1. Prometheus 侧使用
    up{job="node"} * on(instance) group_left(env,app,team,owner) node_meta
    即可在告警规则里直接引用 env、team 等标签做路由,无需修改 Prometheus 配置。

拓展思考

  1. 多云场景下 ansible_product_uuid 可能重复(如阿里云 lightwight UUID),可拼接 cloud + instance_id 作为唯一键;
  2. 容器化 Node Exporter 时,把 /var/lib/node_exporter/textfile 挂到宿主机目录,防止容器重建丢失标签;
  3. 对大规模集群(>10k 节点),Ansible 一次执行耗时高,可改用 event-driven:监听 CMDB 变更消息,只更新变更主机;
  4. 如果公司用 Consul 做服务发现,可把 node_meta 直接注册到 Consul KV,Prometheus 通过 consul_sd_configs 重新标记,实现“零文件”方案;
  5. 国内等保 2.0 要求“资产责任人”字段必须可审计,可在模板里加入 # Generated by {{ ansible_user_id }} at {{ ansible_date_time.iso8601 }},满足审计溯源。