Missing sudo password when using agent-based authentication for sudo

This is more of a sudo problem than an Ansible one.

When you ssh to a server, you create an interactive tty session with that remote host. When you run Ansible against your inventory, Ansible creates non-interactive tty sessions with the remote hosts.

Sudo has special rules for environment variables, when they are preserved, and when they are not. If you become root, should root use the non-root user’s ENV or load its own? Should it reset the ENV and load defaults? It has rules defined for various scenarios, but has arguments to work around those when exceptions arrise.

In your case, your SSH_AUTH_SOCK environment variable is preserved in your interactive session, but not in Ansible’s non-interactive session. Passing become_flags=--preserve-env=SSH_AUTH_SOCK to become_method=sudo then instructs sudo that it needs to preserve this ENV for the session, when it normally wouldn’t by default.