linux - Ansible sudo 不起作用

标签 linux centos sudo ansible

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

我无法使用 sudo 在 Ansible 中执行简单的 YUM 命令

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

出现以下错误

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

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

remote_user: dexter

我正在使用 ansible 1.7.2

我只能访问 dexter 用户(不能访问 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 命令不同。还是我做错了什么?

谢谢!

[EDIT] 根据@tedder42 的建议,我在控制台输出中添加了不带 sudo_user 的 (-vvvv'),但使用了 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

最佳答案

你不需要线:

sudo_user: dexter 

它导致脚本以“dexter”身份执行,而你想成为 root。所以 ansible ssh 到远程主机作为用户 'dexter',然后 sudo 到 'dexter'。 :)

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

关于linux - Ansible sudo 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26935810/

相关文章:

linux - 当用户按任意键时telnet发送哪些数据

java - maven-compiler-plugin 在 linux 上坏了?

regex - 使用 iname 在 Linux Find 命令中匹配 RegExp

java - sudo: java: 从 root 用户退出后找不到命令

python - 从 pexpect sendline 读取输出

linux - 在 Linux 上,如何创建具有 sudo 能力的受限 shell

php - 在不使用 'ping' 的情况下从动态语言执行 Ping

ruby - 在 Centos 5.8 上安装 gitorious(乘客模块)

php - require_once 在命令行中不起作用

bash - 获取错误 : bash: parse_git_branch: command not found