I am running an ansible playbook as a sudo user (forcing the sudo password) - however, I am getting a response stating that the su password is incorrect even though I can do the following on the remote server (with the same password that I tried with ansible):
sudo su - root
error message
fatal: [testserver]: FAILED! => {"failed": true, "msg": "Incorrect su password"}
hosts
[webservers]
testserver ansible_ssh_host=ec2-52-87-166-241.compute-1.amazonaws.com ansible_ssh_port=9876
ansible command
ansible-playbook test_playbook.yml -i hosts --ask-become-pass -vvv
test_playbook
---
- hosts: all
gather_facts: no
remote_user: testuser
become: yes
become_method: su
become_user: root
any_errors_fatal: true
tasks:
- group:
name: devops
state: present
- name: create devops user with admin privileges
user:
name: devops
comment: "Devops User"
uid: 2001
groups: devops
Any thoughts on what I might be doing wrong?
Providing the sudo Password If the remote user needs to provide a password in order to run sudo commands, you can include the option --ask-become-pass to your Ansible command. This will prompt you to provide the remote user sudo password: ansible all -m ping --ask-become-pass.
You can pass variable on the command line via --extra-vars "name=value". You need to use the Sudo password variable named ansible_sudo_pass as shown below.
Ansible is intended for automating administrative tasks, so generally needs top-level (root) level access hence "passwordless sudo". If you only need it to run a subset of the commands available on your system though, you can lock it down to just those commands with a more detailed sudo configuration.
In 'sudo su - root
' the root privilege is gained by sudo
rather than su
(that is why the latter doesn't ask for the root password, since it is invoked by a process already in the role of the root user).
However, in your setup you have specified become_method: su
, which expects root's password.
So the fix will be to change become_method
to sudo
(or, if you know root's password, enter that one instead of your user's password).
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