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
相关产品推荐
相关产品推荐

