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

时间:2019-03-17 10:54:32

标签: redis redis-cli

按以下方式尝试使用redis-cli创建集群

redis-cli --cluster create

出现提示询问配置确认吗?

是否可以编写脚本(最好在ansible中编写脚本并以非交互方式运行它?

我知道this topic,但是它解决的是数据操纵,这不是此问题的范围。

3 个答案:

答案 0 :(得分:1)

从当前的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

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

echo "yes" | redis-cli --cluster create host1:6379 host2:6379 host3:6379

默认的Ansible Redis module仅支持一些命令,而不支持--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

答案 1 :(得分:0)

好吧,我不知道那可笑的部分。但是Redis official site确实提供了一种使用脚本以交互模式创建集群的方法

使用create-cluster脚本创建Redis集群(有关更多详细信息,请参阅文档)


declare
l_line varchar2(2000);
ln_start_string number;
ln_last_string number;
ln_string_length number;
l_word varchar2(4000);
l_table_flag varchar(2):='N';
cursor l_pkg_body_cur
is 
select TEXT from all_source where upper(name) like upper('pac_example') and type = 'PACKAGE BODY'; 
-- to get the source compiled in package boby, mentioned the package to be searched here
begin
for rec in l_pkg_body_cur
loop
-- line by line processing
select TRIM(rec.text) into l_line from dual;
ln_string_length := length(l_line);
loop
-- word by word processing
l_table_flag :='N';
select instr(l_line,' ') into ln_last_string from dual;
select substr(l_line,0,ln_last_string) into l_word from dual;
begin
select 'Y' into l_table_flag from all_tables where upper(table_name) like upper(trim(l_word)) and rownum=1; -- to validate it is table or not
exception
when others then
l_table_flag := 'N';
end;
IF l_table_flag = 'Y'
then
dbms_output.put_line(trim(l_word) ); -- table name
end if;
select length (l_word) into ln_start_string from dual;
select trim(substr(replace(l_line,';',null),ln_start_string)) into l_line from dual;
exit when l_line is NULL;
end loop;
end loop;
end;



--output:
Statement processed.
table1
table2

如果可以以任何方式对您有所帮助,您可以尝试实施! 干杯:)

答案 2 :(得分:0)

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