Skip to content

Instantly share code, notes, and snippets.

@Kukunin
Created June 16, 2017 20:32
Show Gist options
  • Save Kukunin/05ad4852a06c0e828c238f56d515e68a to your computer and use it in GitHub Desktop.
Save Kukunin/05ad4852a06c0e828c238f56d515e68a to your computer and use it in GitHub Desktop.
Uniform distribution of servers in ansible
- hosts: workers
gather_facts: no
pre_tasks:
- set_fact: sequence_number="{{ ansible_default_ipv4['macaddress'].split(':')[-1] | int('', 16) }}"
- set_fact: daemons_count="{{ groups['daemons'] | length }}"
- set_fact: daemon_i='{{ (sequence_number|int) % (daemons_count|int)}}'
- set_fact: daemon_host="{{ hostvars[groups['daemons'][daemon_i|int]]['ansible_eth0']['ipv4']['address'] }}"
- debug: var=daemon_i
roles:
- role: worker
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment