我在运行完整的playbook时遇到了问题,因为一些后面的plays依赖于早期plays中修改的事实,但是ansible不会在运行过程中更新facts。
当针对一个新的VPS开始整个playbook时,运行ansible somehost -m setup
:
"ansible_selinux": {
"status": "disabled"
},
我的playbook包含一个安装SELinux并重新启动服务器(同时等待ansible)的play,以及稍后使用条件语句when: ansible_selinux.status != 'disabled'
的任务。但是,即使SELinux现在已安装并强制执行(需要重新启动),系统的事实仍然显示SELinux已禁用,因此该条件失败并跳过任务。
当然,再次运行playbook可以解决问题,因为事实已更新并返回:
"ansible_selinux": {
"config_mode": "enforcing",
"mode": "enforcing",
"policyvers": 28,
"status": "enabled",
"type": "targeted"
}
有没有办法在playbook运行中刷新事实?也许诀窍是在重启后自己对ansible_selinux.status使用set_fact
?
更新: 好吧,这太容易了,感谢BruceP,我添加了这个任务以在我的SELinux play结束时获取更新的事实。
- name: SELinux - Force ansible to regather facts
setup: filter='ansible_selinux'