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.
More on Write-an-Ansible-playbook tasks
Write a playbook that installs and starts Nginx on Ubuntu web servers.BeginnerWrite an inventory with groups, a group of groups, and per-group variables.BeginnerWrite a playbook that deploys a config file from a Jinja2 template and restarts the service only if it changed.BeginnerWrite a role structure for a Node.js app and show what goes in each folder.IntermediateWrite a task that creates multiple users with SSH keys from a list.BeginnerWrite a rolling deploy playbook: 2 servers at a time, removed from the load balancer during the update.Advanced