python - 使用 ansible 配置 docker 容器

标签 python docker ansible

我正在尝试使用我现有的 docker playbook 来配置一个使用 ubuntu 18.04 的 docker 容器进行本地开发。

我在容器上运行 playbook 时遇到问题,因为它没有安装 python,所以根据我的理解,ansible 无法运行。

有没有办法在容器上安装 python 以便我的 playbook 可以运行?

注意我知道 ansible-container 存在,但我想使用我现有的使用 become_user 的剧本,但它并不像 build instructions 中所述那样工作

最佳答案

您需要先将您的 Docker 容器添加到 ansible list 中,然后才能将其定位到您的 playbook 中。这样的事情会起作用:

---
- hosts: localhost
  gather_facts: false

  tasks:
    - name: create container
      docker_container:
        name: ansible-test
        image: ubuntu:bionic
        command: bash
        detach: true
        interactive: true
        tty: true

    - name: add docker container to inventory
      add_host:
        name: ansible-test
        ansible_connection: docker

- hosts: ansible-test
  gather_facts: false
  tasks:

    - name: update apt cache
      delegate_to: ansible-test
      raw: apt -y update

    - name: install python
      delegate_to: ansible-test
      raw: apt -y install python-minimal

    - name: demonstrate that normal ansible modules work
      file:
        path: /etc/testdir
        state: directory

请注意,虽然这可行,但它并不是一个非常好的模型:您通常不想在运行时在容器中执行配置任务;您想在构建时配置您的图像

关于python - 使用 ansible 配置 docker 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55810587/

相关文章:

python - python networkx中的图例

python - 如何防止 Python 的 urllib(2) 跟随重定向

docker - 我可以将运行在Docker容器中的脚本输出作为本地文件夹吗?

mysql - 如何将 longblob 转换为可读格式

ansible - 从 Ansible 中的 map 列表中提取属性列表的正确方法

python - 在 python 中使用特殊字符解压缩时修复文件名的编码

Python cx_freeze 和 setuptools

linux - Ubuntu Docker 容器立即停止,Dockerfile 出现问题?

docker - Helm 图表使用来自证书管理器的 secret

ansible - 在ansible中将ssl证书从服务器复制到主机