Ansible sudo不起作用

时间:2014-11-14 17:41:15

标签: linux centos sudo ansible

我正在编写Ansible脚本以在CentOS机器上安装一些软件包。

我无法使用sudo

在Ansible中执行简单的YUM命令
- name: Install Java
  yum: name={{java_version}} state=present
  sudo_user: dexter
  sudo: yes

我收到以下错误

  

任务:[tomcat |安装Java] ********************************************* **   失败:[server-name] => {"更改":true," rc":1,"结果":["加载插件:rapidmirror,la   ngpacks \ n"]}   msg:你需要root才能执行这个命令。   致命:所有主机都已经失败 - 中止

我也设置了我的远程用户。

remote_user: dexter

我使用的是ansible 1.7.2

我只能访问dexter用户( NOT 可以访问root用户)

dexter用户位于sudoers列表中。

我可以在机器上对用户执行sudo命令,也可以使用与此类似的东西

sudo yum install ...

很奇怪,我也可以使用shell

在Ansible中运行SUDO命令
- name: Add permissions to dummy path
  shell: sudo chmod 0755 {{dummy_path}}

似乎ansible中的sudo与sudo命令不同。或者我做错了什么?

谢谢!

[编辑]根据@ tedder42建议,我添加我的控制台输出(-vvvv')没有sudo_user`但是使用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

1 个答案:

答案 0 :(得分:4)

你不需要行:

sudo_user: dexter 

它导致脚本以'dexter'形式执行,并且您希望成为root用户。因此,对用户'dexter'的远程主机应用ssh,然后sudo为'dexter'。 :)

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

相关问题