可以在剧本中间获取更新的事实吗?

Can ansible fetch updated facts in middle of a playbook?

我在 运行 编写完整的剧本时遇到了麻烦,因为后面的剧本所依赖的一些事实在前面的剧本中被修改了,但是 ansible 不会在 运行 中更新事实。

运行 ansible somehost -m setup 当整个剧本针对新的 VPS:

"ansible_selinux": {
    "status": "disabled"
}, 

我的 playbook 包含一个安装 SELinux 并重新启动服务器的 play(而 ansible wait_for 的),后面的任务使用条件 when: ansible_selinux.status != 'disabled'。然而,即使现在安装并执行了 SELinux(这需要重新启动),系统的事实仍然显示 SELinux 被禁用,因此条件失败并跳过任务。

运行 剧本当然有效,因为事实已更新,现在 return:

"ansible_selinux": {
    "config_mode": "enforcing", 
    "mode": "enforcing", 
    "policyvers": 28, 
    "status": "enabled", 
    "type": "targeted"
}

有什么方法可以让事实在剧本中期刷新吗?也许黑客是在 ansible_selinux.status 重启后 set_fact 我自己?

更新: 好吧,这太简单了,感谢 BruceP 我添加了这个任务来在我的 SELinux 游戏结束时获取更新的事实

- name: SELinux - Force ansible to regather facts
  setup: filter='ansible_selinux'

setup module 是 Ansible 用来收集事实的。它隐含地 运行 在 运行 你的剧本之前。您应该能够 运行 在您的剧本中手动更新您的事实。

将此添加到您的剧本中以使用设置模块更新事实。

例如,我添加了另一个 DHCP 接口,现在我想知道它有什么地址,然后执行此操作:

- name: do facts module to get latest information
  setup: