【问题标题】:Ansible Become reports success but command fails due to lack of privilegeAnsible become 报告成功,但由于缺少权限,命令失败
【发布时间】:2019-11-05 11:04:03
【问题描述】:

我正在运行一个简单的剧本来测试设置 K8s,第一步是关闭交换,这需要提升才能成功。我在一个 ubuntu docker 容器上安装了 ansible,试图在另一个 centos 容器上运行该游戏,都在我的本地 Windows 机器上。

我的剧本

---
- hosts: local
  become: yes
  become_method: sudo
  roles:
    - kubernetes

失败的任务

---
- name: turn off swap
  shell: |
    swapoff -a

将结果输出剪切到相关部分

ok: [centosbox]
<172.66.2.66> SSH: EXEC ssh -C -q -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=config -o ConnectTimeout=10 -o ControlPath=/home/config/.ansible/cp/ansible-ssh-%h-%p-%r -tt 172.66.2.66 '/bin/sh -c '"'"'sudo -H -S -n -u root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-hvrarwhvnbwtveklbinfwigmrapurugb; LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /home/config/.ansible/tmp/ansible-tmp-1572951151.19-42546213906525/command; rm -rf "/home/config/.ansible/tmp/ansible-tmp-1572951151.19-42546213906525/" > /dev/null 2>&1'"'"'"'"'"'"'"'"''"'"''

fatal: [centosbox]: FAILED! => {"changed": true, "cmd": "swapoff -a", "delta": "0:00:00.119504", "end": "2019-11-05 10:52:31.431316", "failed": true, "invocation": {"module_args": {"_raw_params": "swapoff -a", 
"_uses_shell": true, "chdir": null, "creates": null, "executable": null, "removes": null, "warn": true}, "module_name": "command"}, "rc": 1, "start": "2019-11-05 10:52:31.311812", "stderr": "swapoff: Not superuser.", "stdout": "", "stdout_lines": [], "warnings": []}

输出 swapoff: Not superuser. 正是您期望以非提升用户身份运行命令的结果。用户在目标机器上设置为无密码 sudo,并且与运行 playbook 的用户相同。

[local]
centosbox ansible_host=172.66.2.66 ansible_user=config

我尝试改变设置,在不同级别添加变得,都导致同样的错误。我还尝试使用剧本中的不同方法运行 swapoff 命令,结果相同。 任何建议都非常感谢。

【问题讨论】:

  • 可以加become_user:
  • 我试过了,它返回了同样的错误

标签: ansible


【解决方案1】:

我的第一个猜测是,您用于从 ubuntu 容器 SSH 到目标容器的用户“配置”在两个容器中没有相同的 UID。

由于 uid/gid 空间在容器及其主机系统之间共享,因此您需要确保在使用 ubuntu 容器中的“config”用户时,它会转换为目标容器上的同一用户。这可以通过确保这些用户在创建 docker 映像时具有相同的 UID 来完成,例如在你的 Dockerfile 中:

RUN useradd -r -u 1001 -g config config 

好读:https://medium.com/@mccode/understanding-how-uid-and-gid-work-in-docker-containers-c37a01d01cf

【讨论】:

  • 两个配置用户看起来像同一个内核用户 Centos Box [root@8eda9cde51f6 /]# id config uid=1000(config) gid=1000(config) groups=1000(config),10(wheel) Ubuntu/Ansible Host Box root@40da3e97aee1:/# id config uid=1000(config) gid=1000(config) groups=1000(config),27(sudo)
【解决方案2】:

在这篇文章中找到了问题和解决方法-https://forums.docker.com/t/docker-swap-space/3908 我将privileged: true 添加到我的撰写文件中,它终于可以工作了。 感谢所有帮助我找到这个答案的人。

【讨论】:

    猜你喜欢
    • 2016-08-24
    • 2020-02-13
    • 2016-07-12
    • 1970-01-01
    • 2018-07-16
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多