是否可以在 Ansible 中仅收集特定事实?
Is it possible to gather only specific facts in Ansible?
在 Ansible 中,我可以使用 gather_facts: yes
来收集有关我的主机的信息。由于gather_facts
收集的资料较多,所以需要相当长的时间。就我而言,我只需要一个事实:ansible_env.TEMP
。我可以通过获取这个特定值来加快 gather_facts
进程吗?我目前的剧本:
---
- hosts: all
gather_facts: yes
tasks:
- name: Get TEMP
debug:
msg: "TEMP: {{ ansible_env.TEMP }}"
作为解决方法,我可以将 gather_facts
设置为 no
并通过 shell 命令提取值,但这感觉不像是使用 Ansible...
根据 setup
– Gathers facts about remote hosts if the parameter gather_subset
的文档提供了一个子集
restrict the additional facts collected to the given subset.
---
- hosts: localhost
become: false
gather_facts: true
gather_subset:
- "env"
- "!all"
- "!min"
tasks:
- name: Show Gathered Facts
debug:
msg: "{{ ansible_facts }}"
导致输出
TASK [Show Gathered Facts] ******
ok: [localhost] =>
msg:
env:
HISTCONTROL:
HISTSIZE:
HOME:
HOSTNAME:
KRB5CCNAME:
LANG:
LESSOPEN:
LOGNAME:
LS_COLORS:
MAIL:
PATH:
PWD:
SELINUX_LEVEL_REQUESTED:
SELINUX_ROLE_REQUESTED:
SELINUX_USE_CURRENT_RANGE:
SHELL:
SHLVL:
SSH_CLIENT:
SSH_CONNECTION:
SSH_TTY:
TERM:
TZ:
USER:
XDG_RUNTIME_DIR:
XDG_SESSION_ID:
_:
gather_subset:
- env
- '!all'
- '!min'
module_setup: true
...我只提供了键并删除了所有值。由于 lib/ansible/module_utils/facts/system/env.py.
,密钥可能会随基础架构而变化
但是,这可能仍会提供比您正在寻找的更多信息。
更多文档
- Ansible Issue #63891 - "Since Ansible 2.8, it is possible to define a list of facts modules 将由
gather_facts
模块 运行。"
- Ansible Issue #47603 - "
setup.py
gather_facts
文档不完整"
- How to access environment variable values in Python application
进一步问答
- Getting full name of the OS using Ansible facts
- Save only part of facts in Ansible
您可以使用 gather_subset,只获取环境变量:
- hosts: localhost
gather_subset: ['env','!all','!min']
tasks:
- debug:
msg: "{{ ansible_facts }}"
输出:
TASK [debug] **************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": {
"env": {
... vars here...
},
"gather_subset": [
"env",
"!all",
"!min"
],
"module_setup": true
}
}
gather_subset 允许的选项:全部、all_ipv4_addresses、all_ipv6_addresses、apparmor、体系结构、caps、chroot、cmdline、date_time、default_ipv4、default_ipv6, 设备, 分布, distribution_major_version, distribution_release, distribution_version, dns, effective_group_ids, effective_user_id, env, facter, fibre_channel_wwn 、fips、硬件、接口、is_chroot、iscsi、内核、kernel_version、本地、lsb、机器、machine_id、安装、网络、nvme、ohai、os_family、pkg_mgr、平台、处理器、processor_cores、processor_count、python、python_version、real_user_id、selinux、service_mgr、ssh_host_key_dsa_public, ssh_host_key_ecdsa_public, ssh_host_key_ed25519_public, ssh_host_key_rsa_public, ssh_host_pub_keys, ssh_pub_keys, 系统, system_capabilities, system_capabilities_enforced, 用户, user_dir、user_gecos、user_gid、user_id、user_shell、user_uid、虚拟、virtualization_role、virtualization_tech_guest、 virtualization_tech_host, virtualization_type"
在 Ansible 中,我可以使用 gather_facts: yes
来收集有关我的主机的信息。由于gather_facts
收集的资料较多,所以需要相当长的时间。就我而言,我只需要一个事实:ansible_env.TEMP
。我可以通过获取这个特定值来加快 gather_facts
进程吗?我目前的剧本:
---
- hosts: all
gather_facts: yes
tasks:
- name: Get TEMP
debug:
msg: "TEMP: {{ ansible_env.TEMP }}"
作为解决方法,我可以将 gather_facts
设置为 no
并通过 shell 命令提取值,但这感觉不像是使用 Ansible...
根据 setup
– Gathers facts about remote hosts if the parameter gather_subset
的文档提供了一个子集
restrict the additional facts collected to the given subset.
---
- hosts: localhost
become: false
gather_facts: true
gather_subset:
- "env"
- "!all"
- "!min"
tasks:
- name: Show Gathered Facts
debug:
msg: "{{ ansible_facts }}"
导致输出
TASK [Show Gathered Facts] ******
ok: [localhost] =>
msg:
env:
HISTCONTROL:
HISTSIZE:
HOME:
HOSTNAME:
KRB5CCNAME:
LANG:
LESSOPEN:
LOGNAME:
LS_COLORS:
MAIL:
PATH:
PWD:
SELINUX_LEVEL_REQUESTED:
SELINUX_ROLE_REQUESTED:
SELINUX_USE_CURRENT_RANGE:
SHELL:
SHLVL:
SSH_CLIENT:
SSH_CONNECTION:
SSH_TTY:
TERM:
TZ:
USER:
XDG_RUNTIME_DIR:
XDG_SESSION_ID:
_:
gather_subset:
- env
- '!all'
- '!min'
module_setup: true
...我只提供了键并删除了所有值。由于 lib/ansible/module_utils/facts/system/env.py.
,密钥可能会随基础架构而变化但是,这可能仍会提供比您正在寻找的更多信息。
更多文档
- Ansible Issue #63891 - "Since Ansible 2.8, it is possible to define a list of facts modules 将由
gather_facts
模块 运行。" - Ansible Issue #47603 - "
setup.py
gather_facts
文档不完整" - How to access environment variable values in Python application
进一步问答
- Getting full name of the OS using Ansible facts
- Save only part of facts in Ansible
您可以使用 gather_subset,只获取环境变量:
- hosts: localhost
gather_subset: ['env','!all','!min']
tasks:
- debug:
msg: "{{ ansible_facts }}"
输出:
TASK [debug] **************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": {
"env": {
... vars here...
},
"gather_subset": [
"env",
"!all",
"!min"
],
"module_setup": true
}
}
gather_subset 允许的选项:全部、all_ipv4_addresses、all_ipv6_addresses、apparmor、体系结构、caps、chroot、cmdline、date_time、default_ipv4、default_ipv6, 设备, 分布, distribution_major_version, distribution_release, distribution_version, dns, effective_group_ids, effective_user_id, env, facter, fibre_channel_wwn 、fips、硬件、接口、is_chroot、iscsi、内核、kernel_version、本地、lsb、机器、machine_id、安装、网络、nvme、ohai、os_family、pkg_mgr、平台、处理器、processor_cores、processor_count、python、python_version、real_user_id、selinux、service_mgr、ssh_host_key_dsa_public, ssh_host_key_ecdsa_public, ssh_host_key_ed25519_public, ssh_host_key_rsa_public, ssh_host_pub_keys, ssh_pub_keys, 系统, system_capabilities, system_capabilities_enforced, 用户, user_dir、user_gecos、user_gid、user_id、user_shell、user_uid、虚拟、virtualization_role、virtualization_tech_guest、 virtualization_tech_host, virtualization_type"