So ansible-playbook
has --ask-pass
and --ask-sudo-pass
. Is there a way to get ansible to try ssh without a password first and then only prompt for a password if passwordless login fails? Similarly, can ansible try sudo without a password first and then only prompt if that doesn't work?
FYI I have a little shell function to try to figure this out by trial and error, but I'm hoping something like this is baked into ansible.
get_ansible_auth_args() {
local base_check="ansible all --one-line --inventory-file=deploy/hosts/localhost.yml --args=/bin/true --sudo"
${base_check}
if [[ $? -eq 0 ]]; then
return;
fi
local args="--ask-pass"
${base_check} ${args}
if [[ $? -eq 0 ]]; then
export ANSIBLE_AUTH_ARGS="${args}"
return;
fi
local args="--ask-pass --ask-sudo-pass"
${base_check} ${args}
if [[ $? -eq 0 ]]; then
export ANSIBLE_AUTH_ARGS="${args}"
return;
fi
}
If you need to use password-based authentication in order to connect to the nodes, you need to append the option --ask-pass to your Ansible command. This will make Ansible prompt you for the password of the user on the remote server that you're attempting to connect as: ansible all -m ping --ask-pass.
The “Vault” is a feature of Ansible that allows you to keep sensitive data such as passwords or keys protected at rest, rather than as plaintext in playbooks or roles.
If you want your playbook to prompt the user for certain input, add a 'vars_prompt' section. Prompting the user for variables lets you avoid recording sensitive data like passwords. In addition to security, prompts support flexibility.
Ansible uses SSH protocol to connect to servers and run tasks. By default, Ansible uses SSH keys with ssh-agent and connects to remote machines using your current user name. Root logins are not required. You can log in as any user, and then su or sudo to any user.
If you set ask_pass
and ssh_args
as I show below then ansible should ask you for password at the beginning once and use that password whenever public key auth doesn't work.
[defaults]
ask_pass = True
[ssh_connection]
ssh_args = -o PubkeyAuthentication=yes -o PasswordAuthentication=yes -o ControlMaster=auto -o ControlPersist=60s
This is still not the full solution: Catch being (AFAIK) ansible uses sshpass
, so the password it collected from your at the start would be the only password it would use and it won't work if you have different passwords for different machines. :-)
Only other hack I can think of is to replace /usr/bin/ssh
(or whichever is your openssh's ssh used by ansible) with a script of your own that wraps the logic of reading password from some flat file if needed, I suspect ansible would hide the tty
so your script won't be able to 'read' the password from stdin.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With