一、知识点:
cp.get_file功能可以用于minion从master下载一个文件,该语法像这样:
# salt '*' cp.get_file salt://vimrc /etc/vimrc
该指令将会通知所有Salt minions下载vimrc文件并且拷贝到/etc/vimrc
模板渲染功能可以同时在源和目标文件启用,命名像这样:
# salt '*' cp.get_file "salt://`grains`.`os`/vimrc" /etc/vimrc template=jinja
该范例将会通知所有Salt minions下载vimrc从同名目录名下载,作为他们的OS grain并且拷贝到/etc/vimrc
# salt '*' cp.get_file salt://vimrc /etc/vimrc gzip=5
# salt '*' cp.get_file salt://vimrc /etc/vim/vimrc makedirs=True
使用makedirs=True参数,如果/etc/vim不存在,则被创建。详细内容请参考:
二、实例:
默认salt的仓库目录在/srv/salt,如果没有,请创建该目录
以同步nginx配置文件test.conf为例,创建nginx配置文件目录:
[root@107036110 ~]# cd /srv/salt[root@107036110 salt]# mkdir nginx_conf[root@107036110 nginx_conf]# vim test.conf#内容就不贴了,没意义,看下MD5值吧:[root@107036110 nginx_conf]# md5sum test.conf a1df167be67a7a22ad8d9faf0472f82b test.conf
在统一之前,我们先把之前的都做一个备份,养成一个好习惯:
[root@107036110 nginx_conf]# salt -N web1 cmd.run 'cp /usr/local/nginx/conf/vhosts/test.conf /usr/local/nginx/conf/vhosts/test.conf.bak'
同步前,查看下各个服务器上的配置的md5值:
[root@107036110 nginx_conf]# salt -N web1 cmd.run 'md5sum /usr/local/nginx/conf/vhosts/test.conf'web103: 6550b62b0f4a86c8f907e196fc6113fc /usr/local/nginx/conf/vhosts/test.confweb104: 85b265141da4013e9ce722b72a0d5471 /usr/local/nginx/conf/vhosts/test.confweb202: f3840e999a0c9f1d8486feaf5984a7ea /usr/local/nginx/conf/vhosts/test.confweb201: f3840e999a0c9f1d8486feaf5984a7ea /usr/local/nginx/conf/vhosts/test.conf
同步配置文件:
[root@107036110 nginx_conf]# salt -N web1 cp.get_file salt://nginx_conf/test.conf /usr/local/nginx/conf/vhosts/test.confweb201: /usr/local/nginx/conf/vhosts/test.confweb103: /usr/local/nginx/conf/vhosts/test.confweb202: /usr/local/nginx/conf/vhosts/test.confweb104: /usr/local/nginx/conf/vhosts/test.conf
查看下同步后的文件MD5值:
[root@107036110 nginx_conf]# salt -N web1 cmd.run 'md5sum /usr/local/nginx/conf/vhosts/test.conf'web104: a1df167be67a7a22ad8d9faf0472f82b /usr/local/nginx/conf/vhosts/test.confweb202: a1df167be67a7a22ad8d9faf0472f82b /usr/local/nginx/conf/vhosts/test.confweb201: a1df167be67a7a22ad8d9faf0472f82b /usr/local/nginx/conf/vhosts/test.confweb103: a1df167be67a7a22ad8d9faf0472f82b /usr/local/nginx/conf/vhosts/test.conf
重启下nginx服务:
[root@107036110 nginx_conf]# salt -N web1 cmd.run 'kill -HUP `cat /usr/local/nginx/logs/nginx.pid`'web104: web103: web202: web201: