Redis Cluster:如何获取特定主节点的槽数?

问题描述 投票:0回答:1

如何仅捕获特定主节点在 Ansible 中拥有的槽总数?

我知道如果你跑步:

redis-cli --cluster check

例如,您将得到以下输出:

192.168.56.115:6379 (7d0fb9ab...) -> 0 keys | 4242 slots | 0 slaves.
192.168.56.116:6379 (9fe57cd1...) -> 0 keys | 3950 slots | 0 slaves.
192.168.56.117:6379 (d37e89a6...) -> 0 keys | 4096 slots | 0 slaves.
192.168.56.118:6379 (e0eb74db...) -> 0 keys | 4096 slots | 0 slaves.
[OK] 0 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.56.115:6379)
M: 7d0fb9abd91d6397f11e6f606c8ecf919ba2d1fe 192.168.56.115:6379
   slots:[0-4241] (4242 slots) master
M: 9fe57cd1faaa6e77d0e89c504b9f8b69f72c52aa 192.168.56.116:6379
   slots:[4242-8191] (3950 slots) master
M: d37e89a685ec31f3ce85544203132f862d8330a7 192.168.56.117:6379
   slots:[8192-12287] (4096 slots) master
M: e0eb74db5c6ccbc469538d05bf98302d056c7e05 192.168.56.118:6379
   slots:[12288-16383] (4096 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

我想编写一个 Ansible 剧本,将目标节点的 ip 指定为

target_node_ip
,它给出以下输出:例如对于
target_node_ip = 192.168.56.116

3950 

我尝试了很多正则表达式模式,但它们没有帮助我。

例如,我写了这些任务:

- name: Catch --cluster check
  command: >
    redis-cli --user default --cluster check 192.168.56.115:6379
  register: cluster_check_output

- name: Catch the number of slots the target currently has
  set_fact:
    node_slots: "{{cluster_check_output.stdout | regex_replace('.*{{ target_node_ip }}:6379 \\(.{8}\\.{3}\\) -> \\d+ keys | (\\d+) slots | \\d+ slaves.*', '\\1')}}"

- name: Debug node_slots
  debug:
    var: node_slots

输出是这样的:

TASK [Debug node_slots] ***********************************************************************************************************************
task path: /home/behnia/irancell-projects/redis-test/redis-cluster/redis-cluster-management/playbooks/remove_node.yml:64
ok: [server3] => {
    "node_slots": "192.168.56.115:6379 (7d0fb9ab...) -> 0 keys |4242|\n192.168.56.116:6379 (9fe57cd1...) -> 0 keys |3950|\n192.168.56.117:6379 (d37e89a6...) -> 0 keys |4096|\n192.168.56.118:6379 (e0eb74db...) -> 0 keys |4096|\n\u001b[32;1m[OK] 0 keys in 4 masters.\n\u001b[0m0.00 keys per slot on average.\n\u001b[29;1m>>> Performing Cluster Check (using node 192.168.56.115:6379)\n\u001b[0mM: 7d0fb9abd91d6397f11e6f606c8ecf919ba2d1fe 192.168.56.115:6379\n   slots:[0-4241] (4242 slots) master\nM: 9fe57cd1faaa6e77d0e89c504b9f8b69f72c52aa 192.168.56.116:6379\n   slots:[4242-8191] (3950 slots) master\nM: d37e89a685ec31f3ce85544203132f862d8330a7 192.168.56.117:6379\n   slots:[8192-12287] (4096 slots) master\nM: e0eb74db5c6ccbc469538d05bf98302d056c7e05 192.168.56.118:6379\n   slots:[12288-16383] (4096 slots) master\n\u001b[32;1m[OK] All nodes agree about slots configuration.\n\u001b[0m\u001b[29;1m>>> Check for open slots...\n\u001b[0m\u001b[29;1m>>> Check slots coverage...\n\u001b[0m\u001b[32;1m[OK] All16384covered.\n\u001b[0m"
}

如果您看到,它不是用 3950 替换整个输出,而是用 | <#> slots |

 替换每个 
|#|
,其中 # 是槽数(在本上下文中 
4242, 3950, 4096, 4096

我想要的只是

3950(为了target_node_ip = 192.168.56.116

regex redis ansible regex-group redis-cluster
1个回答
0
投票
正如评论中已经提到的,一个最小的示例剧本......

shell

中的预处理

在远程节点上并且仅为了赶上最终结果集。

--- - hosts: localhost become: false gather_facts: false vars: target_node_ip: 192.168.56.116 tasks: - name: Catch --cluster check shell: | cat redis.stdout | # redis-cli --cluster check grep {{ target_node_ip }} | # for specific node only head -n 1 | # first line found cut -d '|' -f 2 | # result between pipes grep -o '[0-9]' | # and numbers only tr -d '\n' # register: cluster_check_output - name: Show the number of slots the target currently has debug: var: cluster_check_output.stdout
将产生

的输出

TASK [Show the number of slots the target currently has] ****** ok: [localhost] => cluster_check_output.stdout: '3950'
    
© www.soinside.com 2019 - 2024. All rights reserved.