I'm currently having an issue when trying to pull a list of Docker images with Ansible.
My goal is to pull multiple Docker images with Ansible and automatically tag and upload them to our local registry.
Therefore my idea was to use a variable with all our containers:
docker-container:
- mariadb
- guacamole
and then use my task with:
- name: Pull docker container
community.docker.docker_image:
name: "{{ docker-container }}"
source: pull
resulting in:
TASK [docker_registry : Pull image] *******************************
fatal: [192.168.2.20]: FAILED! => {"changed": false, "msg": "Error pulling image ['mariadb', 'guacamole']:latest - 400 Client Error for http+docker://localhost/v1.48/images/create?tag=latest&fromImage=%5B%27mariadb%27%2C+%27guacamole%27%5D: Bad Request (\"invalid reference format\")"}
Pulling each image on its own works, but using a list shouldn't be that hard, right?
It seems like Ansible doesn't use the list as expected but I also don't understand why.
Because the module expect you to pull one unique image at a time, not a list of images.
You can use a loop, though to achieve what you want.
- name: Pull docker container
community.docker.docker_image:
name: "{{ item }}"
source: pull
loop: "{{ docker_container }}"
vars:
docker_container:
- mariadb
- guacamole
Which would result in:
TASK [Pull docker container] *************************************************
changed: [localhost] => (item=mariadb)
changed: [localhost] => (item=guacamole)
Also mind that variables should not contain hyphen (-
), use underscores (_
), instead.
Further reading: in Creating valid variable names.