我正在尝试使用我现有的docker playbooks来配置一个使用ubuntu 18.04进行本地开发的docker容器。
我在容器上运行playbooks时遇到了麻烦,因为它没有安装python,所以根据我的理解,ansible无法运行。
有没有办法我可以在容器上安装python,以便我的剧本可以运行?
NB我知道ansible-container存在,但我想使用我现有的使用become_user的playbook,并且不能像build instructions上所说的那样工作
我没有意识到你可以这样做。
- name: Create container
docker_container:
name: docker-test
image: ubuntu:18.04
command: sleep 1d
- name: Install python on docker
delegate_to: docker-test
raw: apt -y update && apt install -y python-minimal
我认为您最有意义的是在创建映像时在您的Dockerfile中安装Python和其他工具。或者您可以选择已经安装了python的docker镜像,就像在Dockerfile中使用它作为FROM行一样:
FROM python
这样,您每次启动容器时都不必运行Ansible任务来安装Python,它将在您构建映像时构建。
您需要将Docker容器添加到ansible清单中,然后才能在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
请注意,虽然这有效,但它并不是一个非常好的模型:您通常不希望在运行时在容器中执行配置任务;您想在构建时配置图像。