第一步,java调用shell
paraname和paravalue是传给playbook的变量。
try { String cmd5 = "sudo ansible-playbook /setRedisParam.yaml" + " --extra-vars " + "{"paraname":" + paramName + ","paravalue":" + paramValue + "}"; Process ps3 = Runtime.getRuntime().exec(cmd5); ps3.waitFor(); System.out.println(cmd5); System.out.println(ps3.exitValue()); } catch (Exception e) { e.printStackTrace(); return 0; }
第二步,编写ansible playbook
--- - hosts: webservers remote_user: root tasks: - name: redis config set1 command: /home/admin/redis-4.0.7/src/redis-cli config set '{{paraname}}' '{{paravalue}}' - name: redis config rewrite command: /home/admin/redis-4.0.7/src/redis-cli config rewrite
也可以使用ansible的redis模块进行修改参数
--- - hosts: webservers remote_user: root tasks: - redis: command: config name: "{{paraName}}" value: "{{paraValue}}" - name: redis config rewrite command: /home/admin/redis-4.0.7/src/redis-cli config rewrite
注意,在修改参数的任务完成后要执行redis-cli的CONFIG REWRITE命令,否则参数修改并不会体现在配置文件中。
第三步,启动redis服务,需要指定redis配置文件,否则将不能修改参数
./redis-server /etc/redis/redis.conf
第四步,执行代码java代码,传入参数,redis数据库的终端应该显示如下
19539:M 16 Mar 19:15:04.478 # CONFIG REWRITE executed with success.