Interview Prep/Real-World Interview/Write-an-Ansible-playbook tasks

Write a playbook that uses tags so you can run only the config part.

BeginnerHands-onWrite-an-Ansible-playbook tasks
Answer
- hosts: web
  become: true
  tasks:
    - name: Install nginx
      ansible.builtin.apt: { name: nginx, state: present }
      tags: [install]
    - name: Deploy config
      ansible.builtin.template: { src: nginx.conf.j2, dest: /etc/nginx/nginx.conf }
      tags: [config]
      notify: Reload nginx
  handlers:
    - name: Reload nginx
      ansible.builtin.service: { name: nginx, state: reloaded }
ansible-playbook site.yml --tags config
ansible-playbook site.yml --list-tags

Key trick → Tags let you rerun one slice of a big playbook in seconds. Handlers notified by tagged tasks still run.

All Real-World Interview questions Roadmap: Interview readiness

More on Write-an-Ansible-playbook tasks