Using ansible with password authentication and different usernames

ansibleansible-playbook

We're in the position of maintaining multiple database servers, which have one of two admin accounts, and which use password authentication. Lets say that the two accounts are "db_admin" and "db_user". The passwords are the same for all examples of each username, but different between the two usernames – ie the password for all hosts where the username is db_admin is "password1", and the password for all hosts where the username is db_user is "password2".

How can I run playbooks (or even ad-hoc commands) against all hosts. I can set ansible_ssh_user in the inventory, and use -k on the command-line, but this prompts only once for the password, which will therefore be wrong for half the hosts. I could repeat the ansible run, of course, specifying a different host group each time, but I'd like to do it in a single pass.

Aside: Please, I would much prefer key-based authentication – this is not currently an option. I'd also like to (using Ansible, of course) edit /etc/{passwd,shadow,group} and change the username to be the same on all hosts, but that's not possible (and might break other things which rely on, for example an "scp file db_user@host", rather than "scp db_admin@host" ). Thirdly, I'd like to go back in time and prevent the divergence in the first place, but none of these are options yet.

Best Answer

Define the user and password in host_vars per each host or group_vars per host group.

You need to define appropriate parameters listed here, i.e.:

ansible_user The default ssh user name to use.

ansible_ssh_pass The ssh password to use. ** Requires sshpass

Per guidance you should encrypt the values with Ansible Vault.