1

I'm creating playbook which will be applied to new Docker swarm manager(s). Server(s) is/are not configured before playbook run.

We already have some Swarm managers. I can find all of them (include new one) with:

- name: 'Search for SwarmManager server IPs'
  ec2_instance_facts:
    region: "{{ ec2_region }}"
    filters:
      vpc-id: "{{ ec2_vpc_id }}"
      "tag:aws:cloudformation:logical-id": "AutoScalingGroupSwarmManager"
  register: swarmmanager_instance_facts_result

Now I can use something like this to get join-token:

- set_fact:
    swarmmanager_ip: "{{ swarmmanager_instance_facts_result.instances[0].private_ip_address }}"

- name: 'Get the docker swarm join-token'
  shell: docker swarm join-token -q manager
  changed_when: False
  register: docker_swarm_token_result
  delegate_to: "{{ swarmmanager_ip }}"
  run_once: true

Success shell output looks like this — just 1 line started with "SWMTKN-1":

SWMTKN-1-11xxxyyyzzz-xxxyyyzzz

But I see some possible problems here with swarmmanager_ip:

  • it can be new instance which still unconfigured,
  • it can be instance with not working Swarm manager.

So I decided to loop over results until I've got join-token. But many code variants I've tried doesn't work. For example, this one runs over all list without break:

- name: 'Get the docker swarm join-token'
  shell: docker swarm join-token -q manager
  changed_when: False
  register: docker_swarm_token_result
  delegate_to: "{{ item.private_ip_address }}"
  loop: "{{ swarmmanager_instance_facts_result.instances }}"
  # ignore_errors: true
  # until: docker_swarm_token_result.stdout_lines|length == 1
  when: docker_swarm_token_result is not defined or docker_swarm_token_result.stdout_lines is not defined or docker_swarm_token_result.stdout_lines|length == 1
  run_once: true
  check_mode: false

Do you know how to iterate over list until first success shell output?

I use Ansible 2.6.11, it is OK to receive answer about 2.7.

P.S.: I've already read How to break `with_lines` cycle in Ansible?, it doesn't works for modern Ansible versions.

Alexey Vazhnov
  • 1,291
  • 17
  • 20
  • Surely there is a pre-flight command you can run that would inform your playbook whether asking for a `join-token` _could_ succeed, right? – mdaniel Dec 26 '18 at 04:12
  • @MatthewLDaniel, I've already wrote «Success shell output looks like this…», why do I need in pre-flight command? – Alexey Vazhnov Dec 26 '18 at 05:48

0 Answers0