ansible_facts is a special variable in Ansible that contains information about the managed hosts. It is automatically gathered by Ansible during the execution of a playbook, specifically when the setup module is run. This variable provides detailed system information that can be used to make decisions in your playbooks.
Key points about ansible_facts include:
- System Information: It includes various details such as the operating system, network interfaces, CPU architecture, memory, disk space, and more.
- Dynamic Data: The facts are collected dynamically at runtime, meaning they reflect the current state of the system.
- Accessing Facts: You can access specific facts using the
ansible_factsvariable. For example, to get the operating system, you can useansible_facts['os'].
Example of accessing ansible_facts in a playbook:
- name: Gather facts and display OS information
hosts: all
tasks:
- name: Print the operating system
debug:
msg: "The operating system is {{ ansible_facts['os'] }}"
Using ansible_facts allows you to create more dynamic and adaptable playbooks based on the characteristics of the target systems.
