متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 47IaC · Ansible

How to install a package on many servers with Ansible

Installing chrony on one server is one command — on 40 servers it is a job for Ansible. Ansible connects over SSH, needs no agent, and makes the change only where it is missing (idempotent). This how-to builds a small inventory, tests the connection, installs a package with one ad-hoc command, then turns it into a playbook with a service and a dry run, so you can repeat it safely and keep it in Git.

  • 5 الدروس
  • 5 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

47.1

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.

الأوامر في هذا الدرس

47.2

Step by step

  1. Write the inventory — groups of servers, and the user Ansible logs in with:
inventory.ini
[web]
web1.example.com
web2.example.com
 
[db]
db1.example.com ansible_host=10.0.2.21
 
[all:vars]
ansible_user=deploy
  1. 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"}
  1. 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 db

package works on RHEL and Ubuntu (it uses dnf or apt); use dnf/apt modules when you need their special options.

  1. Make it a playbook — repeatable, reviewable, in Git:
chrony.yml
- 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: true

The service has another name on RHEL (chronyd) and Ubuntu (chrony) — the template picks the right one from the facts.

  1. 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 10

Add serial: 5 to the play to change 5 servers at a time (rolling updates without downtime).

  1. Useful options:
OptionUse
--limit webOnly this group or host.
-KAsk for the sudo password.
--check --diffShow changes, change nothing.
-v … -vvvMore output when something fails.
--list-hostsWhich servers would be touched.

47.3

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"

الأوامر في هذا الدرس

47.4

Common problems

ProblemCauseFix
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 passwordThe user needs a password for sudo.Add -K, or allow passwordless sudo for the deploy user.
No package matching 'xyz' is availableDifferent name on that distro, or repo not enabled.Check the name per distro; enable EPEL / the repo first.
/usr/bin/python: not foundNo Python on a minimal server.Install Python first (cloud-init), or use the raw module once.
Host key prompt stops the runNew servers not in known_hosts.Accept the keys once (ssh-keyscan), do not turn off checking.
changed on every runA command/shell task without a condition.Use real modules, or creates: / changed_when:.

47.5

Go further

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.

كل الأوامر في هذا الفصل