You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ansible 2.9.23 playbook找不到ansible_uptime_seconds变量如何处理

问题原因

Ansible 2.9版本中,若inject_facts_as_vars配置项处于关闭状态,收集到的系统事实不会生成带ansible_前缀的顶层变量,只会存入ansible_facts字典,且字典内字段名不带ansible_前缀。单独执行setup模块时默认返回扁平化带前缀的变量,因此会出现两边表现不一致的情况。

解决方法

方法一:直接从ansible_facts字典引用(推荐)

无需修改全局配置,直接调用字典内不带前缀的字段即可,示例写法:

- name: 打印系统运行时长
  ansible.builtin.debug:
    var: ansible_facts.uptime_seconds

方法二:开启顶层事实变量注入

如果需要沿用ansible_uptime_seconds的写法,可通过以下方式开启配置:

  1. 编辑控制节点ansible.cfg,在[defaults]段添加配置:
[defaults]
inject_facts_as_vars = True
  1. 临时生效可在运行playbook时指定环境变量:
ANSIBLE_INJECT_FACTS_AS_VARS=1 ansible-playbook your_playbook.yml

额外排查方案

如果上述操作后仍无法获取,可在playbook中添加手动收集事实的任务,指定收集最小基础子集:

- name: 手动收集基础系统事实
  ansible.builtin.setup:
    gather_subset:
      - '!all'
      - min

内容的提问来源于stack exchange,提问作者sfgroups

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.10 05:06:01