--- # Restart a systemd service on target hosts # # Usage examples: # # Restart unbound on all DNS servers # ansible-playbook restart-service.yml -l role_dns -e service=unbound # # # Restart nginx on a specific host # ansible-playbook restart-service.yml -l http-proxy.home.2rjus.net -e service=nginx # # # Restart promtail on all prod hosts # ansible-playbook restart-service.yml -l tier_prod -e service=promtail - name: Restart systemd service hosts: all gather_facts: false tasks: - name: Validate service name provided ansible.builtin.fail: msg: | The 'service' variable is required. Usage: ansible-playbook restart-service.yml -l -e service= Examples: -e service=nginx -e service=unbound -e service=promtail when: service is not defined run_once: true - name: Restart {{ service }} ansible.builtin.systemd: name: "{{ service }}" state: restarted register: restart_result - name: Display result ansible.builtin.debug: msg: "Service {{ service }} restarted on {{ inventory_hostname }}"