Summary
In roles/elasticsearch/tasks/main.yml the task Count master nodes reads the
globally-accumulated Ansible group elasticsearch_role_master instead of
scoping to the hosts of the current play. When two Elasticsearch clusters (a
primary cluster and a separate monitoring cluster) are deployed from the same
ansible-playbook invocation — both running the same role — group_by appends
hosts from all plays to the same global group. The assertion that asserts an
odd master count then sees the sum across both clusters, not the count of the
current cluster.
Reproduction
-
Inventory with two cluster groups, e.g.:
[elasticsearch]
master-001
master-002
master-003
[elasticsearch_mon]
monitor-001 # single-node monitoring cluster, also master-eligible
-
Playbook that runs oddly.elasticstack.elasticsearch on both groups
in the same run (separate plays).
-
Deploy passes the main cluster (3 masters, odd). During the monitor
cluster play, group_by adds monitor-001 to the same
elasticsearch_role_master group. Count master nodes now returns 4.
-
Check count of master nodes fails:
There must be an odd count of master nodes. You have 4
Expected
Each cluster's master count is evaluated against the hosts in the current play
only. A 3-node main cluster passes (3 is odd). A 1-node monitor cluster also
passes (1 is odd).
Suggested fix
Scope the count to ansible_play_hosts_all (the hosts of the current play):
- name: Count master nodes
ansible.builtin.set_fact:
- elasticsearch_count_of_master_nodes: "{{ groups['elasticsearch_role_master'] | length }}"
+ elasticsearch_count_of_master_nodes: >-
+ {{ ansible_play_hosts_all | intersect(groups['elasticsearch_role_master'] | default([])) | length }}
Semantics: count the master-eligible nodes among the hosts of this play,
which is the cluster being deployed.
Environment
- Collection:
oddly.elasticstack current main
- Ansible 2.19.x
- Two-cluster (main + monitoring) setup
Summary
In
roles/elasticsearch/tasks/main.ymlthe taskCount master nodesreads theglobally-accumulated Ansible group
elasticsearch_role_masterinstead ofscoping to the hosts of the current play. When two Elasticsearch clusters (a
primary cluster and a separate monitoring cluster) are deployed from the same
ansible-playbookinvocation — both running the same role —group_byappendshosts from all plays to the same global group. The assertion that asserts an
odd master count then sees the sum across both clusters, not the count of the
current cluster.
Reproduction
Inventory with two cluster groups, e.g.:
Playbook that runs
oddly.elasticstack.elasticsearchon both groupsin the same run (separate plays).
Deploy passes the main cluster (3 masters, odd). During the monitor
cluster play,
group_byaddsmonitor-001to the sameelasticsearch_role_mastergroup.Count master nodesnow returns 4.Check count of master nodesfails:Expected
Each cluster's master count is evaluated against the hosts in the current play
only. A 3-node main cluster passes (3 is odd). A 1-node monitor cluster also
passes (1 is odd).
Suggested fix
Scope the count to
ansible_play_hosts_all(the hosts of the current play):Semantics: count the master-eligible nodes among the hosts of this play,
which is the cluster being deployed.
Environment
oddly.elasticstackcurrentmain