Configuring the gather_facts option
Disabling fact gathering
To disable the fact gathering process, you can set the gather_facts
option to false
in your playbook:
- hosts: all
gather_facts: false
tasks:
## Your tasks here
By setting gather_facts: false
, Ansible will skip the fact gathering step and proceed directly to the task execution.
Selective fact gathering
In some cases, you may only need a subset of the available facts. Ansible allows you to selectively gather facts by using the gather_subset
option:
- hosts: all
gather_subset:
- "!all"
- "minimal"
tasks:
## Your tasks here
In the example above, Ansible will only gather the "minimal" set of facts, which includes basic information about the target host, such as the operating system and architecture.
You can customize the gather_subset
option to include or exclude specific fact groups, such as hardware
, network
, or virtual
.
Dynamic fact gathering
Ansible also supports dynamic fact gathering, where the facts are gathered only when they are needed in the playbook tasks. This can be useful to reduce the overall execution time of your playbook.
To enable dynamic fact gathering, you can use the when
clause to conditionally gather facts:
- hosts: all
tasks:
- name: Print the operating system
debug:
msg: "The target host is running {{ ansible_facts['distribution'] }} {{ ansible_facts['distribution_version'] }}"
when: ansible_facts['distribution'] is defined
In this example, the when
clause checks if the ansible_facts['distribution']
variable is defined before attempting to use it in the debug
task. This ensures that the facts are only gathered when they are needed.
By understanding these options, you can optimize the fact gathering process in your Ansible playbooks to suit your specific requirements.