Hello,
I am currently in the process of moving from local docker containers to Docker Swarm. I use Ansible docker modules to deploy my containers and this has worked great. I am now using the Docker Swarm Ansible modules, namely - community.docker.docker_swarm_service module. This is working fine until I need to include a command for the service. For example, with Portainer - this will work:
- name: Create portainer service
community.docker.docker_swarm_service:
name: '{{ portainer_defaults_name }}'
image: '{{ portainer_defaults_image_repo }}:{{ portainer_defaults_image_tag }}'
networks:
- name: '{{ network_overlay }}'
env:
PUID: '{{ puid }}'
PGID: '{{ pgid }}'
TZ: '{{ timezone }}'
labels: '{{ portainer_defaults_labels }}'
publish:
- target_port: '{{ portainer_defaults_ports_http_cont }}'
published_port: '{{ portainer_defaults_ports_http_host }}'
protocol: tcp
mode: ingress
- target_port: '{{ portainer_defaults_ports_tcp_tunnel_cont }}'
published_port: '{{ portainer_defaults_ports_tcp_tunnel_host }}'
protocol: tcp
mode: ingress
- target_port: '{{ portainer_defaults_ports_webui_cont }}'
published_port: '{{ portainer_defaults_ports_webui_host }}'
protocol: tcp
mode: ingress
mounts:
- source: '{{ portainer_defaults_location }}'
target: /data
type: bind
restart_config:
condition: '{{ portainer_defaults_restart_policy }}'
delay: 5s
max_attempts: 3
window: 120s
mode: replicated
replicas: 1
placement:
constraints: [node.role == manager]
However, if I include the command to connect to the Portainer agent:
- name: Create portainer service
community.docker.docker_swarm_service:
name: '{{ portainer_defaults_name }}'
image: '{{ portainer_defaults_image_repo }}:{{ portainer_defaults_image_tag }}'
networks:
- name: '{{ network_overlay }}'
command: '-H tcp://tasks.{{ portainer_agent_defaults_name }}:9001 --tlsskipverify'
env:
PUID: '{{ puid }}'
PGID: '{{ pgid }}'
TZ: '{{ timezone }}'
labels: '{{ portainer_defaults_labels }}'
publish:
- target_port: '{{ portainer_defaults_ports_http_cont }}'
published_port: '{{ portainer_defaults_ports_http_host }}'
protocol: tcp
mode: ingress
- target_port: '{{ portainer_defaults_ports_tcp_tunnel_cont }}'
published_port: '{{ portainer_defaults_ports_tcp_tunnel_host }}'
protocol: tcp
mode: ingress
- target_port: '{{ portainer_defaults_ports_webui_cont }}'
published_port: '{{ portainer_defaults_ports_webui_host }}'
protocol: tcp
mode: ingress
mounts:
- source: '{{ portainer_defaults_location }}'
target: /data
type: bind
restart_config:
condition: '{{ portainer_defaults_restart_policy }}'
delay: 5s
max_attempts: 3
window: 120s
mode: replicated
replicas: 1
placement:
constraints: [node.role == manager]
Portainer will not successfully deploy, and upon checking with ‘docker ps -a’, it will show portainer listed 4 times with the status of ‘created’. At first I thought this was a Portainer issue - but it is occurring with every service where I try to include a command. I had none of these issues with the container modules, previously.
Is there a difference in how the ‘command:’ component works for services, as opposed to containers? Is this not a Docker issue at all, and just a quirk with the Ansible docker service module? Any ideas here?
OS Version/build
Client: Docker Engine - Community
Version: 27.5.1
Context: default
Debug Mode: false
Plugins:
buildx: Docker Buildx (Docker Inc.)
Version: v0.20.0
Path: /usr/libexec/docker/cli-plugins/docker-buildx
compose: Docker Compose (Docker Inc.)
Version: v2.32.4
Path: /usr/libexec/docker/cli-plugins/docker-compose
Swarm: active
Is Manager: true
Kernel Version: 6.1.0-28-amd64
Operating System: Debian GNU/Linux 12 (bookworm)
OSType: linux
Architecture: x86_64
ansible [core 2.17.8]
config file = /etc/ansible/ansible.cfg
configured module search path = [‘/root/.ansible/plugins/modules’, ‘/usr/share/ansible/plugins/modules’]
ansible python module location = /usr/lib/python3/dist-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.11.2 (main, Nov 30 2024, 21:22:50) [GCC 12.2.0] (/usr/bin/python3)
jinja version = 3.1.2
libyaml = True
Steps to reproduce
- Deploy service without a command using the Ansible docker swarm service module
- Try deploying the same service with a command using the Ansible docker swarm service module