# Hosts के बारे में Facts — Ansible: Playbooks से Servers का Automation

Source: https://www.geekswithgeeks.com/hi/ansible/var-facts

> हर host के operating system और hardware के अनुसार tasks ढालने के लिए gathered facts उपयोग करें।

## Ansible host से उसके बारे में पूछता है

Play की शुरुआत में Ansible **`setup` module** चलाता है और `ansible_os_family`, `ansible_distribution`, memory, CPUs और network interfaces जैसे **facts** इकट्ठा करता है। आप उन्हें tasks और templates में उपयोग कर सकते हैं, जैसे Debian पर `apt` और Red Hat पर `dnf` चुनना। Facts इकट्ठा करने में थोड़ा समय लगता है, इसलिए ज़रूरत न हो तो `gather_facts: false` रखें (जैसा उदाहरण playbook करती है)।

## एक fact पूछना, चलाकर

मैंने यह इस मशीन पर चलाया; यह Debian-परिवार का system है।

```bash
ansible -i inventory.ini web1 -m ansible.builtin.setup -a "filter=ansible_os_family"
```

Output:

```
"ansible_os_family": "Debian",
```

## Task में fact उपयोग करना

Fact से सही package manager चुनना एक ही playbook को दोनों परिवारों पर चलाए रखता है। (उदाहरण; यहाँ चलाया नहीं गया।)

```yaml
- name: Install nginx (Debian family)
  ansible.builtin.apt:
    name: nginx
    state: present
  when: ansible_os_family == "Debian"

- name: Install nginx (Red Hat family)
  ansible.builtin.dnf:
    name: nginx
    state: present
  when: ansible_os_family == "RedHat"
```

**Quiz:** कौन-सा module facts इकट्ठा करता है?

- [x] setup
- [ ] ping
- [ ] copy
- [ ] debug

*Answer:* setup. gather_facts false न हो तो setup module play की शुरुआत में facts इकट्ठा करता है।
