如何过滤剧本中的收集事实? [英] How to filter gathering facts inside a playbook?

查看:22
本文介绍了如何过滤剧本中的收集事实?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在担任一个只需要收集一个事实的角色.

I'm working on a role that only needs to gather a single fact.

性能是一个问题,我知道收集事实很耗时.

Performance it's a concern and I know that gathering facts it's time-consuming.

我正在寻找某种方法来过滤剧本中的 gather_facts,这将允许我只收集所需的事实.

I'm looking for some way to filter gather_facts inside a playbook, this will allow me to gather only the required facts.

这可以使用 setup 核心模块:

This is possible using the setup core module:

ansible -m setup -a 'filter=ansible_hostname' my_host

10.200.0.127 | success >> {
    "ansible_facts": {
        "ansible_hostname": "my_host"
    },
    "changed": false
}

可以在剧本中使用此功能吗?像这样吗?

It's possible to use this feature inside the playbook? Something like this?

- hosts: all
  sudo: yes
  gather_facts: True
    filter: "filter=ansible_*"

PS:上面的代码抛出语法异常.

PS: The code above throws syntax exception.

编辑 1:如果有人需要获取主机名,还有另一个有用的变量 inventory_hostname.

EDIT 1: If someone needs to get hostname there's also another useful variable inventory_hostname.

推荐答案

是的,这是可能的,但不是收集事实的默认行为.将 gather_facts 设置为 true 后,只需调用 设置模块作为该剧的首要任务.这样你就无法参数化 setup 模块调用.

Yes, that's possible, but not in the default behavior of gathering facts. Having set gather_facts to true simply calls the setup module as very first task of the play. This way you do not have any way to parameterize the setup module call.

但是您可以禁用默认行为并使用过滤器参数自行调用设置.

But you can disable the default behavior and call setup yourself with the filter parameter.

- hosts: all
  sudo: yes
  gather_facts: False
  tasks:
   - setup:
       filter: ansible_*

由于您正在处理一个角色并且可能不想在您的角色中进行此设置调用,您可以使用 pre_tasks.

Since you're working on a role and might not want to have this setup call in your role, you could make use of pre_tasks.

- hosts: all
  sudo: yes
  gather_facts: False
  pre_tasks:
   - setup:
       filter: ansible_*
  roles:
   - your_role_here

这篇关于如何过滤剧本中的收集事实?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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