Is there a way to access the value passed to the prompt when the flags --ask-become-pass or -u are used?
The objective is for the following task to check whether privilege escalation is correctly set up:
- debug:
var: become_pass
- debug:
var: ansible_remote_user
- name: Fail if no become_pass is set
ansible.builtin.fail:
msg: "User {{remote_user}} is not root and become password is not set. Try using '--ask-become-pass' when executing this playbook."
when: remote_user != 'root' and become_pass is not defined
The output:
TASK [debug] ***********************************************************************************************************************************************************************************************************************
ok: [my-host] => {
"become_pass": "VARIABLE IS NOT DEFINED!"
}
TASK [debug] ***********************************************************************************************************************************************************************************************************************
ok: [my-host] => {
"ansible_remote_user": "VARIABLE IS NOT DEFINED!"
}
TASK [Fail if no become_pass is set] ***********************************************************************************************************************************************************************************************
fatal: [my-host]: FAILED! => {"msg": "The conditional check 'remote_user != 'root' and become_pass is not defined' failed. The error was: error while evaluating conditional (remote_user != 'root' and become_pass is not defined): 'remote_user' is undefined\n\nThe error appears to be in '/usr/src/ansible/playbooks/setup-gitlab-runner.yml': line 25, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n var: ansible_remote_user\n - name: Fail if no become_pass is set\n ^ here\n"}