通过Ansible复制文件到docker容器



我想复制一个文件到docker容器,作为我的Ansible剧本步骤之一。我用jinja2"模板"创建文件。我可以复制/tmp/下的文件,然后运行命令将其复制到docker容器中,例如:
' docker cp/tmp/config。json my_image:/app/config/路径/'
但我正在寻找更好的方法不使用"/tmp"或类似的

Ansible有一个docker连接插件,你可以用它来与现有的容器进行交互。例如,如果我有一个名为mycontainer的容器:

$ docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
07899303ac55   alpine    "sleep inf"   7 seconds ago   Up 2 seconds             mycontainer

我可以像这样创建一个Ansible库存,将ansible_connection变量设置为community.general.docker:

all:
hosts:
mycontainer:
ansible_connection: community.docker.docker    

现在我可以像这样瞄准容器了:

- hosts: mycontainer
gather_facts: false
become: true
tasks:
- name: create target directory in container
file:
path: /target
state: directory
- name: copy a file into the container
copy:
src: example.file
dest: /target/example.file

最新更新