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

كيف 163IaC · Ansible

How to write an Ansible role

A playbook with 200 tasks for Nginx, firewall, users and monitoring is hard to read and impossible to reuse. A role packs one job (for example "nginx web server") with its tasks, templates, handlers and default settings in a fixed folder layout — and any playbook can use it with its own values. This how-to creates a role, fills it, uses it, and tests it.

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

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

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

163.1

The quick answer

ansible-galaxy role init roles/nginx
tree roles/nginx -L 1
roles/nginx
├── defaults
├── handlers
├── meta
├── tasks
├── templates
└── vars
site.yml
- hosts: web
  become: true
  roles:
    - role: nginx
      vars:
        nginx_server_name: shop.example.com
ansible-playbook -i inventory.ini site.yml --check --diff

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

163.2

Step by step

  1. The folders (created by role init):
FolderHolds
tasks/main.ymlThe steps.
handlers/main.ymlActions run on change (reload nginx).
templates/Jinja2 templates (nginx.conf.j2).
defaults/main.ymlVariables users may override (lowest priority).
vars/main.ymlInternal constants (high priority — not for users).
meta/main.ymlAuthor, supported platforms, dependencies.
  1. Tasks:
roles/nginx/tasks/main.yml
- 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
  1. Defaults and a template:
roles/nginx/defaults/main.yml
nginx_server_name: localhost
nginx_upstream: 127.0.0.1:8000
roles/nginx/templates/site.conf.j2
server {
    listen 80;
    server_name {{ nginx_server_name }};
    location / {
        proxy_pass http://{{ nginx_upstream }};
    }
}
  1. Handler — runs once at the end, only if something notified it:
roles/nginx/handlers/main.yml
- name: Reload nginx
  ansible.builtin.service:
    name: nginx
    state: reloaded
  1. Use roles from others — pin versions in requirements.yml:
requirements.yml
roles:
  - name: geerlingguy.docker
    version: 7.4.1
collections:
  - name: ansible.posix
    version: ">=1.5.0"
ansible-galaxy install -r requirements.yml
  1. Quality: ansible-lint roles/nginx, and test the role in a container or VM with Molecule before it touches servers.

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

163.3

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.

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

163.4

Common problems

ProblemCauseFix
the role 'nginx' was not foundRole not in roles/ next to the playbook or roles_path.Put it in ./roles/ or set roles_path in ansible.cfg.
Your value is ignoredSet in vars/main.yml of the role (high priority).User settings belong in defaults/main.yml.
Handler never runsTask did not change, or wrong handler name.notify must match the handler name exactly.
changed on every runcommand/shell tasks or templates with time stamps.Use modules; avoid changing content in templates.
Different versions of a Galaxy role on each laptopNot pinned.requirements.yml with versions; install in CI.
Template errorsUndefined variable.Add a default in defaults/main.yml.

163.5

Go further

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.

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

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