Ansible has a docker
connection plugin that you can use to interact with existing containers in your playbook. For example, if I have a container named mycontainer
:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
07899303ac55 alpine "sleep inf" 7 seconds ago Up 2 seconds mycontainer
I can create an Ansible inventory like this that sets the ansible_connection
variable to community.general.docker
:
all:
hosts:
mycontainer:
ansible_connection: community.docker.docker
Now I can target the container in a play like this:
- 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