我在运行完整的剧本时遇到问题,因为后期剧本所依赖的一些事实在早期剧本中被修改了,但 ansible 不会在中途更新事实。
当整个剧本针对新的 VPS 开始时运行ansible somehost -m setup
:
"ansible_selinux": {
"status": "disabled"
},
我的剧本包含一个安装 SELinux 并重新启动服务器的重头戏(而 ansible wait_for的(,后面的任务使用条件when: ansible_selinux.status != 'disabled'
。然而,即使 SELinux 现已安装并强制执行(这需要重新启动(,系统的事实仍然显示 SELinux 被禁用,因此条件失败并跳过任务。
再次运行剧本当然有效,因为事实已更新,现在返回:
"ansible_selinux": {
"config_mode": "enforcing",
"mode": "enforcing",
"policyvers": 28,
"status": "enabled",
"type": "targeted"
}
有没有办法让事实在剧本中刷新?也许黑客是在重新启动后自己set_fact
ansible_selinux.status?
更新:好吧,这太容易了,多亏了 BruceP,我添加了这个任务以在我的 SELinux 游戏结束时获取更新的事实
- name: SELinux - Force ansible to regather facts
setup: filter='ansible_selinux'
将此添加到您的剧本中,以使用设置模块更新事实。
例如,我添加了另一个带有DHCP的接口,现在我想知道它有什么地址,然后执行以下操作:
- name: do facts module to get latest information
setup:
设置模块是 Ansible 用来收集事实的模块。 它会在运行剧本之前隐式运行它。 您应该能够在剧本中手动运行它以更新您的事实。