Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Ansible become password in a task

My playbook is invoked with --ask-become-pass. The become password is supplied via an environmental variable on the Control machine.

Everything is working just fine for the many tasks I have which require privilege escalation. However, there's one or two tasks that require the password directly as part of the command. E.g. I was hoping I could use something like: command: /foo/bar autoLoginUser {{ ansible_become_pass }}

...but I get the following: "The task includes an option with an undefined variable. The error was: 'ansible_become_pass' is undefined."

I'd rather not duplicate this password in the Vault if I can help it.

like image 604
mc7h Avatar asked Oct 31 '25 22:10

mc7h


1 Answers

There is another way to supply the become password. As per the documentation:

  • providing the --ask-become-pass command line option
  • setting the ansible_become_password connection variable

This variable can be set in the inventory, like so:

webserver01 ansible_user=ansible ansible_become_password=secret

Or supplied from the command line instead of --ask-become-pass. Like so:

export MY_PASSWORD=secret
ansible-playbook myplaybook.yml -e "ansible_become_password=$MY_PASSWORD"

Since this variable is now set, you can then have tasks like:

command: "/foo/bar autoLoginUser {{ ansible_become_password }}"
like image 65
seshadri_c Avatar answered Nov 02 '25 11:11

seshadri_c



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!