9

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!

1
  • which version of ansible are you using? The -s switch is deprecated in newer versions. Maybe try with -b (become). Also, try to explicitly set the login user via -u, does it work then?
    – fishi0x01
    May 25, 2016 at 10:48

4 Answers 4

15

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.

4
  • As you can see the first command is successful [root@centos1 cp]# ansible cent2 -m shell -a "sudo yum -y install httpd" cent2 | SUCCESS | rc=0 >> i have already add sa1 ALL=(ALL) NOPASSWD: SOFTWARE , /bin/echo
    – bbkaaka
    May 26, 2016 at 1:51
  • 1
    I assume SOFTWARE is a command alias. What do you have defined there? Can you try with ALL to see if that is the limiting factor? If you run ansible with -vvvv you can see what command it is trying to invoke. May 26, 2016 at 8:11
  • I success when change from SOFTWARE TO ALL. This is my alias Cmnd_Alias SOFTWARE = /bin/rpm, /usr/bin/up2date, /usr/bin/yum
    – bbkaaka
    May 26, 2016 at 9:02
  • 3
    Ansible is doing all sorts of tricks with copying executables to temporary directory that it deletes after execution. I've tested on a server of mine an ansible run with -vvvv. It puts and calls a different yum file: PUT /var/folders/sr/hcg9zzm12jg28txszy7nnh0x721jx7/T/tmpLs5wbd TO /home/centos/.ansible/tmp/ansible-tmp-1464250141.88-48281030641080/yum and after that /usr/bin/python -tt /home/centos/.ansible/tmp/ansible-tmp-1464250141.88-48281030641080/yum so your alias is not matching with the command being executed with ansible module. May 26, 2016 at 12:30
10

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.

0
8

Here's the playbook in case you want ansible make it for you

  1. Add user to chosen group ( in my case wheel)
  2. Add this to your playbook
- name: Make users passwordless for sudo in group wheel
  lineinfile:
    path: /etc/sudoers
    state: present
    regexp: '^%wheel'
    line: '%wheel ALL=(ALL) NOPASSWD: ALL'
    validate: 'visudo -cf %s'
1
  • 2
    Yep, and use -K when running this playbook only to ask for the elevation password. Aug 26, 2021 at 13:30
0
##-----------------------
  - name: sudo without password
    become: true
    copy:
      dest: /etc/sudoers.d/dont-prompt-ubuntu_user-for-sudo-password
      content: 'ubuntu ALL=(ALL) NOPASSWD:ALL'

It creates a file called /etc/sudoers.d/dont-prompt-ubuntu_user-for-sudo-password with the following contents:

ubuntu ALL=(ALL) NOPASSWD:ALL

This works because Debian's and Ubuntu's default /etc/sudoers file has this line:

@includedir /etc/sudoers.d

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.