Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible: sudo without password

I want to run ansible with user sa1 without sudo password:

First time OK:

[root@centos1 cp]# ansible cent2 -m shell -a "sudo yum -y install httpd"

cent2 | SUCCESS | rc=0 >>

Second time FAILED:

[root@centos1 cp]# ansible cent2 -s -m yum -a "name=httpd state=absent"

cent2 | FAILED! => {
    "changed": false,
    "failed": true,
    "module_stderr": "",
    "module_stdout": "sudo: a password is required\r\n",
    "msg": "MODULE FAILURE",
    "parsed": false
}

Please help!

like image 336
bbkaaka Avatar asked May 25 '16 10:05

bbkaaka


People also ask

Does Ansible sudo need Passwordless?

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.

How do I bypass sudo password in Ansible Tower?

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.

Can we sudo without a password?

We can set the Sudo without a password using the /etc/sudoers file. The sudoers is the system administration configuration file having all the information about the user, what, and when they performed in the system.

Does Ansible need sudo?

If you expect ansible to perform tasks that require root access, then ansible needs root privileges, either via sudo or via appropriate ssh credentials to the root account. You can't restrict Ansible to particular commands because Ansible isn't running specific commands; it's running (typically) python .


2 Answers

It's not ansible it's your server's configuration. Make sure that sudo is allowed for the user ansible is using without password.

  1. To do that login to the server
  2. Open the sudoers file with sudo visudo
  3. Make sure you have a line something like this: centos ALL=(ALL) NOPASSWD:ALL
  4. Replace centos with the your user
  5. Save the file

You can try from the server itself by running:

sudo -u [yourusername] sudo echo "success"

If this works it should work from ansible too.

like image 170
Károly Nagy Avatar answered Sep 20 '22 15:09

Károly Nagy


By default ansible runs sudo with the flags: -H -S -n to become root. Where --non-interactive would be the corresponding long form for option -n. This option seems to make sudo return the error message, without attempting to let the authentication modules do their thing.

I managed to get around the password error by creating a ~/.ansible.cfg containing lines as below, for the most relevant ansible version.

ansible 2.4

[defaults]
sudo_flags = --set-home --stdin

ansible 2.9

[sudo_become_plugin]
flags = -H -S

That was at least enough to allow pam_ssh_agent_auth.so to run and authenticate me.

Prior to version 2.8 the above example works, newer than 2.8 requires the second example. Documentation for the new style configuration can be found in the Ansible User Guide.

like image 25
sampi Avatar answered Sep 18 '22 15:09

sampi