The playbook looks like this, I am aware of this ticket with the same issue ( docker_container: mac_address not respected · Issue #64555 · ansible/ansible (github.com)), and I believe the defaults now should mean that it functions with out me by default by setting the values explicitly. But even when I have tried setting. networks_cli_compatible=true and network_mode: custom_network. it still fails to set the mac address
---
- hosts: my-host
tasks:
- name: Pull the latest Ubuntu image
docker_image:
name: ubuntu
tag: latest
source: pull
- name: Create a custom Docker network
docker_network:
name: custom_network
driver: bridge
- name: Create a Docker container with a specific MAC address
docker_container:
name: my_container
image: ubuntu:latest
state: started
networks:
- name: custom_network
mac_address: "02:42:ac:11:65:43"
command: /bin/bash -c "while true; do sleep 1000; done"
Hi,
Yes, I can send the mac address via the docker command line and also through compose files. The global mac address is deprecated and but it still works both on the docker command line and in ansible. But I am trying to not use the method as it is deprecated.
The new method is to set it in the networks option after defining a custom network. I did that in my example, in your response you said I successfully set it, but you can see I requested, “02:42:ac:11:65:43” but received a different mac address “02:42:ac:12:00:02”.
I don’t think there is anything wrong with my syntax, and I think this is a bug. The bug report page says to ask for help here before reporting a bug. Can anyone tell me what I am doing wrong or confirm this behaviour is unexpected?
The behavior is indeed unexpected, the module is passing the requested MAC address on to the Docker daemon but for some reason it is not set. It is set by Compose though, so it would be interesting to find out what the difference between the two methods is…
I tracked this down to a problem with the Docker Daemon: if a container is attached to a network with Docker Engine API v1.44 Reference, then it ignores the provided MAC address. If the container is created with a list of networks provided (Docker Engine API v1.44 Reference with NetworkingConfig provided), then the provided MAC address is used.
The docker_container module so far created the container without attaching the networks, and then attached the networks in a second step. That causes the difference between the module and Compose that you experienced.
(I also noticed that Compose always recreates the container if the network config changes, even if simply attaching/detaching from a network would suffice. So it will never run into this bug…)