Interview Prep/Ansible/Ad-hoc Commands, Modules and Idempotency

What are the common `lineinfile` mistakes?

IntermediateConceptAd-hoc Commands, Modules and Idempotency
Answer
  • A regexp that is too loose replaces the wrong line, or one that does not match makes the module append a duplicate on each run.
  • No regexp: only the exact line is checked, so changing the value later appends a second line.
  • Without backup: true or validate:, a bad edit to sshd_config or sudoers locks you out.
  • state: present with insertafter only applies when no match is found.
- ansible.builtin.lineinfile:
    path: /etc/ssh/sshd_config
    regexp: '^#?PermitRootLogin'
    line: 'PermitRootLogin no'
    validate: '/usr/sbin/sshd -t -f %s'
All Ansible questions Roadmap: Ansible

More on Ad-hoc Commands, Modules and Idempotency