-
Notifications
You must be signed in to change notification settings - Fork 13
Add node priority configuration #317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
f7e20c7
3853805
2d8537a
b76fcb0
9d98540
6d13f7b
fd97f31
214797a
16e5a2c
d43cb82
75fad81
05c7f87
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -25,10 +25,12 @@ | |||||||
| register: mongodb_state | ||||||||
| vars: | ||||||||
| ansible_python_interpreter: "{{ mongodb_python_venv }}/bin/python3" | ||||||||
| tags: reconfigure_priority | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure we need this tag. And if it is needed, I'm not sure it's name appropriately. |
||||||||
|
|
||||||||
| - name: Print MongoDB configuration state | ||||||||
| ansible.builtin.debug: | ||||||||
| msg: "{{ mongodb_state }}" | ||||||||
| tags: reconfigure_priority | ||||||||
|
|
||||||||
| # Execute the template to apply changes to the mongo.conf for replication | ||||||||
| - name: Create MongoDB config file (replicaset) | ||||||||
|
|
@@ -56,28 +58,116 @@ | |||||||
| - name: Set empty array of mongo servers | ||||||||
| ansible.builtin.set_fact: | ||||||||
| mongodb_servers: [] | ||||||||
| when: not mongodb_state.replication_enabled | ||||||||
| when: | ||||||||
| - inventory_hostname in groups.mongodb | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you need any of these when clauses. The role is only executed on mongodb hosts. |
||||||||
| - groups.mongodb.index(inventory_hostname) == 0 | ||||||||
| tags: reconfigure_priority | ||||||||
|
|
||||||||
| - name: Set preferred primary host | ||||||||
| ansible.builtin.set_fact: | ||||||||
| mongodb_preferred_primary_host: "{{ groups.mongodb[0] }}" | ||||||||
| mongodb_preferred_primary_resolved: "{{ mongodb_preferred_primary | default('', true) | length == 0 }}" | ||||||||
| when: | ||||||||
| - inventory_hostname in groups.mongodb | ||||||||
| - groups.mongodb.index(inventory_hostname) == 0 | ||||||||
| tags: reconfigure_priority | ||||||||
|
|
||||||||
| - name: Normalize preferred primary override | ||||||||
| ansible.builtin.set_fact: | ||||||||
| mongodb_preferred_primary_input: >- | ||||||||
| {{ mongodb_preferred_primary | regex_replace(':' + (mongodb_port | string) + '$', '') }} | ||||||||
| when: | ||||||||
| - inventory_hostname in groups.mongodb | ||||||||
| - groups.mongodb.index(inventory_hostname) == 0 | ||||||||
| - mongodb_preferred_primary | default('', true) | length > 0 | ||||||||
| tags: reconfigure_priority | ||||||||
|
|
||||||||
| - name: Resolve preferred primary override by inventory hostname | ||||||||
| ansible.builtin.set_fact: | ||||||||
| mongodb_preferred_primary_host: "{{ mongodb_preferred_primary_input }}" | ||||||||
| mongodb_preferred_primary_resolved: true | ||||||||
| when: | ||||||||
| - inventory_hostname in groups.mongodb | ||||||||
| - groups.mongodb.index(inventory_hostname) == 0 | ||||||||
| - mongodb_preferred_primary | default('', true) | length > 0 | ||||||||
| - mongodb_preferred_primary_input in groups.mongodb | ||||||||
| tags: reconfigure_priority | ||||||||
|
|
||||||||
| - name: Resolve preferred primary override by ansible_host or short hostname | ||||||||
| ansible.builtin.set_fact: | ||||||||
| mongodb_preferred_primary_host: "{{ item }}" | ||||||||
| mongodb_preferred_primary_resolved: true | ||||||||
| loop: "{{ groups.mongodb }}" | ||||||||
| when: | ||||||||
| - inventory_hostname in groups.mongodb | ||||||||
| - groups.mongodb.index(inventory_hostname) == 0 | ||||||||
| - mongodb_preferred_primary | default('', true) | length > 0 | ||||||||
| - mongodb_preferred_primary_input not in groups.mongodb | ||||||||
| - mongodb_preferred_primary_input in [ | ||||||||
| item, | ||||||||
| (item.split('.')[0]), | ||||||||
| (hostvars[item].ansible_host | default('')) | ||||||||
| ] | ||||||||
| tags: reconfigure_priority | ||||||||
|
|
||||||||
| - name: Validate preferred primary override | ||||||||
| ansible.builtin.assert: | ||||||||
| that: | ||||||||
| - mongodb_preferred_primary_resolved | bool | ||||||||
|
Comment on lines
+115
to
+116
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| fail_msg: >- | ||||||||
| mongodb_preferred_primary must match a host in groups.mongodb. | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| Received {{ mongodb_preferred_primary }}. | ||||||||
| Valid inventory hosts: {{ groups.mongodb | join(', ') }} | ||||||||
| when: | ||||||||
| - inventory_hostname in groups.mongodb | ||||||||
| - groups.mongodb.index(inventory_hostname) == 0 | ||||||||
| - mongodb_preferred_primary | default('', true) | length > 0 | ||||||||
| tags: reconfigure_priority | ||||||||
|
|
||||||||
| # This task should always run, arbiter or not | ||||||||
| - name: Create the replicaset members list (no arbiter) | ||||||||
| ansible.builtin.set_fact: | ||||||||
| mongodb_servers: "{{ mongodb_servers + [item + ':' + mongodb_port | string] }}" | ||||||||
| with_items: "{{ groups.mongodb }}" | ||||||||
| mongodb_servers: >- | ||||||||
| {{ mongodb_servers + [{'host': item + ':' + (mongodb_port | string), | ||||||||
| 'priority': ((item == mongodb_preferred_primary_host) | | ||||||||
| ternary(mongodb_primary_priority, mongodb_secondary_priority))}] | ||||||||
| }} | ||||||||
| loop: "{{ groups.mongodb }}" | ||||||||
| when: | ||||||||
| - not mongodb_state.replication_enabled | ||||||||
| - inventory_hostname in groups.mongodb | ||||||||
| - groups.mongodb.index(inventory_hostname) == 0 | ||||||||
| tags: reconfigure_priority | ||||||||
|
|
||||||||
| # This task will only run when there is an arbiter defined in the hosts file | ||||||||
| - name: Add the arbiter to the list of servers when there is one | ||||||||
| ansible.builtin.set_fact: | ||||||||
| mongodb_servers: "{{ mongodb_servers + [item + ':' + mongodb_port | string] }}" | ||||||||
| with_items: "{{ groups.mongodb_arbiter }}" | ||||||||
| mongodb_servers: >- | ||||||||
| {{ mongodb_servers + [{'host': item + ':' + (mongodb_port | string), 'priority': 0}] }} | ||||||||
| loop: "{{ groups.mongodb_arbiter }}" | ||||||||
| when: | ||||||||
| - not mongodb_state.replication_enabled | ||||||||
| - inventory_hostname in groups.mongodb | ||||||||
| - groups.mongodb.index(inventory_hostname) == 0 | ||||||||
| - groups.mongodb_arbiter is defined | ||||||||
| tags: reconfigure_priority | ||||||||
|
|
||||||||
| - name: Debug replication state | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| ansible.builtin.debug: | ||||||||
| msg: "replication_enabled={{ mongodb_state.replication_enabled }} primary={{ mongodb_state.primary }} members={{ mongodb_state.members }}" | ||||||||
| when: | ||||||||
| - inventory_hostname in groups.mongodb | ||||||||
| - groups.mongodb.index(inventory_hostname) == 0 | ||||||||
| tags: reconfigure_priority | ||||||||
|
|
||||||||
| - name: Wait for all MongoDB members to be reachable before creating the replicaset | ||||||||
| ansible.builtin.wait_for: | ||||||||
| host: "{{ item }}" | ||||||||
| port: "{{ mongodb_port }}" | ||||||||
| timeout: 60 | ||||||||
| loop: "{{ groups.mongodb + (groups.mongodb_arbiter | default([])) }}" | ||||||||
| when: | ||||||||
| - not mongodb_state.replication_enabled | ||||||||
| - inventory_hostname in groups.mongodb | ||||||||
| - groups.mongodb.index(inventory_hostname) == 0 | ||||||||
|
|
||||||||
| - name: Create the replicaset | ||||||||
| community.mongodb.mongodb_replicaset: | ||||||||
|
|
@@ -119,6 +209,94 @@ | |||||||
| vars: | ||||||||
| ansible_python_interpreter: "{{ mongodb_python_venv }}/bin/python3" | ||||||||
|
|
||||||||
| - name: Refresh MongoDB configuration state after replicaset changes | ||||||||
| itential.deployer.mongodb_config_state: | ||||||||
| login_database: admin | ||||||||
| login_host: "{{ inventory_hostname }}" | ||||||||
| login_port: "{{ mongodb_port }}" | ||||||||
| register: mongodb_state_after_replicaset | ||||||||
| when: | ||||||||
| - inventory_hostname in groups.mongodb | ||||||||
| - groups.mongodb.index(inventory_hostname) == 0 | ||||||||
| vars: | ||||||||
| ansible_python_interpreter: "{{ mongodb_python_venv }}/bin/python3" | ||||||||
| tags: reconfigure_priority | ||||||||
|
|
||||||||
| - name: Reconfigure the replicaset priorities | ||||||||
| community.mongodb.mongodb_replicaset: | ||||||||
| arbiter_at_index: "{{ (groups.mongodb_arbiter | default([]) | length > 0) | ternary(mongodb_servers | length - 1, omit) }}" | ||||||||
| auth_mechanism: "SCRAM-SHA-256" | ||||||||
| login_user: "{{ (mongodb_state.auth_enabled | bool) | ternary(mongodb_user_admin, omit) }}" | ||||||||
| login_password: "{{ (mongodb_state.auth_enabled | bool) | ternary(mongodb_user_admin_password, omit) }}" | ||||||||
| login_port: "{{ mongodb_port }}" | ||||||||
| login_database: admin | ||||||||
| login_host: "{{ mongodb_state_after_replicaset.primary | regex_replace(':.*$', '') }}" | ||||||||
| members: "{{ mongodb_servers }}" | ||||||||
| replica_set: "{{ mongodb_replset_name }}" | ||||||||
| reconfigure: true | ||||||||
| validate: true | ||||||||
| register: reconfig_result | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Registered variables should be prefixed by the role name.
Suggested change
|
||||||||
| when: | ||||||||
| - inventory_hostname in groups.mongodb | ||||||||
| - groups.mongodb.index(inventory_hostname) == 0 | ||||||||
| - mongodb_state_after_replicaset.primary is defined | ||||||||
| - mongodb_state_after_replicaset.primary | length > 0 | ||||||||
| vars: | ||||||||
| ansible_python_interpreter: "{{ mongodb_python_venv }}/bin/python3" | ||||||||
| tags: reconfigure_priority | ||||||||
|
|
||||||||
| - name: Print reconfigure result | ||||||||
| ansible.builtin.debug: | ||||||||
| msg: "{{ reconfig_result }}" | ||||||||
| when: | ||||||||
| - reconfig_result is defined | ||||||||
| - reconfig_result is not skipped | ||||||||
| tags: reconfigure_priority | ||||||||
|
|
||||||||
| - name: Step down non-preferred primary after replicaset changes | ||||||||
| community.mongodb.mongodb_shell: | ||||||||
| mongo_cmd: auto | ||||||||
| login_user: "{{ mongodb_state.auth_enabled | ternary(mongodb_user_admin, omit) }}" | ||||||||
| login_password: "{{ mongodb_state.auth_enabled | ternary(mongodb_user_admin_password, omit) }}" | ||||||||
| login_port: "{{ mongodb_port }}" | ||||||||
| login_database: admin | ||||||||
| login_host: "{{ mongodb_state_after_replicaset.primary | regex_replace(':.*$', '') }}" | ||||||||
| eval: "db.adminCommand({replSetStepDown: 60, force: true})" | ||||||||
| register: stepdown_result | ||||||||
| failed_when: false | ||||||||
| when: | ||||||||
| - inventory_hostname in groups.mongodb | ||||||||
| - groups.mongodb.index(inventory_hostname) == 0 | ||||||||
| - reconfig_result is changed | ||||||||
| - mongodb_state_after_replicaset.primary is defined | ||||||||
| - (mongodb_state_after_replicaset.primary | regex_replace(':.*$', '')) != mongodb_preferred_primary_host | ||||||||
| vars: | ||||||||
| ansible_python_interpreter: "{{ mongodb_python_venv }}/bin/python3" | ||||||||
| tags: reconfigure_priority | ||||||||
|
|
||||||||
| - name: Ensure replicaset is stable after primary stepdown | ||||||||
| community.mongodb.mongodb_status: | ||||||||
| login_user: "{{ mongodb_state.auth_enabled | ternary(mongodb_user_admin, omit) }}" | ||||||||
| login_password: "{{ mongodb_state.auth_enabled | ternary(mongodb_user_admin_password, omit) }}" | ||||||||
| login_port: "{{ mongodb_port }}" | ||||||||
| login_database: admin | ||||||||
| login_host: "{{ inventory_hostname }}" | ||||||||
| replica_set: "{{ mongodb_replset_name }}" | ||||||||
| poll: "{{ mongodb_status_poll }}" | ||||||||
| interval: "{{ mongodb_status_interval }}" | ||||||||
| validate: minimal | ||||||||
| register: rs_after_stepdown | ||||||||
| failed_when: | ||||||||
| - "'Unable to determine if auth is enabled' not in rs_after_stepdown.msg" | ||||||||
| - "'replicaset is in a converged state' not in rs_after_stepdown.msg" | ||||||||
| when: | ||||||||
| - inventory_hostname in groups.mongodb | ||||||||
| - stepdown_result is defined | ||||||||
| - stepdown_result is not skipped | ||||||||
| vars: | ||||||||
| ansible_python_interpreter: "{{ mongodb_python_venv }}/bin/python3" | ||||||||
| tags: reconfigure_priority | ||||||||
|
|
||||||||
| # Starting in MongoDB 5.0, the implicit default write concern is w: majority. | ||||||||
| # However, special considerations are made for deployments containing arbiters: | ||||||||
| # The voting majority of a replica set is 1 plus half the number of voting | ||||||||
|
|
||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of setting the default to empty, why not default it to the first server in the list? So something like:
That way you don't have to write any code to set it.