--- - name: Generate the command to join a node as swarm worker hosts: localhost gather_facts: yes tasks: - name: Determine Docker swarm status shell: > docker info | egrep 'Swarm: ' register: swarm_status become: true - name: Prepare the command to join a node as worker in this swarm command: docker swarm join-token worker -q become: true when: "' Swarm: active' in swarm_status.stdout_lines" register: worker_join_token - set_fact: run_on_worker: "sudo docker swarm join --token {{ worker_join_token.stdout }} {{ ansible_eth0.ipv4.address }}:2377" - debug: var=run_on_worker