salt stack最佳实践

server上:
yum install -y epel-release
yum install -y salt-master  salt-minion

client上:
yum install -y epel-release
yum install -y salt-minion

启动
server上:
/etc/init.d/salt-master start
/etc/init.d/salt-minion start
server
vim /etc/salt/master
 
file_roots
file_roots:
  base:
    - /srv/salt
pillar
pillar_roots:
  base:
    - /srv/pillar
#
 
client
vim /etc/salt/minion
# resolved, then the minion will fail to start.
master: 10.0.0.100
id: client
 
/etc/init.d/salt-master start
/etc/init.d/salt-minion start
目标  模块 返回
salt '*' test.ping 检测主机存活
salt '*'  cmd.run 'df -h' 不支持交互式命令 vim ssh
mkdir /srv/{salt,pillar}
拷贝文件
[root@bogon ~]# cd /srv/salt
[root@bogon salt]# vim host_file.sls
/etc/hosts:            ## 目标
  file.managed:
    - source: salt://files/hosts  ##源文件
    - user: rootvim
    - group: root
    - mode: 644nginx_nginx
~                                                                                                               
~                   
 
[root@bogon salt]# mkdir files
[root@bogon salt]# cd files/
[root@bogon files]# cp /etc/hosts .
[root@bogon files]# ll
总用量 4
-rw-r--r-- 1 root root 158 2月  25 18:09 hosts
[root@bogon files]# pwd
/srv/salt/files
[root@bogon files]#
salt '*' state.sls host_file
salt-cp '*' /etc/hosts /mnt
 
安装nginx
 
[root@localhost salt]# cat nginx_install.sl
nginx-install:
  pkg.installed:
    - names:
      - nginx
 
/etc/hosts:
  file.managed: ll
    - source: salt://files/hosts
    - user: root
    - group: root
    - mode: 644
    - require:
      - pkg: nginx-install  ##依赖
  service.running:
    - names:
      - nginx
计划任务
 
[root@bogon salt]# cat crontab_time.sls
/usr/sbin/ntpdate times.aliyun.com >>/dev/null:
  cron.present:
    - user: root
    - minute: '*/5'
 
[root@localhost salt]# salt '*' state.sls crontab_time
client:
----------
          ID: /usr/sbin/ntpdate times.aliyun.com >>/dev/null
    Function: cron.present
      Result: True
     Comment: Cron /usr/sbin/ntpdate times.aliyun.com >>/dev/null added to root's crontab
     Started: 19:57:44.156525
    Duration: 78.342 ms
     Changes:   
              ----------
              root:
                  /usr/sbin/ntpdate times.aliyun.com >>/dev/null
 
Summary
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
删除任务
[root@localhost salt]# cat del_cron.sls
/usr/sbin/ntpdate times.aliyun.com >>/dev/null:
   cron.absent:
    - name: /usr/sbin/ntpdate times.aliyun.com >>/dev/null
 
salt '*' state.sls 'del_cron'
报错
 
[root@bogon salt]# salt '*' state.sls nginx
client:
    Data failed to compile:
----------
    The function "state.sls" is running as PID 32727 and was started at 2017, Feb 26 21:34:45.353979 with jid 20170226213445353979
ERROR: Minions returned with non-zero exit code
[root@bogon salt]# salt '*' saltutil.kill_job jid
client:
原文地址:https://www.cnblogs.com/jinbiaof/p/6845175.html