LOGBOOK

HELP

Quiz Entry - updated: 2026.11.12

The lab enables SSL with shell: a2enmod ssl and the site with shell: a2ensite, both with notify: Restart web service. What happens on every later run, and how do you make these tasks report changed only when something was really enabled?

shell always reports changed, so Apache is restarted on every run even when nothing changed. Use creates: with the file the command produces, or a changed_when on the exact "already enabled" line for that module or site.

a2enmod ssl is safe to run twice: the second time it prints Module ssl already enabled and exits 0. But Ansible doesn't read that output. A shell task that exits 0 counts as changed, the handler is notified, and every run of the playbook restarts the web server. That is unnecessary downtime, and the recap never reaches changed=0.

Two fixes:

- name: Aktivieren Sie mod_ssl für SSL-Verschlüsselung
  ansible.builtin.shell:
    cmd: a2enmod ssl
    creates: /etc/apache2/mods-enabled/ssl.load
  notify: Restart web service

creates skips the command (reports ok) when the file already exists. a2enmod creates that symlink, so the task runs only the first time.

  register: ssl_mod
  changed_when: "'Module ssl already enabled' not in ssl_mod.stdout"

Use the full line. On the very first run, a2enmod ssl also prints Module mime already enabled for a dependency, so a test for just 'already enabled' would report ok on the run that actually enabled SSL, and the restart would never happen. a2ensite works the same way (Site itia-lhe-ssl already enabled; the symlink is in /etc/apache2/sites-enabled/). The module community.general.apache2_module does this properly, without a shell.

Go deeper:

From Quiz: ITIA / Ansible Task Control: Loops, Conditionals, Handlers and Error Handling | Updated: Nov 12, 2026