هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
ansible all -i inventory.ini -m ansible.builtin.ping
ansible web -i inventory.ini -b -m ansible.builtin.package -a "name=chrony state=present"
ansible-playbook -i inventory.ini chrony.yml --check --diff
ansible-playbook -i inventory.ini chrony.yml-b = become root (sudo). --check --diff shows what would change, without changing it.
Step by step
- Write the inventory — groups of servers, and the user Ansible logs in with:
[web]
web1.example.com
web2.example.com
[db]
db1.example.com ansible_host=10.0.2.21
[all:vars]
ansible_user=deploy- Test the connection (SSH keys must work, and Python must be on the servers):
ansible all -i inventory.ini -m ansible.builtin.ping
web1.example.com | SUCCESS => {"changed": false, "ping": "pong"}- One-off install with an ad-hoc command:
ansible all -i inventory.ini -b -m ansible.builtin.package -a "name=chrony state=present"
ansible all -i inventory.ini -b -m ansible.builtin.dnf -a "name=* state=latest security=true" --limit dbpackage works on RHEL and Ubuntu (it uses dnf or apt); use dnf/apt modules when you need their special options.
- Make it a playbook — repeatable, reviewable, in Git:
- name: Time sync on all servers
hosts: all
become: true
tasks:
- name: Install chrony
ansible.builtin.package:
name: chrony
state: present
- name: Start and enable chrony
ansible.builtin.service:
name: "{{ 'chronyd' if ansible_facts['os_family'] == 'RedHat' else 'chrony' }}"
state: started
enabled: trueThe service has another name on RHEL (chronyd) and Ubuntu (chrony) — the template picks the right one from the facts.
- Dry run first, then roll out in small groups:
ansible-playbook -i inventory.ini chrony.yml --check --diff
ansible-playbook -i inventory.ini chrony.yml --limit web1.example.com
ansible-playbook -i inventory.ini chrony.yml --forks 10Add serial: 5 to the play to change 5 servers at a time (rolling updates without downtime).
- Useful options:
| Option | Use |
|---|---|
--limit web | Only this group or host. |
-K | Ask for the sudo password. |
--check --diff | Show changes, change nothing. |
-v … -vvv | More output when something fails. |
--list-hosts | Which servers would be touched. |
Check that it worked
Run the playbook a second time — it must say **changed=0** on every host (idempotent):
ansible-playbook -i inventory.ini chrony.yml
PLAY RECAP
web1.example.com : ok=3 changed=0 unreachable=0 failed=0
ansible all -i inventory.ini -b -m ansible.builtin.command -a "chronyc tracking"Common problems
| Problem | Cause | Fix |
|---|---|---|
UNREACHABLE … Permission denied (publickey) | SSH key not on the server, or wrong user. | ssh deploy@web1 first; ssh-copy-id; set ansible_user. |
Missing sudo password | The user needs a password for sudo. | Add -K, or allow passwordless sudo for the deploy user. |
No package matching 'xyz' is available | Different name on that distro, or repo not enabled. | Check the name per distro; enable EPEL / the repo first. |
/usr/bin/python: not found | No Python on a minimal server. | Install Python first (cloud-init), or use the raw module once. |
| Host key prompt stops the run | New servers not in known_hosts. | Accept the keys once (ssh-keyscan), do not turn off checking. |
changed on every run | A command/shell task without a condition. | Use real modules, or creates: / changed_when:. |
Go further
- Command pages: ansible, ssh, chronyc.
- Next how-tos: Log in with SSH keys, Create a user with sudo rights, Plan and apply Terraform safely, Run one command on many servers, Write an Ansible role.
- Learn it: Ansible in daily DevOps, Ansible at work.
- Full guides: Install Ansible, chrony.
- Practise: Lab: automate a web server with Ansible.
1How do you see what a playbook would change without changing anything?
2How do you know a playbook is idempotent?
A second run shows changed=0 on all hosts.