redis:使用 redis-cli 非交互方式创建集群

pka*_*mol 9 redis redis-cli

当尝试创建集群时,redis-cli如下所示

redis-cli --cluster create
Run Code Online (Sandbox Code Playgroud)

出现提示要求确认配置?

有没有办法编写此脚本(最好在 中ansible)并以非交互方式运行它?

我知道这个主题,但它涉及数据操作,这不是这个问题的范围。

Leo*_*ira 14

--cluster-yes是正确的选择!


Jon*_*ijs 10

编辑:莱昂纳多奥利维拉的回答 正确地指出该选项--cluster-yes将避免提示。例如:

redis-cli --cluster create host1:6379 host2:6379 host3:6379 --cluster-yes
Run Code Online (Sandbox Code Playgroud)

截至当前的 Redis 版本(5.0.5),似乎没有一个标志--cluster可以使交互式问题静音或自动回答:

$ redis-cli --cluster help
Cluster Manager Commands:
  create         host1:port1 ... hostN:portN
                 --cluster-replicas <arg>
  check          host:port
                 --cluster-search-multiple-owners
  info           host:port
  fix            host:port
                 --cluster-search-multiple-owners
  reshard        host:port
                 --cluster-from <arg>
                 --cluster-to <arg>
                 --cluster-slots <arg>
                 --cluster-yes
                 --cluster-timeout <arg>
                 --cluster-pipeline <arg>
                 --cluster-replace
  rebalance      host:port
                 --cluster-weight <node1=w1...nodeN=wN>
                 --cluster-use-empty-masters
                 --cluster-timeout <arg>
                 --cluster-simulate
                 --cluster-pipeline <arg>
                 --cluster-threshold <arg>
                 --cluster-replace
  add-node       new_host:new_port existing_host:existing_port
                 --cluster-slave
                 --cluster-master-id <arg>
  del-node       host:port node_id
  call           host:port command arg arg .. arg
  set-timeout    host:port milliseconds
  import         host:port
                 --cluster-from <arg>
                 --cluster-copy
                 --cluster-replace
  help
Run Code Online (Sandbox Code Playgroud)

通过使用echo您可以执行命令并自动回答提示:

echo "yes" | redis-cli --cluster create host1:6379 host2:6379 host3:6379
Run Code Online (Sandbox Code Playgroud)

默认的Ansible Redis 模块仅支持少数命令,因此--cluster您必须使用命令/shell 任务创建自己的逻辑:

- name: Create cluster
  shell: echo "yes" | redis-cli --cluster create host1:6379 host2:6379 host3:6379
  run_once: true
  when: not cluster_setup_done
Run Code Online (Sandbox Code Playgroud)