You need to enable JavaScript to run this app. Ansible set_fact设置的facts无法跨运行持久化及条件任务问题
优惠活动
大模型
产品
解决方案
定价
更多

Ansible set_fact设置的facts无法跨运行持久化及条件任务问题

问题1:set_fact创建的fact未出现在setup模块输出中

我想通过set_fact动态创建可在后续运行中复用的facts,编写了如下Ansible playbook:

- name: record updated mysql root password
  hosts: all
  tasks:
    - name: setting a new fact
      set_fact:
        pwd_updated: True
        cacheable: yes

该playbook运行正常,但执行命令ansible all -m setup -i inventory > ~/ansible_localhost.facts后,在生成的文件里找不到pwd_updated这个fact,请问操作是否有误?


问题2:优化后的playbook中debug任务始终被跳过

按照@VladimirBotka的建议处理后上述问题解决,随后我优化了playbook:希望仅当mysql_root_pwd_updated未定义时才设置fact,且仅当该fact已定义时执行debug任务,优化后的代码如下:

- name: record updated mysql root password
  hosts: all
  tasks:
    - name: setting a new fact
      set_fact:
        pwd_updated: True
        cacheable: yes
      when: mysql_root_pwd_updated is undefined
    - debug: 
        var: pwd_updated
      when: mysql_root_pwd_updated is defined

但第一次和第二次运行结果完全相同,debug任务始终被跳过,请问此次操作的问题出在哪里?


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 15:25:09