ansible-community / ansible-community/molecule-plugins
Docker plugin tries to create / delete the same network multiple times
- Dominant language
- Python
- Stars
- 164
- Forks
- 113
- Avg merge
- 4d 20h
- Merged PRs (30d)
- 1
Description
When using the docker plugin, if multiple platforms are specified with the same network, the create and destroy playbooks loop through the same network multiple times, trying to create / delete the docker network. This doesn't cause any major problem, as the create / delete network operation is idempotent, but it's inefficient, especially when there are lots of platforms in the same molecule scenario, all sharing the same network.
A minimal example of the molecule.yml to produce this is below:
```yaml
---
dependency:
name: galaxy
driver:
name: docker
platforms:
- name: test1.local
image: alpine
pre_build_image: true
command: ${MOLECULE_DOCKER_COMMAND:-""}
networks:
- name: "my-test-network"
network_mode: "my-test-network"
- name: test2.local
image: alpine
pre_build_image: true
command: ${MOLECULE_DOCKER_COMMAND:-""}
networks:
- name: "my-test-network"
network_mode: "my-test-network"
provisioner:
name: ansible
verifier:
name: ansible
```
When `molecule create` is run with the above, it gives the following output (other output removed for clarity) - note that it calls the "create_network.yml" twice:
```
...
TASK [Create docker network(s)] ************************************************
included: /usr/local/lib/python3.9/site-packages/molecule_plugins/docker/playbooks/tasks/create_network.yml for localhost => (item=my-test-network)
included: /usr/local/lib/python3.9/site-packages/molecule_plugins/docker/playbooks/tasks/create_network.yml for localhost => (item=my-test-network)
TASK [Check if network exist] **************************************************
ok: [localhost]
TASK [Create docker network(s)] ************************************************
changed: [localhost]
TASK [Check if network exist] **************************************************
ok: [localhost]
TASK [Create docker network(s)] ************************************************
skipping: [localhost]
...
```
This could be easily solved by adding a `unique` filter to the `with_items` statements in `create.yml` and `destroy.yml` playbooks - i.e. in https://github.com/ansible-community/molecule-plugins/blob/main/src/molecule_plugins/docker/playbooks/create.yml#L90 , change:
```
with_items: "{{ molecule_yml.platforms | molecule_get_docker_networks(molecule_labels) }}"
```
to
```
with_items: "{{ molecule_yml.platforms | molecule_get_docker_networks(molecule_labels) | unique }}"
```
and in https://github.com/ansible-community/molecule-plugins/blob/main/src/molecule_plugins/docker/playbooks/destroy.yml#L49 , change
```
loop: "{{ molecule_yml.platforms | molecule_get_docker_networks() }}"
```
to
```
loop: "{{ molecule_yml.platforms | molecule_get_docker_networks() | unique }}"
```
Happy to submit a PR for this if helpful?
Contributor guide
No contributing guide indexed for this repository
Research direction
Read src/molecule_plugins/docker/playbooks/create.yml and destroy.yml, then run the minimal molecule.yml scenario with two platforms sharing my-test-network. The create and destroy playbooks should process each distinct network once rather than once per platform.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ansible, docker
- Domain
- devops
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100