Specify sudo password for Ansible
How do I specify a sudo password for Ansible in non-interactive way?
I'm running Ansible playbook like this:
$ ansible-playbook playbook.yml -i inventory.ini \
--user=username --ask-sudo-pass
But I want to run it like this:
$ ansible-playbook playbook.yml -i inventory.ini \
--user=username` **--sudo-pass=12345**
Is there a way? I want to automate my project deployment as much as possible.
Solution 1:
The docs strongly recommend against setting the sudo password in plaintext, and instead using --ask-sudo-pass
on the command line when running ansible-playbook
2016 Update:
Ansible 2.0 (not 100% when) marked --ask-sudo-pass
as deprecated. The docs now recommend using --ask-become-pass
instead, while also swapping out the use of sudo
throughout your playbooks with become
.
Solution 2:
You can pass variable on the command line via --extra-vars "name=value"
. Sudo password variable is ansible_sudo_pass
. So your command would look like:
ansible-playbook playbook.yml -i inventory.ini --user=username \
--extra-vars "ansible_sudo_pass=yourPassword"
Update 2017: Ansible 2.2.1.0 now uses var ansible_become_pass
. Either seems to work.
Update 2021: ansible_become_pass is still working, but for now, we should use -e instead of --extra-vars