Ansible变得报告成功,但由于缺少特权而命令失败

时间:2019-11-05 11:04:03

标签: ansible

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

我的剧本

---
- 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:不是超级用户。正是您期望以非提升用户身份运行命令的结果。在目标计算机上将用户设置为无密码sudo,并且该用户正在运行剧本。

[local]
centosbox ansible_host=172.66.2.66 ansible_user=config

我尝试过更改设置,添加级别不同,所有这些都会导致相同的错误。我还尝试使用剧本中的不同方法运行swapoff命令,结果相同。 任何建议,不胜感激。

2 个答案:

答案 0 :(得分:0)

我的第一个猜测是,您用来从ubuntu容器SSH到目标容器的SSH用户'config'在两个容器中没有相同的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

答案 1 :(得分:0)

在本文中找到了问题和解决方法-https://forums.docker.com/t/docker-swap-space/3908 我将privileged: true添加到了撰写文件中,该文件终于可以使用了。 谢谢帮助我的所有人。