将事实收集到事实缓存的最快方法
Fastest way to gather facts to fact cache
我正在尝试让 Ansible 与 --limit 一起工作,为此我需要有关其他主机的信息,我正在使用 fact_caching 对其进行缓存。我应该 运行 什么命令,以便它只收集所有主机上的所有事实并缓存它们,而无需 运行 执行任何任务?像 setup module 这样的东西,如果它缓存了它收集的事实,那将是完美的,但它似乎没有。
绝对最快的方法是使用 1.8 版中引入的 Ansible fact caching。它需要使用 redis 服务器来存储事实,但它可以让您在 playbook 运行之间存储事实。如文档所述:
Imagine, for instance, a very large infrastructure with thousands of hosts. Fact caching could be configured to run nightly, but configuration of a small set of servers could run ad-hoc or periodically throughout the day. With fact-caching enabled, it would not be necessary to “hit” all servers to reference variables and information about them.
--limit 的问题是它会限制你 ansible 与之交互的所有主机,所以它甚至会限制像 all
这样的特殊主机组会影响。如果您不使用 --limit 而是使用主机组,那么您可以这样做:
---
- hosts: all
tasks: []
- hosts: my_host_group
tasks:
- name: task1
...
- name: task2
...
在此示例中,第一个播放将强制收集自您指定 hosts: all
以来拥有的所有主机的事实。然后,第二个播放将在组 my_host_group
.
中的主机上执行所需的任务
这是我解决问题的方法:
1.- 在您的剧本中启用事实收集 (site.yml):
gather_facts: yes
2.- 在 ansible.cfg 上启用 facts caching:
2.1.- 选项 1 - 如果您有时间安装 redis,请使用此选项:
[defaults]
gathering = smart
fact_caching = redis
# two hours timeout
fact_caching_timeout = 7200
2.2.- 选项 2 - 现在使用它进行测试很简单但比 redis 慢:
[defaults]
gathering = smart
fact_caching = jsonfile
fact_caching_connection = /tmp/facts_cache
# two hours timeout
fact_caching_timeout = 7200
3.- 更新或创建事实缓存。为此,只需执行一项任务即可创建一个新角色(缓存更新):执行 ping。我们使用 ping 是因为它是最简单和最快的 ansible 任务,所以它将帮助我们真正快速地更新缓存:
- name: Pinging server to update facts cache
ping:
你好,
正在创建只有一行的新剧本:
- hosts: all
和 运行 它收集所有的事实,如果 fact_caching 开启,它将缓存它们以供将来使用 --limit 的剧本使用。
以下示例显示了如何使用 /usr/bin/ansible
到 运行 一个收集事实并将其存储在指定目录中的临时任务。
$ ANSIBLE_CONFIG=facts.cfg ansible r4i1 -i lab -m setup
这是facts.cfg
的内容
[defaults]
gathering = smart
fact_caching = jsonfile
fact_caching_connection = /home/lab/facts
以及示例主机的输出
$ head /home/lab/facts/r4i1
{
"ansible_all_ipv4_addresses": [
"10.10.20.89"
],
"ansible_all_ipv6_addresses": [
"fe80::46a8:42ff:fe18:141c"
],
"ansible_apparmor": {
"status": "disabled"
},
我正在尝试让 Ansible 与 --limit 一起工作,为此我需要有关其他主机的信息,我正在使用 fact_caching 对其进行缓存。我应该 运行 什么命令,以便它只收集所有主机上的所有事实并缓存它们,而无需 运行 执行任何任务?像 setup module 这样的东西,如果它缓存了它收集的事实,那将是完美的,但它似乎没有。
绝对最快的方法是使用 1.8 版中引入的 Ansible fact caching。它需要使用 redis 服务器来存储事实,但它可以让您在 playbook 运行之间存储事实。如文档所述:
Imagine, for instance, a very large infrastructure with thousands of hosts. Fact caching could be configured to run nightly, but configuration of a small set of servers could run ad-hoc or periodically throughout the day. With fact-caching enabled, it would not be necessary to “hit” all servers to reference variables and information about them.
--limit 的问题是它会限制你 ansible 与之交互的所有主机,所以它甚至会限制像 all
这样的特殊主机组会影响。如果您不使用 --limit 而是使用主机组,那么您可以这样做:
---
- hosts: all
tasks: []
- hosts: my_host_group
tasks:
- name: task1
...
- name: task2
...
在此示例中,第一个播放将强制收集自您指定 hosts: all
以来拥有的所有主机的事实。然后,第二个播放将在组 my_host_group
.
这是我解决问题的方法:
1.- 在您的剧本中启用事实收集 (site.yml):
gather_facts: yes
2.- 在 ansible.cfg 上启用 facts caching:
2.1.- 选项 1 - 如果您有时间安装 redis,请使用此选项:
[defaults]
gathering = smart
fact_caching = redis
# two hours timeout
fact_caching_timeout = 7200
2.2.- 选项 2 - 现在使用它进行测试很简单但比 redis 慢:
[defaults]
gathering = smart
fact_caching = jsonfile
fact_caching_connection = /tmp/facts_cache
# two hours timeout
fact_caching_timeout = 7200
3.- 更新或创建事实缓存。为此,只需执行一项任务即可创建一个新角色(缓存更新):执行 ping。我们使用 ping 是因为它是最简单和最快的 ansible 任务,所以它将帮助我们真正快速地更新缓存:
- name: Pinging server to update facts cache
ping:
你好,
正在创建只有一行的新剧本:
- hosts: all
和 运行 它收集所有的事实,如果 fact_caching 开启,它将缓存它们以供将来使用 --limit 的剧本使用。
以下示例显示了如何使用 /usr/bin/ansible
到 运行 一个收集事实并将其存储在指定目录中的临时任务。
$ ANSIBLE_CONFIG=facts.cfg ansible r4i1 -i lab -m setup
这是facts.cfg
[defaults]
gathering = smart
fact_caching = jsonfile
fact_caching_connection = /home/lab/facts
以及示例主机的输出
$ head /home/lab/facts/r4i1
{
"ansible_all_ipv4_addresses": [
"10.10.20.89"
],
"ansible_all_ipv6_addresses": [
"fe80::46a8:42ff:fe18:141c"
],
"ansible_apparmor": {
"status": "disabled"
},