هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
ansible web1 -i inventory.ini -m ansible.builtin.setup -a 'filter=ansible_distribution*'- name: Open HTTP on RHEL (firewalld)
ansible.posix.firewalld:
service: http
permanent: true
immediate: true
state: enabled
when: ansible_facts['os_family'] == "RedHat"
- name: Open HTTP on Ubuntu (UFW)
community.general.ufw:
rule: allow
port: "80"
proto: tcp
when: ansible_facts['os_family'] == "Debian"Step by step
- Facts you use most:
| Fact | Example value |
|---|---|
ansible_facts['os_family'] | RedHat, Debian |
ansible_facts['distribution'] / ['distribution_major_version'] | RedHat / 9, Ubuntu / 24 |
ansible_facts['memtotal_mb'] | 7821 |
ansible_facts['processor_vcpus'] | 4 |
ansible_facts['default_ipv4']['address'] | 10.0.1.11 |
ansible_facts['virtualization_type'] | kvm, VMware |
- **Per-OS variables instead of many
when:** — one vars file per family:
chrony_service: chronyd
firewall: firewalldchrony_service: chrony
firewall: ufw- name: Load OS-specific variables
ansible.builtin.include_vars: "{{ ansible_facts['os_family'] }}.yml"- Conditions — combine with
and/or, lists mean AND:
- name: Tune for big servers only
ansible.posix.sysctl:
name: vm.swappiness
value: "10"
when:
- ansible_facts['memtotal_mb'] > 16000
- env == "prod"- React to a command result —
register, then decide; tell Ansible what "changed" and "failed" mean:
- name: Is the app already migrated?
ansible.builtin.command: /opt/shop/bin/migrate --status
register: mig
changed_when: false
failed_when: mig.rc not in [0, 3]
- name: Run migrations
ansible.builtin.command: /opt/shop/bin/migrate --apply
when: mig.rc == 3- Speed: facts take time on every host. Turn them off for plays that do not need them, or collect only a subset:
- hosts: all
gather_facts: false(Or gather_subset: [min]; fact caching in ansible.cfg for big inventories.)
- Test the logic with
--check --diffand-v;ansible HOST -m setupshows what a host really reports.
Check that it worked
A run on a RHEL and an Ubuntu host each executes only the tasks for its OS (others show skipping), and a second run shows changed=0.
Common problems
| Problem | Cause | Fix |
|---|---|---|
'ansible_facts' is undefined / missing fact | gather_facts: false in that play. | Turn facts on, or run setup in a task first. |
| Condition always false | Wrong case/value (Redhat vs RedHat). | Print the fact with setup; copy the exact value. |
| Comparing a version as text | "10" < "9" as strings. | ansible_facts['distribution_major_version'] | int >= 9. |
Task always changed | command without changed_when. | changed_when: false for checks. |
include_vars file not found | No file for that OS family. | Add the file, or a default with first_found. |
Old style ansible_distribution vs ansible_facts[...] | Both work. | Prefer ansible_facts['...'] (clear and future-proof). |
Go further
- Command pages: ansible.
- Next how-tos: Write an Ansible role, Run one command on many servers, Encrypt secrets with Ansible Vault.
- Full guide: Ansible. At work: Ansible in daily DevOps.
- Practise: Lab: automate a web server with Ansible.
1Why load per-OS vars files instead of writing when: on every task?
The tasks stay the same; only the values (package and service names) change per OS.
2Why use changed_when: false on a check command?
A read-only check should never report a change.