stackprotector
stackprotector

Reputation: 13432

Is it possible to gather only specific facts in Ansible?

In Ansible, I can use gather_facts: yes to collect info about my hosts. As gather_facts collects a lot of information, it takes quite a while. In my case, I only need one fact: ansible_env.TEMP. Can I speed up the gather_facts process by just fetching this specific value? My current playbook:

---
- hosts: all
  gather_facts: yes
  tasks:
    - name: Get TEMP
      debug:
        msg: "TEMP: {{ ansible_env.TEMP }}"

As a workaround, I can set gather_facts to no and extract the value via a shell command, but that just does not feel like using Ansible...

Upvotes: 4

Views: 10397

Answers (2)

U880D
U880D

Reputation: 12017

According documentation of setup – Gathers facts about remote hosts if the parameter gather_subset with a subset is supplied

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 }}"

resulting into an output of

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

... I've provided keys only and removed all values. The keys may change with the infrastructure because of lib/ansible/module_utils/facts/system/env.py.

However, this will probably still provide more information then you are looking for.

Further Documentation

Further Q&A

Upvotes: 3

jeroenflvr
jeroenflvr

Reputation: 332

You can use gather_subset, to only grab env vars:

- hosts: localhost
  gather_subset: ['env','!all','!min']

  tasks:
    - debug:
        msg: "{{ ansible_facts }}"

output:

TASK [debug] **************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": {
        "env": {
                ... vars here...
        },
        "gather_subset": [
            "env",
            "!all",
            "!min"
        ],
        "module_setup": true
    }
}

gather_subset options allowed: all, all_ipv4_addresses, all_ipv6_addresses, apparmor, architecture, caps, chroot, cmdline, date_time, default_ipv4, default_ipv6, devices, distribution, distribution_major_version, distribution_release, distribution_version, dns, effective_group_ids, effective_user_id, env, facter, fibre_channel_wwn, fips, hardware, interfaces, is_chroot, iscsi, kernel, kernel_version, local, lsb, machine, machine_id, mounts, network, nvme, ohai, os_family, pkg_mgr, platform, processor, 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, system_capabilities, system_capabilities_enforced, user, user_dir, user_gecos, user_gid, user_id, user_shell, user_uid, virtual, virtualization_role, virtualization_tech_guest, virtualization_tech_host, virtualization_type"

Upvotes: 2

Related Questions