Ansible能在playbook执行过程中获取更新后的事实吗?

20

我在运行完整的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'
2个回答

32

将以下内容添加到你的剧本中,以使用setup模块更新事实。

例如,我添加了另一个使用DHCP的接口,现在我想知道它的地址,然后执行以下操作:

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

8

setup module是Ansible用于收集信息的工具。Ansible在运行您的playbook之前会隐式地运行它。您可以在playbook中手动运行它来更新您的信息。


1
更新链接:https://docs.ansible.com/ansible/latest/collections/ansible/builtin/setup_module.html - Glenn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接