community.docker
community.docker copied to clipboard
Support list style labels
SUMMARY
Allow specifying labels as list like in docker-compose.
- name: Create Traefik labels's dictionary
ansible.builtin.set_fact:
my_labels: "{{ my_labels | default({}) | combine ({ item.key : item.value }) }}"
with_items:
- { 'key': 'traefik.enable', 'value': 'true'}
- { 'key': 'traefik.http.routers.{{ project_name }}.rule', 'value': 'Host(`{{ traefik_host }}`)'}
- { 'key': 'traefik.http.routers.{{ project_name }}.tls.certresolver', 'value': 'le_main'}
- { 'key': 'traefik.http.services.{{ project_name }}.loadbalancer.server.port', 'value': '80'}
ISSUE TYPE
- Feature Idea
COMPONENT NAME
community.docker.docker_container
ADDITIONAL INFORMATION
Allows to use templating in keys which is useful, e.g for traefik.
Example: Currently this VERY ugly workaround has to be used:
- name: Create app container
community.docker.docker_container:
name: "{{ project_name }}_app"
image: "vaultwarden/server:1.25.1"
labels:
- "traefik.enable=true"
- "traefik.http.routers.wekan.rule=Host(`{{ traefik_host }}`)"
- "traefik.http.routers.wekan.entrypoints=web"
- "traefik.http.routers.wekan.tls.certresolver=le_main"
- "traefik.http.routers.wekan.entrypoints=websecure"
I'm not 100% sure how you prefer to provide the labels (list of dictionaries? list of strings?), but both ways can easily be converted to a dictionary:
- hosts: localhost
gather_facts: false
tasks:
- debug:
msg: "{{ labels_list | items2dict }}"
vars:
labels_list:
- key: foo
value: bar
- key: 1
value: 2
- debug:
msg: "{{ dict(labels_list | map('split', '=', 1)) }}"
vars:
labels_list:
- foo=bar=baz
- 1=2
Output:
PLAY [localhost] *************************************************************************************************
TASK [debug] *****************************************************************************************************
ok: [localhost] => {
"msg": {
"foo": "bar",
"1": 2
}
}
TASK [debug] *****************************************************************************************************
ok: [localhost] => {
"msg": {
"1": "2",
"foo": "bar=baz"
}
}
PLAY RECAP *******************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
@felixfontein Thanks, that already looks a lot better, but still a bit hacky IMO.