From f7e20c77e1a167fa017034e726a929a7b9cfc86f Mon Sep 17 00:00:00 2001 From: Marcos Dias Date: Mon, 2 Feb 2026 23:24:17 -0300 Subject: [PATCH 01/14] Added NUMA detection and configure mongodb to work correctly --- .../mongodb/tasks/configure-mongodb-numa.yml | 120 ++++++++++++++++++ roles/mongodb/tasks/install-mongodb.yml | 5 + 2 files changed, 125 insertions(+) create mode 100644 roles/mongodb/tasks/configure-mongodb-numa.yml diff --git a/roles/mongodb/tasks/configure-mongodb-numa.yml b/roles/mongodb/tasks/configure-mongodb-numa.yml new file mode 100644 index 00000000..a78ea015 --- /dev/null +++ b/roles/mongodb/tasks/configure-mongodb-numa.yml @@ -0,0 +1,120 @@ +# Copyright (c) 2025, Itential, Inc +# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) +--- + +- name: Detect NUMA nodes + ansible.builtin.shell: + cmd: | + if [ -d /sys/devices/system/node ]; then + ls -d /sys/devices/system/node/node[0-9]* 2>/dev/null | wc -l + else + echo 0 + fi + args: + executable: /bin/bash + register: mongodb_numa_nodes + changed_when: false + become: true + +- name: Set NUMA enabled fact + ansible.builtin.set_fact: + mongodb_numa_enabled: "{{ (mongodb_numa_nodes.stdout | int) > 1 }}" + +- name: Ensure numactl is installed when NUMA is enabled + ansible.builtin.package: + name: numactl + state: present + when: mongodb_numa_enabled + become: true + +- name: Discover numactl path + ansible.builtin.shell: + cmd: "command -v numactl || true" + args: + executable: /bin/bash + register: mongodb_numactl_path_result + changed_when: false + when: mongodb_numa_enabled + become: true + +- name: Discover mongod path + ansible.builtin.shell: + cmd: "command -v mongod || true" + args: + executable: /bin/bash + register: mongodb_mongod_path_result + changed_when: false + when: mongodb_numa_enabled + become: true + +- name: Set NUMA helper facts + ansible.builtin.set_fact: + mongodb_numactl_path: "{{ mongodb_numactl_path_result.stdout | default('', true) }}" + mongodb_mongod_path: "{{ mongodb_mongod_path_result.stdout | default('/usr/bin/mongod', true) }}" + mongodb_numactl_present: "{{ (mongodb_numactl_path_result.stdout | default('', true) | length) > 0 }}" + when: mongodb_numa_enabled + +- name: Apply NUMA sysctl settings + ansible.posix.sysctl: + name: vm.zone_reclaim_mode + value: "{{ mongodb_vm_zone_reclaim_mode }}" + state: present + sysctl_file: "{{ mongodb_sysctl_file }}" + reload: true + when: mongodb_numa_enabled + become: true + +- name: Disable kernel NUMA balancing + ansible.posix.sysctl: + name: kernel.numa_balancing + value: "0" + state: present + sysctl_file: "{{ mongodb_sysctl_file }}" + reload: true + when: mongodb_numa_enabled + become: true + +- name: Ensure mongod systemd drop-in directory exists + ansible.builtin.file: + path: /etc/systemd/system/mongod.service.d + state: directory + mode: "0755" + when: + - mongodb_numa_enabled + - mongodb_numactl_present + become: true + +- name: Configure mongod to run with numactl interleave + ansible.builtin.copy: + dest: /etc/systemd/system/mongod.service.d/numa.conf + mode: "0644" + content: | + [Service] + ExecStart= + ExecStart={{ mongodb_numactl_path }} --interleave=all {{ mongodb_mongod_path }} --config {{ mongodb_conf_file }} + register: mongodb_numa_dropin + when: + - mongodb_numa_enabled + - mongodb_numactl_present + become: true + +- name: Reload systemd if NUMA drop-in changed + ansible.builtin.systemd: + daemon_reload: true + when: + - mongodb_numa_enabled + - mongodb_numactl_present + - mongodb_numa_dropin is defined + - mongodb_numa_dropin.changed + become: true + +- name: Restart mongod to apply NUMA settings + ansible.builtin.systemd: + name: mongod + state: restarted + when: + - mongodb_numa_enabled + - mongodb_numactl_present + - mongodb_numa_dropin is defined + - mongodb_numa_dropin.changed + become: true diff --git a/roles/mongodb/tasks/install-mongodb.yml b/roles/mongodb/tasks/install-mongodb.yml index d2d1784b..82ed07cb 100644 --- a/roles/mongodb/tasks/install-mongodb.yml +++ b/roles/mongodb/tasks/install-mongodb.yml @@ -68,6 +68,11 @@ file: configure-mongodb-logrotate.yml tags: configure_logrotate +- name: Configure NUMA for MongoDB + ansible.builtin.include_tasks: + file: configure-mongodb-numa.yml + tags: configure_numa + # Check if firewalld is running, if it is then open the appropriate ports - name: Gather service facts ansible.builtin.service_facts: From 3853805e8da8b3bb4f231d9cd0212bbb4eaaeab1 Mon Sep 17 00:00:00 2001 From: Marcos Dias Date: Tue, 3 Feb 2026 12:43:31 -0300 Subject: [PATCH 02/14] Remove redundant become directives in MongoDB config Removed unnecessary 'become: true' directives from various tasks in the configure-mongodb-numa.yml file. --- roles/mongodb/tasks/configure-mongodb-numa.yml | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/roles/mongodb/tasks/configure-mongodb-numa.yml b/roles/mongodb/tasks/configure-mongodb-numa.yml index a78ea015..0b3ebe2e 100644 --- a/roles/mongodb/tasks/configure-mongodb-numa.yml +++ b/roles/mongodb/tasks/configure-mongodb-numa.yml @@ -14,7 +14,6 @@ executable: /bin/bash register: mongodb_numa_nodes changed_when: false - become: true - name: Set NUMA enabled fact ansible.builtin.set_fact: @@ -25,7 +24,6 @@ name: numactl state: present when: mongodb_numa_enabled - become: true - name: Discover numactl path ansible.builtin.shell: @@ -45,7 +43,6 @@ register: mongodb_mongod_path_result changed_when: false when: mongodb_numa_enabled - become: true - name: Set NUMA helper facts ansible.builtin.set_fact: @@ -62,7 +59,6 @@ sysctl_file: "{{ mongodb_sysctl_file }}" reload: true when: mongodb_numa_enabled - become: true - name: Disable kernel NUMA balancing ansible.posix.sysctl: @@ -72,7 +68,6 @@ sysctl_file: "{{ mongodb_sysctl_file }}" reload: true when: mongodb_numa_enabled - become: true - name: Ensure mongod systemd drop-in directory exists ansible.builtin.file: @@ -82,7 +77,6 @@ when: - mongodb_numa_enabled - mongodb_numactl_present - become: true - name: Configure mongod to run with numactl interleave ansible.builtin.copy: @@ -96,7 +90,6 @@ when: - mongodb_numa_enabled - mongodb_numactl_present - become: true - name: Reload systemd if NUMA drop-in changed ansible.builtin.systemd: @@ -106,7 +99,6 @@ - mongodb_numactl_present - mongodb_numa_dropin is defined - mongodb_numa_dropin.changed - become: true - name: Restart mongod to apply NUMA settings ansible.builtin.systemd: @@ -117,4 +109,4 @@ - mongodb_numactl_present - mongodb_numa_dropin is defined - mongodb_numa_dropin.changed - become: true + From 2d8537abaf46cac1c82f3c78705296e3c8b79fb1 Mon Sep 17 00:00:00 2001 From: Marcos Dias Date: Tue, 3 Feb 2026 14:09:20 -0300 Subject: [PATCH 03/14] Refactor systemd reload and restart tasks for mongod to streamline NUMA configuration --- roles/mongodb/tasks/configure-mongodb-numa.yml | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/roles/mongodb/tasks/configure-mongodb-numa.yml b/roles/mongodb/tasks/configure-mongodb-numa.yml index 0b3ebe2e..320619e6 100644 --- a/roles/mongodb/tasks/configure-mongodb-numa.yml +++ b/roles/mongodb/tasks/configure-mongodb-numa.yml @@ -91,22 +91,14 @@ - mongodb_numa_enabled - mongodb_numactl_present -- name: Reload systemd if NUMA drop-in changed - ansible.builtin.systemd: - daemon_reload: true - when: - - mongodb_numa_enabled - - mongodb_numactl_present - - mongodb_numa_dropin is defined - - mongodb_numa_dropin.changed - -- name: Restart mongod to apply NUMA settings +- name: Reload systemd and restart mongod to apply NUMA settings ansible.builtin.systemd: name: mongod state: restarted + daemon_reload: true when: - mongodb_numa_enabled - mongodb_numactl_present - mongodb_numa_dropin is defined - mongodb_numa_dropin.changed - + From b76fcb0e12c98d4fa2bc9487065db43c4a6aef2a Mon Sep 17 00:00:00 2001 From: Marcos Dias Date: Fri, 6 Feb 2026 10:32:22 -0300 Subject: [PATCH 04/14] Refactor NUMA configuration tasks for MongoDB to improve structure and clarity --- .../mongodb/tasks/configure-mongodb-numa.yml | 139 ++++++++---------- 1 file changed, 65 insertions(+), 74 deletions(-) diff --git a/roles/mongodb/tasks/configure-mongodb-numa.yml b/roles/mongodb/tasks/configure-mongodb-numa.yml index 320619e6..5a31ec66 100644 --- a/roles/mongodb/tasks/configure-mongodb-numa.yml +++ b/roles/mongodb/tasks/configure-mongodb-numa.yml @@ -19,86 +19,77 @@ ansible.builtin.set_fact: mongodb_numa_enabled: "{{ (mongodb_numa_nodes.stdout | int) > 1 }}" -- name: Ensure numactl is installed when NUMA is enabled - ansible.builtin.package: - name: numactl - state: present +- name: Enable NUMA settings if multiple NUMA nodes detected when: mongodb_numa_enabled + block: + - name: Ensure numactl is installed when NUMA is enabled + ansible.builtin.package: + name: numactl + state: present -- name: Discover numactl path - ansible.builtin.shell: - cmd: "command -v numactl || true" - args: - executable: /bin/bash - register: mongodb_numactl_path_result - changed_when: false - when: mongodb_numa_enabled - become: true + - name: Discover numactl path + ansible.builtin.shell: + cmd: "command -v numactl || true" + args: + executable: /bin/bash + register: mongodb_numactl_path_result + changed_when: false + become: true -- name: Discover mongod path - ansible.builtin.shell: - cmd: "command -v mongod || true" - args: - executable: /bin/bash - register: mongodb_mongod_path_result - changed_when: false - when: mongodb_numa_enabled + - name: Discover mongod path + ansible.builtin.shell: + cmd: "command -v mongod || true" + args: + executable: /bin/bash + register: mongodb_mongod_path_result + changed_when: false -- name: Set NUMA helper facts - ansible.builtin.set_fact: - mongodb_numactl_path: "{{ mongodb_numactl_path_result.stdout | default('', true) }}" - mongodb_mongod_path: "{{ mongodb_mongod_path_result.stdout | default('/usr/bin/mongod', true) }}" - mongodb_numactl_present: "{{ (mongodb_numactl_path_result.stdout | default('', true) | length) > 0 }}" - when: mongodb_numa_enabled + - name: Set NUMA helper facts + ansible.builtin.set_fact: + mongodb_numactl_path: "{{ mongodb_numactl_path_result.stdout | default('', true) }}" + mongodb_mongod_path: "{{ mongodb_mongod_path_result.stdout | default('/usr/bin/mongod', true) }}" + mongodb_numactl_present: "{{ (mongodb_numactl_path_result.stdout | default('', true) | length) > 0 }}" -- name: Apply NUMA sysctl settings - ansible.posix.sysctl: - name: vm.zone_reclaim_mode - value: "{{ mongodb_vm_zone_reclaim_mode }}" - state: present - sysctl_file: "{{ mongodb_sysctl_file }}" - reload: true - when: mongodb_numa_enabled - -- name: Disable kernel NUMA balancing - ansible.posix.sysctl: - name: kernel.numa_balancing - value: "0" - state: present - sysctl_file: "{{ mongodb_sysctl_file }}" - reload: true - when: mongodb_numa_enabled + - name: Apply NUMA sysctl settings + ansible.posix.sysctl: + name: vm.zone_reclaim_mode + value: "{{ mongodb_vm_zone_reclaim_mode }}" + state: present + sysctl_file: "{{ mongodb_sysctl_file }}" + reload: true -- name: Ensure mongod systemd drop-in directory exists - ansible.builtin.file: - path: /etc/systemd/system/mongod.service.d - state: directory - mode: "0755" - when: - - mongodb_numa_enabled - - mongodb_numactl_present + - name: Disable kernel NUMA balancing + ansible.posix.sysctl: + name: kernel.numa_balancing + value: "0" + state: present + sysctl_file: "{{ mongodb_sysctl_file }}" + reload: true -- name: Configure mongod to run with numactl interleave - ansible.builtin.copy: - dest: /etc/systemd/system/mongod.service.d/numa.conf - mode: "0644" - content: | - [Service] - ExecStart= - ExecStart={{ mongodb_numactl_path }} --interleave=all {{ mongodb_mongod_path }} --config {{ mongodb_conf_file }} - register: mongodb_numa_dropin - when: - - mongodb_numa_enabled - - mongodb_numactl_present + - name: Ensure mongod systemd drop-in directory exists + ansible.builtin.file: + path: /etc/systemd/system/mongod.service.d + state: directory + mode: "0755" + when: mongodb_numactl_present -- name: Reload systemd and restart mongod to apply NUMA settings - ansible.builtin.systemd: - name: mongod - state: restarted - daemon_reload: true - when: - - mongodb_numa_enabled - - mongodb_numactl_present - - mongodb_numa_dropin is defined - - mongodb_numa_dropin.changed + - name: Configure mongod to run with numactl interleave + ansible.builtin.copy: + dest: /etc/systemd/system/mongod.service.d/numa.conf + mode: "0644" + content: | + [Service] + ExecStart= + ExecStart={{ mongodb_numactl_path }} --interleave=all {{ mongodb_mongod_path }} --config {{ mongodb_conf_file }} + register: mongodb_numa_dropin + when: mongodb_numactl_present + - name: Reload systemd and restart mongod to apply NUMA settings + ansible.builtin.systemd: + name: mongod + state: restarted + daemon_reload: true + when: + - mongodb_numactl_present + - mongodb_numa_dropin is defined + - mongodb_numa_dropin.changed From 6d13f7b6f888260b8b0691f203029a8a2a60b3ba Mon Sep 17 00:00:00 2001 From: Marcos Dias Date: Fri, 6 Feb 2026 14:50:23 -0300 Subject: [PATCH 05/14] Add pipefail option to NUMA node detection command for improved error handling --- roles/mongodb/tasks/configure-mongodb-numa.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/roles/mongodb/tasks/configure-mongodb-numa.yml b/roles/mongodb/tasks/configure-mongodb-numa.yml index 5a31ec66..d4fd8ca7 100644 --- a/roles/mongodb/tasks/configure-mongodb-numa.yml +++ b/roles/mongodb/tasks/configure-mongodb-numa.yml @@ -5,6 +5,7 @@ - name: Detect NUMA nodes ansible.builtin.shell: cmd: | + set -o pipefail if [ -d /sys/devices/system/node ]; then ls -d /sys/devices/system/node/node[0-9]* 2>/dev/null | wc -l else From bab7b353eafcb3460b1265918bc6a32e79de38da Mon Sep 17 00:00:00 2001 From: Marcos Dias Date: Thu, 26 Feb 2026 18:32:04 -0300 Subject: [PATCH 06/14] Add max memmory calculation and configure in redis.conf --- inventories | 1 + roles/redis/tasks/configure-redis.yml | 11 +++++++++++ roles/redis/templates/redis.conf.j2 | 2 +- 3 files changed, 13 insertions(+), 1 deletion(-) create mode 120000 inventories diff --git a/inventories b/inventories new file mode 120000 index 00000000..a8b5c9f8 --- /dev/null +++ b/inventories @@ -0,0 +1 @@ +/Users/madias/Documents/dev/inventories \ No newline at end of file diff --git a/roles/redis/tasks/configure-redis.yml b/roles/redis/tasks/configure-redis.yml index f0a872cf..809c0748 100644 --- a/roles/redis/tasks/configure-redis.yml +++ b/roles/redis/tasks/configure-redis.yml @@ -9,6 +9,17 @@ group: root mode: "0644" +- name: Calculate Redis maxmemory (dedicated node) + vars: + redis_maxmemory_ratio: 0.60 # safe for noeviction + AOF + replicas + redis_maxmemory_min_mb: 512 # avoid tiny values on small VMs + ram_mb: "{{ ansible_facts.memtotal_mb | int }}" + raw_mb: "{{ (ram_mb * redis_maxmemory_ratio) | int }}" + clamped_mb: "{{ [redis_maxmemory_min_mb, raw_mb] | max }}" + ansible.builtin.set_fact: + redis_maxmemory_bytes: "{{ clamped_mb * 1024 * 1024 }}" + tags: configure_redis + - name: Use template to generate redis.conf ansible.builtin.template: src: redis.conf.j2 diff --git a/roles/redis/templates/redis.conf.j2 b/roles/redis/templates/redis.conf.j2 index 6d807084..a9bc2ea9 100644 --- a/roles/redis/templates/redis.conf.j2 +++ b/roles/redis/templates/redis.conf.j2 @@ -1035,7 +1035,7 @@ acllog-max-len 128 # limit for maxmemory so that there is some free RAM on the system for replica # output buffers (but this is not needed if the policy is 'noeviction'). # -# maxmemory +maxmemory {{ redis_maxmemory_bytes }} # MAXMEMORY POLICY: how Redis will select what to remove when maxmemory # is reached. You can select one from the following behaviors: From f1e0b65f4adb42c4f6cb533db11b91e237bb3c2c Mon Sep 17 00:00:00 2001 From: Marcos Dias Date: Thu, 26 Feb 2026 18:37:11 -0300 Subject: [PATCH 07/14] Delete inventories wrong directory --- inventories | 1 - 1 file changed, 1 deletion(-) delete mode 120000 inventories diff --git a/inventories b/inventories deleted file mode 120000 index a8b5c9f8..00000000 --- a/inventories +++ /dev/null @@ -1 +0,0 @@ -/Users/madias/Documents/dev/inventories \ No newline at end of file From a9dc675052ee261f1951d0ab49e0dc50730a1d8c Mon Sep 17 00:00:00 2001 From: Marcos Dias Date: Thu, 26 Feb 2026 18:47:55 -0300 Subject: [PATCH 08/14] Fix maxmemory calculation to avoid Ansible treat as tagged float --- roles/redis/tasks/configure-redis.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/roles/redis/tasks/configure-redis.yml b/roles/redis/tasks/configure-redis.yml index 809c0748..0d198547 100644 --- a/roles/redis/tasks/configure-redis.yml +++ b/roles/redis/tasks/configure-redis.yml @@ -11,13 +11,13 @@ - name: Calculate Redis maxmemory (dedicated node) vars: - redis_maxmemory_ratio: 0.60 # safe for noeviction + AOF + replicas + redis_maxmemory_ratio: "0.60" # keep as string, cast later redis_maxmemory_min_mb: 512 # avoid tiny values on small VMs ram_mb: "{{ ansible_facts.memtotal_mb | int }}" - raw_mb: "{{ (ram_mb * redis_maxmemory_ratio) | int }}" - clamped_mb: "{{ [redis_maxmemory_min_mb, raw_mb] | max }}" + raw_mb: "{{ (ram_mb | int * (redis_maxmemory_ratio | float)) | int }}" + clamped_mb: "{{ [redis_maxmemory_min_mb | int, raw_mb | int] | max }}" ansible.builtin.set_fact: - redis_maxmemory_bytes: "{{ clamped_mb * 1024 * 1024 }}" + redis_maxmemory_bytes: "{{ (clamped_mb | int) * 1024 * 1024 }}" tags: configure_redis - name: Use template to generate redis.conf From 777494e802504da99f8dccb1a4ca2f95b406a46a Mon Sep 17 00:00:00 2001 From: Marcos Dias Date: Fri, 27 Feb 2026 03:27:31 -0300 Subject: [PATCH 09/14] Add option to user override the maxmemory instead to use the system calculation --- roles/redis/defaults/main/redis.yml | 12 ++++++++++++ roles/redis/tasks/configure-redis.yml | 11 ++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/roles/redis/defaults/main/redis.yml b/roles/redis/defaults/main/redis.yml index afde9d4d..767811a1 100644 --- a/roles/redis/defaults/main/redis.yml +++ b/roles/redis/defaults/main/redis.yml @@ -61,3 +61,15 @@ redis_user_repluser_password: repluser redis_user_sentineladmin_password: admin redis_user_sentineluser_password: sentineluser redis_user_prometheus_password: prometheus + +# Redis memory sizing defaults (dedicated node) + +# Ratio of total RAM to assign to Redis maxmemory +redis_maxmemory_ratio: "0.60" + +# Minimum maxmemory in MB (safety clamp for small VMs) +redis_maxmemory_min_mb: 512 + +# Optional hard override (bytes) +# If set (>0), this value is used and auto-calculation is skipped +redis_maxmemory_bytes: 0 diff --git a/roles/redis/tasks/configure-redis.yml b/roles/redis/tasks/configure-redis.yml index 0d198547..26fd14fb 100644 --- a/roles/redis/tasks/configure-redis.yml +++ b/roles/redis/tasks/configure-redis.yml @@ -10,14 +10,15 @@ mode: "0644" - name: Calculate Redis maxmemory (dedicated node) + when: redis_maxmemory_bytes | int == 0 vars: - redis_maxmemory_ratio: "0.60" # keep as string, cast later - redis_maxmemory_min_mb: 512 # avoid tiny values on small VMs ram_mb: "{{ ansible_facts.memtotal_mb | int }}" - raw_mb: "{{ (ram_mb | int * (redis_maxmemory_ratio | float)) | int }}" - clamped_mb: "{{ [redis_maxmemory_min_mb | int, raw_mb | int] | max }}" + ratio: "{{ redis_maxmemory_ratio | float }}" + min_mb: "{{ redis_maxmemory_min_mb | int }}" + raw_mb: "{{ (ram_mb * ratio) | int }}" + clamped_mb: "{{ [min_mb, raw_mb] | max }}" ansible.builtin.set_fact: - redis_maxmemory_bytes: "{{ (clamped_mb | int) * 1024 * 1024 }}" + redis_maxmemory_bytes: "{{ clamped_mb * 1024 * 1024 }}" tags: configure_redis - name: Use template to generate redis.conf From a24b031f0b02d31e0d9d1d713abb50ccad922a22 Mon Sep 17 00:00:00 2001 From: Marcos Dias Date: Fri, 27 Feb 2026 03:33:17 -0300 Subject: [PATCH 10/14] Fix _AnsibleTaggedStr error --- roles/redis/tasks/configure-redis.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/roles/redis/tasks/configure-redis.yml b/roles/redis/tasks/configure-redis.yml index 26fd14fb..cbd2d475 100644 --- a/roles/redis/tasks/configure-redis.yml +++ b/roles/redis/tasks/configure-redis.yml @@ -10,15 +10,15 @@ mode: "0644" - name: Calculate Redis maxmemory (dedicated node) - when: redis_maxmemory_bytes | int == 0 + when: (redis_maxmemory_bytes | default(0) | int) == 0 vars: ram_mb: "{{ ansible_facts.memtotal_mb | int }}" - ratio: "{{ redis_maxmemory_ratio | float }}" - min_mb: "{{ redis_maxmemory_min_mb | int }}" - raw_mb: "{{ (ram_mb * ratio) | int }}" - clamped_mb: "{{ [min_mb, raw_mb] | max }}" + ratio: "{{ redis_maxmemory_ratio | default('0.60') | float }}" + min_mb: "{{ redis_maxmemory_min_mb | default(512) | int }}" + raw_mb: "{{ ((ram_mb | int) * (ratio | float)) | int }}" + clamped_mb: "{{ [min_mb | int, raw_mb | int] | max }}" ansible.builtin.set_fact: - redis_maxmemory_bytes: "{{ clamped_mb * 1024 * 1024 }}" + redis_maxmemory_bytes: "{{ ((clamped_mb | int) * 1024 * 1024) | int }}" tags: configure_redis - name: Use template to generate redis.conf From 20c19b2e189f3ab24a20bb60d2fd027c998ffd6d Mon Sep 17 00:00:00 2001 From: Marcos Dias Date: Fri, 27 Feb 2026 15:46:32 -0300 Subject: [PATCH 11/14] Changed some variables structure to be more consistent with the rest of the codebase. Also added a new variable for maxmemory in bytes to be used in the redis.conf template. --- roles/redis/defaults/main/redis.yml | 13 ++++++------- roles/redis/tasks/configure-redis.yml | 23 ++++++++++++++--------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/roles/redis/defaults/main/redis.yml b/roles/redis/defaults/main/redis.yml index 767811a1..7151e9f3 100644 --- a/roles/redis/defaults/main/redis.yml +++ b/roles/redis/defaults/main/redis.yml @@ -62,14 +62,13 @@ redis_user_sentineladmin_password: admin redis_user_sentineluser_password: sentineluser redis_user_prometheus_password: prometheus -# Redis memory sizing defaults (dedicated node) +# Redis memory sizing defaults (dedicated Redis node) -# Ratio of total RAM to assign to Redis maxmemory -redis_maxmemory_ratio: "0.60" +# If set to "auto", maxmemory is calculated from system RAM. +# If set to a number (bytes), that value is used verbatim. +redis_maxmemory_bytes: auto -# Minimum maxmemory in MB (safety clamp for small VMs) +# Used only when redis_maxmemory_bytes == "auto" +redis_maxmemory_ratio: 0.60 redis_maxmemory_min_mb: 512 -# Optional hard override (bytes) -# If set (>0), this value is used and auto-calculation is skipped -redis_maxmemory_bytes: 0 diff --git a/roles/redis/tasks/configure-redis.yml b/roles/redis/tasks/configure-redis.yml index cbd2d475..1fe138df 100644 --- a/roles/redis/tasks/configure-redis.yml +++ b/roles/redis/tasks/configure-redis.yml @@ -9,16 +9,21 @@ group: root mode: "0644" -- name: Calculate Redis maxmemory (dedicated node) - when: (redis_maxmemory_bytes | default(0) | int) == 0 - vars: - ram_mb: "{{ ansible_facts.memtotal_mb | int }}" - ratio: "{{ redis_maxmemory_ratio | default('0.60') | float }}" - min_mb: "{{ redis_maxmemory_min_mb | default(512) | int }}" - raw_mb: "{{ ((ram_mb | int) * (ratio | float)) | int }}" - clamped_mb: "{{ [min_mb | int, raw_mb | int] | max }}" +- name: Calculate Redis maxmemory automatically ansible.builtin.set_fact: - redis_maxmemory_bytes: "{{ ((clamped_mb | int) * 1024 * 1024) | int }}" + redis_maxmemory_bytes: >- + {{ + ( + [ + redis_maxmemory_min_mb | int, + ( + (ansible_facts.memtotal_mb | int) + * (redis_maxmemory_ratio | float) + ) | int + ] | max + ) * 1024 * 1024 + }} + when: redis_maxmemory_bytes == "auto" tags: configure_redis - name: Use template to generate redis.conf From 71371f0eca30988c74d52444beb0874f1b9e918f Mon Sep 17 00:00:00 2001 From: Marcos Dias Date: Fri, 27 Feb 2026 15:50:46 -0300 Subject: [PATCH 12/14] Fixed blank extra blank line. --- roles/redis/defaults/main/redis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/roles/redis/defaults/main/redis.yml b/roles/redis/defaults/main/redis.yml index 7151e9f3..3b31f0cb 100644 --- a/roles/redis/defaults/main/redis.yml +++ b/roles/redis/defaults/main/redis.yml @@ -71,4 +71,3 @@ redis_maxmemory_bytes: auto # Used only when redis_maxmemory_bytes == "auto" redis_maxmemory_ratio: 0.60 redis_maxmemory_min_mb: 512 - From bc470e81a3f6109887e2fd7bb69fa83fd32ff8dd Mon Sep 17 00:00:00 2001 From: Marcos Dias Date: Thu, 5 Mar 2026 02:07:42 -0300 Subject: [PATCH 13/14] Update redis guide doc and also documented the formula in the configure-redis.yml task. --- docs/redis_guide.md | 3 +++ roles/redis/tasks/configure-redis.yml | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/docs/redis_guide.md b/docs/redis_guide.md index b14b739d..ec921e63 100644 --- a/docs/redis_guide.md +++ b/docs/redis_guide.md @@ -107,6 +107,9 @@ The following tables lists the default variables located in `roles/redis/default | `redis_bind_addr_source` | String | The bind address source. Will default to the Ansible `inventory_hostname` unless explicitly set to `default_ipv4_address`. | `inventory_hostname` | | `redis_bind_addrs` | String | A space-separated list of hostnames/IP addresses on which Redis listeners will be created. If `redis_bind_ipv6` is set to `true`, `::1` will be added to the addresses. The `redis_bind_addr_source` will also be added to the addresses. | `127.0.0.1` | | `redis_tls_enabled` | Boolean | Flag to enable TLS connections. | `false` | +| `redis_maxmemory_bytes` | Integer | Maximum memory Redis can use (maxmemory). When set to auto, the installer calculates the value from the system RAM using: `maxmemory = max(redis_maxmemory_min_mb, system_ram × redis_maxmemory_ratio)`. If a numeric value is provided, that value (in bytes) is used directly and the automatic calculation is skipped. | `auto` | +| `redis_maxmemory_ratio` | Float | Define how much memory the system will use. Only work if `redis_maxmemory_bytes` is configured as auto. Default value 0.6 means 60%. | `0.60` | +| `redis_maxmemory_bytes` | Integer | This parameter defines the minimum amount of memory Redis is allowed to use, even if the automatic calculation would result in a smaller value. It acts as a safety floor for the maxmemory calculation. | `512` | ### Auth Variables diff --git a/roles/redis/tasks/configure-redis.yml b/roles/redis/tasks/configure-redis.yml index 1fe138df..d65de0ce 100644 --- a/roles/redis/tasks/configure-redis.yml +++ b/roles/redis/tasks/configure-redis.yml @@ -9,6 +9,32 @@ group: root mode: "0644" +# Calculate the Redis maxmemory value automatically when the parameter +# `redis_maxmemory_bytes` is set to "auto". +# +# The calculation uses the following formula: +# +# maxmemory = max(redis_maxmemory_min_mb, system_ram × redis_maxmemory_ratio) +# +# Where: +# system_ram -> Total system memory detected by Ansible (in MB) +# redis_maxmemory_ratio -> Percentage of system memory Redis is allowed to use +# redis_maxmemory_min_mb -> Minimum memory Redis should be assigned +# +# The `max()` function ensures Redis always receives at least the configured +# minimum memory, even on small systems. +# +# The final value is converted from MB to bytes because Redis expects the +# `maxmemory` configuration parameter to be expressed in bytes. +# +# Example (10 GB system): +# system_ram = 10240 MB +# ratio = 0.60 +# result = 6144 MB +# redis_maxmemory_bytes = 6442450944 +# +# If `redis_maxmemory_bytes` is set to a numeric value instead of "auto", +# this calculation is skipped and the provided value is used directly. - name: Calculate Redis maxmemory automatically ansible.builtin.set_fact: redis_maxmemory_bytes: >- From 4a8a7812a25eaa0bc7225568cc73bae489f3e02d Mon Sep 17 00:00:00 2001 From: Marcos Dias Date: Thu, 5 Mar 2026 13:53:00 -0300 Subject: [PATCH 14/14] Fix mistyped variable name. --- docs/redis_guide.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/redis_guide.md b/docs/redis_guide.md index ec921e63..96fde629 100644 --- a/docs/redis_guide.md +++ b/docs/redis_guide.md @@ -109,7 +109,7 @@ The following tables lists the default variables located in `roles/redis/default | `redis_tls_enabled` | Boolean | Flag to enable TLS connections. | `false` | | `redis_maxmemory_bytes` | Integer | Maximum memory Redis can use (maxmemory). When set to auto, the installer calculates the value from the system RAM using: `maxmemory = max(redis_maxmemory_min_mb, system_ram × redis_maxmemory_ratio)`. If a numeric value is provided, that value (in bytes) is used directly and the automatic calculation is skipped. | `auto` | | `redis_maxmemory_ratio` | Float | Define how much memory the system will use. Only work if `redis_maxmemory_bytes` is configured as auto. Default value 0.6 means 60%. | `0.60` | -| `redis_maxmemory_bytes` | Integer | This parameter defines the minimum amount of memory Redis is allowed to use, even if the automatic calculation would result in a smaller value. It acts as a safety floor for the maxmemory calculation. | `512` | +| `redis_maxmemory_min_mb` | Integer | This parameter defines the minimum amount of memory Redis is allowed to use, even if the automatic calculation would result in a smaller value. It acts as a safety floor for the maxmemory calculation. | `512` | ### Auth Variables