Interview Prep/Ansible/Playbook Design, Handlers and Idempotency

How do you safely edit one line in `/etc/ssh/sshd_config` and be sure SSH keeps working?

IntermediateScenarioPlaybook Design, Handlers and Idempotency
Answer

Situation read → A change that can lock you out of every server.

What I'd do →

- ansible.builtin.lineinfile:
    path: /etc/ssh/sshd_config
    regexp: '^#?PasswordAuthentication'
    line: 'PasswordAuthentication no'
    validate: '/usr/sbin/sshd -t -f %s'
    backup: true
  notify: Reload sshd

Test on one host first with --limit and keep an existing session open.

Why → validate rejects a broken file, and reload keeps current connections alive.

Interview tip → Prefer dropping a file in sshd_config.d/ where supported.

All Ansible questions Roadmap: Ansible

More on Playbook Design, Handlers and Idempotency