0

我正在尝试使用 Ansible Container 作为一个基本示例,该示例应该在映像中安装 Node。当我使用该ansible-container build命令时,在成功构建导体映像后,它使第一个任务失败并出现sudo相关错误。有问题的任务需要root特权才能执行。

我正在运行 Debian GNU/Linux 9.2 (stretch),并通过 Docker APT 存储库安装了 Docker 17.09.0-ce。我尝试使用 Debian Stretch (2.2.1.0-2) 和 Pypi (2.4.1.0) 的 Ansible。我从 Pypi (0.9.3rc0) 和最新的 Git 源中尝试了 Ansible Container。我总是得到完全相同的错误输出。

Ansible 模块抱怨以下内容:

sudo: error while loading shared libraries: libsudo_util.so.0: cannot open shared object file: No such file or directory

正在运行的任务如下所示:

- name: Add the Node Source repository signing certificate to APT
  apt_key:
    id: 9FD3B784BC1C6FC31A8A0A1C1655A0AB68576280
    keyserver: hkps://hkps.pool.sks-keyservers.net
  become: yes

我尝试创建的导体和服务都使用debian:stretch基本图像。

我正在运行ansible-container build带有sudo前缀的命令,因为只能root访问我系统上的 Docker 套接字。

这是我的内容container.yml

version: "2"
settings:
  conductor:
    base: debian:stretch
  project_name: container_test
services:
  nodejs:
    from: debian:stretch
    roles:
      - nodejs
registries: {}

这是完整的错误输出:

Building Docker Engine context...
Starting Docker build of Ansible Container Conductor image (please be patient)...
Parsing conductor CLI args.
Docker™ daemon integration engine loaded. Build starting.       project=container_test
Building service...     project=container_test service=nodejs

PLAY [nodejs] ******************************************************************

TASK [Gathering Facts] *********************************************************
ok: [nodejs]

TASK [nodejs : Add the Node Source repository signing certificate to APT] ******
fatal: [nodejs]: FAILED! => {"changed": false, "module_stderr": "sudo: error while loading shared libraries: libsudo_util.so.0: cannot open shared object file: No such file or directory\n", "module_stdout": "", "msg": "MODULE FAILURE", "rc": 127}
        to retry, use: --limit @/tmp/tmpTRBQDe/playbook.retry

PLAY RECAP *********************************************************************
nodejs                     : ok=1    changed=0    unreachable=0    failed=1

ERROR   Error applying role!    engine=<container.docker.engine.Engine object at 0x7f84da0c5ed0> exit_code=2 playbook=[{'hosts': u'nodejs', 'roles': ['nodejs'], 'vars': {}}]
Traceback (most recent call last):
  File "/usr/local/bin/conductor", line 11, in <module>
    load_entry_point('ansible-container', 'console_scripts', 'conductor')()
  File "/_ansible/container/__init__.py", line 19, in __wrapped__
    return fn(*args, **kwargs)
  File "/_ansible/container/cli.py", line 408, in conductor_commandline
    **params)
  File "/_ansible/container/__init__.py", line 19, in __wrapped__
    return fn(*args, **kwargs)
  File "/_ansible/container/core.py", line 843, in conductorcmd_build
    raise RuntimeError('Build failed.')
RuntimeError: Build failed.
Conductor terminated. Cleaning up.      command_rc=1 conductor_id=e8899239ad1017a89acc97396d38ab805d937a7b3d74e5a7d2741d7b1124bb0c save_container=False
ERROR   Conductor exited with status 1
4

1 回答 1

1

我找到了原因:

基本映像debian:stretch不包括sudo. 因此,一个解决方案是将 设置become_methodsu。通常,这可以按任务、主机或剧本完成。因为在 Ansible Container 中相当于 playbook 的位置以及主机的概念如何应用并不明显,所以我只能选择使用任务级别。

我决定添加一个安装sudo在映像中的角色,并且只为该角色中的单独任务设置为become_methodsu应用角色后,无需进一步更改,原始任务即可工作。

一个后续问题是,也没有安装 GnuPG 以apt_key正确完成模块任务。以下解决方案解决了这两个问题:

- become: yes
  become_method: su
  block:
    - name: Update package cache
      apt:
        update_cache: yes
        cache_valid_time: 86400

    - name: Install GnuPG
      package:
        name: gnupg2
        state: present

    - name: Install sudo
      package:
        name: sudo
        state: present

假设一个更干净的选项是生成一个已经包含这些依赖项的基础镜像,以允许更简化的 Ansible 容器镜像生成。

于 2017-11-20T13:22:36.200 回答