What do the lineinfile and apache2_module modules do?
lineinfile makes sure one particular line is present in a text file, or replaces a line matched by a regular expression. apache2_module (from community.general) enables or disables an Apache module, such as rewrite.
lineinfile is for surgical edits to an existing config file:
- name: apache2 listen on port 8081
lineinfile:
dest: /etc/apache2/ports.conf
regexp: "^Listen 80"
line: "Listen 8081"
It searches for a line matching regexp. If found, that line is replaced by line; if not, line is added at the end. Either way the result is the same file, which is why it is idempotent where echo "..." >> file is not. (dest is the old name of the option; current docs call it path.)
apache2_module wraps Debian's a2enmod/a2dismod:
- name: enable mod_rewrite
apache2_module:
name: rewrite
state: present
Its full name is community.general.apache2_module: it is not part of ansible.builtin, so it needs the community.general collection. ansible-doc lineinfile and ansible-doc apache2_module show where each comes from.
For anything more than a line or two, a template of the whole file (template module) is usually cleaner than many lineinfile edits.
Go deeper:
lineinfile module (official docs) —
regexp,insertafter,backrefsand thepath/destalias.community.general.apache2_module (official docs) — options including
forcefor default modules on Debian.