هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
ansible-galaxy role init roles/nginx
tree roles/nginx -L 1
roles/nginx
├── defaults
├── handlers
├── meta
├── tasks
├── templates
└── vars- hosts: web
become: true
roles:
- role: nginx
vars:
nginx_server_name: shop.example.comansible-playbook -i inventory.ini site.yml --check --diffStep by step
- The folders (created by
role init):
| Folder | Holds |
|---|---|
tasks/main.yml | The steps. |
handlers/main.yml | Actions run on change (reload nginx). |
templates/ | Jinja2 templates (nginx.conf.j2). |
defaults/main.yml | Variables users may override (lowest priority). |
vars/main.yml | Internal constants (high priority — not for users). |
meta/main.yml | Author, supported platforms, dependencies. |
- Tasks:
- name: Install nginx
ansible.builtin.package:
name: nginx
state: present
- name: Site config
ansible.builtin.template:
src: site.conf.j2
dest: /etc/nginx/conf.d/site.conf
mode: "0644"
notify: Reload nginx
- name: Start and enable nginx
ansible.builtin.service:
name: nginx
state: started
enabled: true- Defaults and a template:
nginx_server_name: localhost
nginx_upstream: 127.0.0.1:8000server {
listen 80;
server_name {{ nginx_server_name }};
location / {
proxy_pass http://{{ nginx_upstream }};
}
}- Handler — runs once at the end, only if something notified it:
- name: Reload nginx
ansible.builtin.service:
name: nginx
state: reloaded- Use roles from others — pin versions in
requirements.yml:
roles:
- name: geerlingguy.docker
version: 7.4.1
collections:
- name: ansible.posix
version: ">=1.5.0"ansible-galaxy install -r requirements.yml- Quality:
ansible-lint roles/nginx, and test the role in a container or VM with Molecule before it touches servers.
Check that it worked
The playbook runs, the second run shows changed=0, the handler runs only when the template changed, and curl -sI http://web1 answers through the proxy.
Common problems
| Problem | Cause | Fix |
|---|---|---|
the role 'nginx' was not found | Role not in roles/ next to the playbook or roles_path. | Put it in ./roles/ or set roles_path in ansible.cfg. |
| Your value is ignored | Set in vars/main.yml of the role (high priority). | User settings belong in defaults/main.yml. |
| Handler never runs | Task did not change, or wrong handler name. | notify must match the handler name exactly. |
changed on every run | command/shell tasks or templates with time stamps. | Use modules; avoid changing content in templates. |
| Different versions of a Galaxy role on each laptop | Not pinned. | requirements.yml with versions; install in CI. |
| Template errors | Undefined variable. | Add a default in defaults/main.yml. |
Go further
- Command pages: ansible.
- Next how-tos: Encrypt secrets with Ansible Vault, Use facts and conditions in Ansible, Install a package on many servers.
- Full guide: Ansible: playbooks and roles. At work: Ansible.
- Practise: Lab: automate a web server with Ansible — inventory, playbook, role, handler, vault and check mode.
1Where should variables go that users of a role may change?
In defaults/main.yml — the lowest priority, easy to override.
2When does a handler run?
Once at the end of the play, only if a task that notified it reported changed.