become: yes user: root

Hi

Two things, related.

Firstly, I currently run my ansible playbook with --ask-sudo-pass, otherwise as some of my tasks include:

become: yes
user: root

I get the error:

fatal: [localhost]: FAILED! => {“changed”: false, “failed”: true, “module_stderr”: “sudo: a password is required\n”, “module_stdout”: “”, “msg”: “MODULE FAILURE”}

(I am currently learning how to write Ansible playbooks, and using it to control the software installed on my local machine, which is working fine).

Secondly, if I instead use:

become: yes
user: myusername

I get:

fatal: [localhost]: FAILED! => {“changed”: false, “failed”: true, “msg”: “This command has to be run under the root user.”}

This error is in relation to a dnf, for example:

  • name: install vim
    dnf: name=vim state=latest
    become: yes
    become_user: root

Perhaps, since the flag is --ask-sudo-pass, and not “ask su pass” or “ask root pass” or something like that, my playbook is already using sudo on my account, and I just don’t realize.
But, barring that possibility, what is a better way for me to do this?

Thanks very much. I searched for information regarding this before posting here, but could not find clear guidance.

So ‘user’ is a deprecated form for ‘remote_user’ which is the ‘login user’, ‘become_user’ is the you escalate to, by default this is root so you should not need to specify it.

The password prompt tries to guess which method you are using, but it does not matter if you switch it in play as the password is still used the same.

It seems you are mixing options in incompatible ways, w/o more information I cannot even guess how. I’m not sure what you mean by ‘my playbook is already using sudo on my account’.