在所有主机上强制收集事实 [英] Force fact-gathering on all hosts

查看:38
本文介绍了在所有主机上强制收集事实的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正坐在一个相当复杂的 Ansible 项目前面,我们用它来设置我们的本地开发环境(多个 VM),并且有一个角色使用 Ansible 收集的事实来设置 /每个 VM 上的 etc/hosts 文件.不幸的是,当您只想为一台主机运行剧本时(使用 -limit 参数),来自其他主机的事实(显然)丢失了.

I'm sitting in front of a fairly complex Ansible project that we're using to set up our local development environments (multiple VMs) and there's one role that uses the facts gathered by Ansible to set up the /etc/hosts file on every VM. Unfortunately, when you want to run the playbook for one host only (using the -limit parameter) the facts from the other hosts are (obviously) missing.

有没有办法强制 Ansible 收集所有主机上的事实,即使您将剧本限制在一个特定主机上?

Is there a way to force Ansible to gather facts on all hosts, even if you limit the playbook to one specific host?

我们尝试在剧本中添加一个剧本以从所有主机收集事实,但当然这也仅限于 -limit 参数给出的一个主机.如果有一种方法可以强制此剧在其他剧之前在所有主机上运行,​​那将是完美的.

We tried to add a play to the playbook to gather facts from all hosts, but of course that also gets limited to the one host given by the -limit parameter. If there'd be a way to force this play to run on all hosts before the other plays, that would be perfect.

我在谷歌上搜索了一下,找到了使用 redis 进行事实缓存的解决方案,但由于我们的剧本是在本地使用的,我想避免对其他软件的需求.我知道,这没什么大不了的,但我只是在寻找一个更清洁"的、仅适用于 Ansible 的解决方案,并且想知道这是否存在.

I've googled a bit and found the solution with fact caching with redis, but since our playbook is used locally, I wanted to avoid the need for additional software. I know, it's not a big deal, but I was just looking for a "cleaner", Ansible-only solution and was wondering, if that would exist.

推荐答案

Ansible 版本 2 引入了一种干净、官方的方法来使用委托事实来做到这一点(请参阅:http://docs.ansible.com/ansible/latest/playbooks_delegation.html#delegated-facts).

Ansible version 2 introduced a clean, official way to do this using delegated facts (see: http://docs.ansible.com/ansible/latest/playbooks_delegation.html#delegated-facts).

when: hostvars[item]['ansible_default_ipv4'] is not defined 是一项检查,以确保您不检查您已经知道的主机中的事实

when: hostvars[item]['ansible_default_ipv4'] is not defined is a check to ensure you don't check for facts in a host you already know the facts about

---
# This play will still work as intended if called with --limit "<host>" or --tags "some_tag"

- name: Hostfile generation
  hosts: all
  become: true

  pre_tasks:
    - name: Gather facts from ALL hosts (regardless of limit or tags)
      setup:
      delegate_to: "{{ item }}"
      delegate_facts: True
      when: hostvars[item]['ansible_default_ipv4'] is not defined
      with_items: "{{ groups['all'] }}"

  tasks:
    - template:
        src: "templates/hosts.j2"
        dest: "/etc/hosts"
      tags:
        - hostfile

     ...

这篇关于在所有主机上强制收集事实的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆