Linux – Ansible sudo does not work

ansiblecentoslinuxsudo

I am writing Ansible scripts to install a few packages on CentOS machine.

I am unable to execute a simple YUM command in Ansible with sudo

- name: Install Java
  yum: name={{java_version}} state=present
  sudo_user: dexter
  sudo: yes

I get the following error

TASK: [tomcat | Install Java] *************************************************
failed: [server-name] => {"changed": true, "rc": 1, "results": ["Loaded plugins: fastestmirror, la
ngpacks\n"]}
msg: You need to be root to perform this command.
FATAL: all hosts have already failed — aborting

I have also set my remote user.

remote_user: dexter

I am using ansible 1.7.2

I only have access to dexter user (do NOT have access to root)

The dexter user is in sudoers list.

I can perform sudo commands on user on the machine and also something similar to this

sudo yum install ...

Weird enough, I am also able to run SUDO commands in Ansible using shell

- name: Add permissions to dummy path
  shell: sudo chmod 0755 {{dummy_path}}

It seems like the sudo in ansible isnt the same as sudo command. Or am I doing something completely wrong?

Thanks!

[EDIT] As per @tedder42 suggestion, I am adding my console output with (-vvvv') without sudo_user` but with sudo: yes

TASK: [apache | Install Apache] ***********************************************

<server-name> ESTABLISH CONNECTION FOR USER: dexter

<server-name> REMOTE_MODULE yum name=httpd state=present

<server-name> EXEC ['ssh', '-C', '-tt', '-vvv', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o', 'ControlPath=/home/vagrant/.ansible/cp/ansible-ssh-%h-%p-%r', '-o', 'KbdInteractiveAuthentication=no', '-o', 'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', '-o', 'PasswordAuthentication=no', '-o', 'User=dexter', '-o','ConnectTimeout=10', 'server-name', "/bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1416243654.43-120840990326663 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1416243654.43-120840990326663 && echo $HOME/.ansible/tmp/ansible-tmp-1416243654.43-120840990326663'"]

<server-name> PUT /tmp/tmpV8bobo TO /home/dexter/.ansible/tmp/ansible-tmp-1416243654.43-120840990326663/yum

<server-name> EXEC ['ssh', '-C', '-tt', '-vvv', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o', 'ControlPath=/home/vagrant/.ansible/cp/ansible-ssh-%h-%p-%r', '-o', 'KbdInteractiveAuthentication=no', '-o', 'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', '-o', 'PasswordAuthentication=no', '-o', 'User=dexter', '-o', 'ConnectTimeout=10', 'server-name', u'/bin/sh -c \'sudo -k && sudo -H -S -p "[sudo via ansible, key=nnqylcywghyrogwhmdyzwidpsqbqxuef] password: " -u root /bin/sh -c \'"\'"\'echo SUDO-SUCCESS-nnqylcywghyrogwhmdyzwidpsqbqxuef; LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 /usr/bin/python -tt /home/dexter/.ansible/tmp/ansible-tmp-1416243654.43-120840990326663/yum; rm -rf /home/dexter/.ansible/tmp/ansible-tmp-1416243654.43-120840990326663/ >/dev/null 2>&1\'"\'"\'\'']

fatal: [server-name] => failed to parse: Sorry, user dexter is not allowed to execute '/bin/sh -c echo SUDO-SUCCESS-nnqylcywghyrogwhmdyzwidpsqbqxuef; LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 /usr/bin/python -tt /home/dexter/.ansible/tmp/ansible-tmp-1416243654.43-120840990326663/yum; rm -rf /home/dexter/.ansible/tmp/ansible-tmp-1416243654.43-120840990326663/ >/dev/null 2>&1' as root on server-name.mia.amadeus.net.


FATAL: all hosts have already failed -- aborting

Best Answer

You don't need line:

sudo_user: dexter 

It causes the script is executed as 'dexter', and you want to be root. So ansible ssh to remote host as user 'dexter', then sudo to 'dexter'. :)

http://docs.ansible.com/playbooks_intro.html

Related Topic