Interview Prep/Ansible/Modules, Idempotency and Handlers

`Unable to start service myapp: Job for myapp.service failed because the control process exited with error code.`

BeginnerTroubleshootingModules, Idempotency and Handlers
Answer

Likely causes → The application itself fails to start (bad config, port in use, missing environment), or the unit file was changed but systemd was never reloaded, so it still runs the old definition.

Diagnose →

ansible web01 -b -m command -a 'systemctl status myapp --no-pager'
ansible web01 -b -m command -a 'journalctl -u myapp -n 50 --no-pager'

Fix → Fix the application error shown in the journal. After changing a unit file, reload systemd:

- ansible.builtin.systemd:
    name: myapp
    state: started
    enabled: true
    daemon_reload: true

Prevent → Notify a handler with daemon_reload: true whenever a unit file changes, and add a health check (uri or wait_for) after starting.

All Ansible questions Roadmap: Ansible

More on Modules, Idempotency and Handlers