# \[community.docker.docker\_node\] Setting labels does not run on manager node

**URL:** <https://forum.ansible.com/t/community-docker-docker-node-setting-labels-does-not-run-on-manager-node/10656>\
**Category:** Get Help\
**Tags:** docker-swarm, collections\
**Created:** [November 3, 2024, 2:43pm UTC](https://forum.ansible.com/t/community-docker-docker-node-setting-labels-does-not-run-on-manager-node/10656 "2024-11-03T14:43:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![FSeidinger](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/fseidinger/32/5338_2.png) [@FSeidinger](https://forum.ansible.com/u/FSeidinger)\
**Post date:** [November 3, 2024, 2:43pm UTC](https://forum.ansible.com/t/community-docker-docker-node-setting-labels-does-not-run-on-manager-node/10656/1 "2024-11-03T14:43:50Z")

</div>

I am using ansible `community.docker` modules to manage a docker swarm cluster. So far the initialization of manager and worker nodes does work great.

Now I try to label nodes in the cluster and use the `community.docker.docker_node` for that. When I try to label the nodes, I get an error from the node telling me that this module must be run on a manger node.

```auto
fatal: [aragorn.m.internal]: FAILED! => {"changed": false, "msg": "Error running docker swarm module: must run on swarm manager node"}

```

How do I do that? Checked the module syntax and examples but could not get a clue about it.

My code looks like that:

```auto
- name: Manage docker swarm cluster
  hosts: swarm
  tasks:

  ...

    - name: Set Labels on swarm nodes
      community.docker.docker_node:
        hostname: "{{ inventory_hostname_short }}"
        labels: "{{ swarm_labels }}"
        labels_state: replace

```

Whereas `swarm` is the group of all nodes of the swarm and `swarm_labels` is a dictionary of labels given as a host variable of the host to set the labels on. Only a few of them are managers.

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [November 3, 2024, 3:10pm UTC](https://forum.ansible.com/t/community-docker-docker-node-setting-labels-does-not-run-on-manager-node/10656/2 "2024-11-03T15:10:39Z")

</div>

The module needs to be run on one of the host(s) that’s a manager node.

Something like:

```yaml
    - name: Set Labels on swarm nodes
      community.docker.docker_node:
        hostname: "{{ inventory_hostname_short }}"
        labels: "{{ swarm_labels }}"
        labels_state: replace
      delegate_to: "{{ swarm_manager_node }}"

```

where `swarm_manager_node` is the inventory hostname of a manager node.

---

<div class="post-metadata">

**Author:** ![FSeidinger](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/fseidinger/32/5338_2.png) [@FSeidinger](https://forum.ansible.com/u/FSeidinger)\
**Post date:** [November 3, 2024, 8:32pm UTC](https://forum.ansible.com/t/community-docker-docker-node-setting-labels-does-not-run-on-manager-node/10656/3 "2024-11-03T20:32:20Z")

</div>

> [@felixfontein](#):
>
> `delegate_to`

I tried that already. This will only work if the delegated target is the master node. For all worker nodes this gives an error like:

```auto
fatal: [concierge.m.internal -> camina.m.internal]: FAILED! => {"changed": false, "msg": "Failed to update node : 400 Client Error for http+docker://localhost/v1.47/nodes/sxrdnv670whbk2ifo96pw1qrj/update?version=3152: Bad Request (\"invalid JSON: json: cannot unmarshal number into Go struct field NodeSpec.Labels of type string\")"}

```

Looks like the connection is not made via docker socket but it tries to contact via http.

And the 400er status code means bad request, which means the module does not create a valid request.

Unfortunately the documentation does not give any further hints but only shows the example I used in this post.

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [November 3, 2024, 8:53pm UTC](https://forum.ansible.com/t/community-docker-docker-node-setting-labels-does-not-run-on-manager-node/10656/4 "2024-11-03T20:53:12Z")

</div>

That error message sounds like you are trying to set a label to an integer value, and not to a string.

---

<div class="post-metadata">

**Author:** ![FSeidinger](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/fseidinger/32/5338_2.png) [@FSeidinger](https://forum.ansible.com/u/FSeidinger)\
**Post date:** [November 4, 2024, 12:46pm UTC](https://forum.ansible.com/t/community-docker-docker-node-setting-labels-does-not-run-on-manager-node/10656/5 "2024-11-04T12:46:22Z")

</div>

Oh, mea culpa. You are totally right. I guess I still have to go a long way with ansible. Giving the label as strings worked perfectly.

Here is a draft from my inventory, in case others come over this post:

```auto
swarm:
    children:
        swarm_masters:
            hosts:
                camina.m.internal:
                    swarm_advertise_addr: 192.168.1.4
                    swarm_data_path_addr: 192.168.2.8
                    swarm_labels:
                        capacity: "small"

        swarm_nodes:
            hosts:
                concierge.m.internal:
                    swarm_advertise_addr: 192.168.1.3
                    swarm_data_path_addr: 192.168.2.3
                    swarm_labels:
                        capacity: "large"
                        rack: "1"

        ...

```

My problem was to assign the rack label as integer and not as string, as you already guessed.

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [November 4, 2024, 6:02pm UTC](https://forum.ansible.com/t/community-docker-docker-node-setting-labels-does-not-run-on-manager-node/10656/6 "2024-11-04T18:02:17Z")

</div>

The module should be fixed so that it automatically converts non-strings to strings (or warns/fails for things that cannot be converted correctly, like booleans), but fixing the types on your side is of course the better way 🙂 There’s also always the problem with having `1.10` ending up as `1.1` because it’s treated as a floating point number 😉 (Integers are safe though, except if you happen to use hexadecimal or octal encoding, which will be gone by the time when they’re converted back to strings…)

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [November 5, 2024, 7:55pm UTC](https://forum.ansible.com/t/community-docker-docker-node-setting-labels-does-not-run-on-manager-node/10656/7 "2024-11-05T19:55:26Z")

</div>

I created [Sanitize labels by felixfontein · Pull Request #985 · ansible-collections/community.docker · GitHub](https://github.com/ansible-collections/community.docker/pull/985) which should fix this (also for some more modules).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [December 5, 2024, 7:56pm UTC](https://forum.ansible.com/t/community-docker-docker-node-setting-labels-does-not-run-on-manager-node/10656/8 "2024-12-05T19:56:20Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
