Bruc3
Bruc3

Reputation: 103

sudo error when running playbook in ansible

I am attempting to use ansible without a password. This is not my first time using ansible but this is the first time I have encountered an issue with the --become and --become_method. My issue is very similar to another stack overflow issue with a couple of difference: Ansible playbook: Requires sudo password

The system I am running ansible from is a Ubuntu 16.04 derivative and the server I am trying to configure with ansible is running Centos 7.

The only modification I have made to my ansible.cfg file is the "nocows = 1" is enabled. Outside of that setting everything else is default.

I am able to ssh into the server without needing a password and use sudo to switch into the root user:

admin@linuxdesktop ~/Documents/ansible/test1 $ ansible --version
ansible 2.1.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
admin@linuxdesktop ~/Documents/ansible/test1 $ ssh ansible@server
Last login: Wed Oct 18 17:51:21 2017 from 10.4.1.28
[ansible@server ~]$ sudo su -
Last login: Wed Oct 18 17:53:41 CDT 2017 on pts/1
[root@server ~]# cat /etc/sudoers.d/ansible 
ansible ALL=(ALL) NOPASSWD:ALL
[root@server ~]#

This is the contents of my test.yml file:

---
- hosts: server
  become: yes
  become_method: sudo
  tasks:
    - shell: echo "hello world!"

This is the result of when I run the test.yml file:

admin@linuxdesktop ~/Documents/ansible/test1 $ ansible-playbook -i dev test.yml 

PLAY [server] ******************************************************************

TASK [setup] *******************************************************************
fatal: [server]: FAILED! => {"changed": false, "failed": true, "module_stderr": "", "module_stdout": "sudo: a password is required\r\n", "msg": "MODULE FAILURE", "parsed": false}

NO MORE HOSTS LEFT *************************************************************
 [WARNING]: Could not create retry file 'test.retry'.         [Errno 2] No such file or directory: ''


PLAY RECAP *********************************************************************
server                     : ok=0    changed=0    unreachable=0    failed=1 

This is what I did to get things working for me:

I was able to modify my test.yml file to the following to get this to work:

---
- hosts: server
  remote_user: ansible
  become: yes
  become_method: sudo
  tasks:
    - shell: echo "hello world!"

I also found that if you use the same user across all your servers you can set the following option in your /etc/ansible/ansible.cfg file as well:

remote_user = ansible

Upvotes: 2

Views: 2654

Answers (2)

thebjorn
thebjorn

Reputation: 27311

Last time I had that problem it was because the line giving the user's group sudo permissions was after the include of sudoers.d. The fix was to switch the lines:

deployer@linoidbc:/etc$ sudo cat sudoers
...

%deployers ALL=(ALL) ALL
#includedir /etc/sudoers.d

deployer@linoidbc:/etc$ sudo cat sudoers.d/deployer
deployer ALL=NOPASSWD: ALL

Upvotes: 0

Ratul
Ratul

Reputation: 451

Here is my output of test.yml ansible-playbook test.yml

I think you've problem in inventory (dev) file.. mine is

[server]
server ansible_host=ip_address ansible_user=ubuntu ansible_private_key_file=/path_to_keyfile/docker-key.pem

Upvotes: 2

Related Questions