1,主要功能
1)salt 文件模块
salt -N api file.copy /etc/nginx/nginx.conf /home/zhanqi/nginx.conf 文件复制
salt -N api file.get_sum /home/zhanqi/nginx.conf md5 文件md5校验
salt -N api cmd.run 'ls -lh /home/zhanqi/nginx.conf'
salt -N api file.chown /home/zhanqi/nginx.conf zhanqi zhanqi 修改文件所属用户以及组
salt -N api file.set_mode /home/zhanqi/nginx.conf 755 修改文件权限
salt -N api file.mkdir /home/zhanqi/testdir 创建目录
salt -N api file.remove /home/zhanqi/testdir 移除目录
salt -N api file.append /home/zhanqi/nginx.conf 'add_header "Access-Control-Allow-Origin" "*";' 追加内容到文件
2)salt文件file模块,可以使用jinja模板。
Salt lets you manage files using templates and variables.
file.managed 是salt的state function。管理minion'上的local file,通过指定master上的source文件
deploy the http.conf file:
file.managed: ##distribute a global configuration
- name: /etc/http/conf/http.conf ##minion上的文件
- source: salt://apache/http.conf ##master上的/srv/salt/apache/http.conf文件
copy some files to the web server:
file.recurse:##copies an entire directory
- name: /var/www
- source: salt://apache/www
3)salt file模块中使用模板和变量。
如果使用模板,用户定义的模板变量,必须通过defaults或者context参数,才能传递给source中定义的文件。就是说source文件必须通过defaults或者context参数传递模板变量,才可以在文件中使用
If using a template, any user-defined template variables in the file defined in source must be passed in using the defaults and/or context arguments.
conf_test.sls文件
{% set minion = pillar.get('salt_minion_id') %}
/etc/http/conf/http.conf:
file.managed:
- source: salt://test/api_test.conf
- user: root
- group: root
- mode: 644
- template: jinja
- defaults:
deployType: 'all'
api_test.conf文件
{%- if deployType == 'all' %}
server 210.73.212.116:80 weight=3 max_fails=2 fail_timeout=60s;
server 210.73.212.118:80 weight=3 max_fails=2 fail_timeout=60s;
{% endif %}