Last active
October 1, 2019 12:57
-
-
Save halberom/b36200260db3c54cbd56 to your computer and use it in GitHub Desktop.
ansible - example of dynamic even/odd groups
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# requirement | |
# given 2 groups (A and B), target 50% of each for code deploy | |
- hosts: localhost | |
tasks: | |
# this might need to be group_by or add_host | |
- set_fact: | |
even: "{{ groups['groupA'][::2] | union(groups['groupB'][::2] }}" | |
odd: "{{ groups['groupA'][1::2] | union(groups['groupB'][1::2] }}" | |
- hosts: even | |
roles: | |
- deploy something | |
- hosts: odd | |
roles: | |
- deploy something | |
# it may also be possible to do | |
#- hosts: even;odd | |
# serial: 50% |
Thanks for this trick.
I think it's not necessary to do set_fact (except to increase readibility):
- name: work on odd servers
hosts: "{{ groups['web'][::2] | union(groups['database'][::2]) }}"
tasks:
- name: reboot odd servers
debug:
msg: "I will reboot odd server: {{ ansible_hostname}}"
- name: work on even servers
hosts: "{{ groups['web'][1::2] | union(groups['database'][1::2]) }}"
tasks:
- name: reboot even servers
debug:
msg: "I will reboot even server: {{ ansible_hostname}}"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I can make sure I assigned a list of hosts to the fact
even
in the first play, but in the second play I still got theno hosts matched
error.Can anyone give some hints?