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

Fix this playbook: the handler never runs, and the loop uses deprecated syntax.

IntermediateHands-onWrite-an-Ansible-playbook tasks
Answer

Broken:

tasks:
  - name: copy config
    copy: src=app.conf dest=/etc/app/app.conf
    notify: restart App
  - name: install pkgs
    apt: name={{ item }} state=present
    with_items:
      - git
      - curl
handlers:
  - name: restart app
    service: name=app state=restarted

Fixed:

tasks:
  - name: Copy config
    ansible.builtin.copy:
      src: app.conf
      dest: /etc/app/app.conf
    notify: Restart app
  - name: Install packages
    ansible.builtin.apt:
      name: [git, curl]
      state: present
handlers:
  - name: Restart app
    ansible.builtin.service:
      name: app
      state: restarted

Key trick → Handler names are matched exactly, case included, so restart App didn't match restart app. Passing a list to apt installs all packages in one transaction, which is faster than a loop.

All Real-World Interview questions Roadmap: Interview readiness

More on Write-an-Ansible-playbook tasks