saltstack执行模块

(1)master中编写自定义模块

[root@server1 ~]# mkdir /srv/salt/_modules -p
[root@server1 ~]# cd /srv/salt/_modules/
[root@server1 _modules]# vim my_disk.py
#!/use/bin/env python
def df():
        return __salt__['cmd.run']('df -h')

刷新salt模块库

[root@server1 _modules]# salt '*' saltutil.sync_modules
server2:
    - modules.my_disk
server1:
    - modules.my_disk
server4:
    - modules.my_disk
server3:
    - modules.my_disk

推送自己的模块

[root@server1 _modules]# salt '*' my_disk.df
server3:
    Filesystem                    Size  Used Avail Use% Mounted on
    /dev/mapper/VolGroup-lv_root   19G  1.1G   17G   6% /
    tmpfs                         246M   16K  246M   1% /dev/shm
    /dev/vda1                     485M   33M  427M   8% /boot
server4:
    Filesystem                    Size  Used Avail Use% Mounted on
    /dev/mapper/VolGroup-lv_root   19G  1.1G   17G   6% /
    tmpfs                         372M   16K  372M   1% /dev/shm
    /dev/vda1                     485M   33M  427M   8% /boot
server2:
    Filesystem                    Size  Used Avail Use% Mounted on
    /dev/mapper/VolGroup-lv_root   19G  990M   17G   6% /
    tmpfs                         246M   96K  246M   1% /dev/shm
    /dev/vda1                     485M   33M  427M   8% /boot
server1:
    Filesystem                    Size  Used Avail Use% Mounted on
    /dev/mapper/VolGroup-lv_root   19G  1.1G   17G   7% /
    tmpfs                         499M   76K  499M   1% /dev/shm
    /dev/vda1                     485M   33M  427M   8% /boot

函数存放位置

[root@server4 ~]# cd /var/cache/salt
[root@server4 salt]# tree
.
`-- minion
    |-- accumulator
    |-- extmods
    |   |-- grains
    |   |   |-- my_grains.py
    |   |   `-- my_grains.pyc
    |   `-- modules            #外部函数位置
    |       |-- my_disk.py
    |       `-- my_disk.pyc
    |-- files
    |   `-- base
    |       |-- _grains
    |       |   `-- my_grains.py
    |       |-- haproxy
    |       |   |-- files
    |       |   |   `-- haproxy.cfg
    |       |   `-- install.sls
    |       |-- keepalived
    |       |   |-- files
    |       |   |   |-- keepalived
    |       |   |   |-- keepalived-2.0.6.tar.gz
    |       |   |   `-- keepalived.conf
    |       |   |-- install.sls
    |       |   `-- service.sls
    |       |-- _modules
    |       |   `-- my_disk.py
    |       `-- top.sls
    |-- highstate.cache.p
    |-- module_refresh
    |-- pkg_refresh
    |-- proc
    `-- sls.p

14 directories, 18 files

(2)salt中的常用模块ssh

<1>server4关闭salt-minion

<2>在server1上安装salt-ssh

 [root@server1 ~]# yum install -y salt-ssh

<3>在server上编辑ssh的配置文件

  [root@server1 ~]# vim /etc/salt/roster
    server3:
      host: 172.25.1.4       # 想要连接的主机
      user: root             # 该主机的超级用户
      passwd: redhat         # 该超级用户的密码

<4> 在server1中连通server4

salt-ssh 'server4' test.ping -i

<5>server1中执行命令,不启动minion

[root@server1 salt]# salt-ssh 'server4' cmd.run 'df -h'
server4:
    Filesystem                    Size  Used Avail Use% Mounted on
    /dev/mapper/VolGroup-lv_root   14G  1.1G   12G   9% /
    tmpfs                         499M   16K  499M   1% /dev/shm
    /dev/vda1                     485M   33M  427M   8% /boot

(3) salt-syndic功能

<1> 选server4作为top master,关闭server4的minion,server1中去掉server4的minion

 [root@server4 ~]# /etc/init.d/salt-minion stop
 [root@server4 ~]# chkconfig salt-minion off

<2> server4中安装salt-master

   [root@server4 ~]# yum install salt-master

<3>修改sevrer4中master配置文件

vim /etc/salt/master
857 order_masters: True    # 打开允许syndic功能

<4>server1中安装salt-syndic

[root@server1 ~]# yum install salt-syndic -y
[root@server1 ~]# vim /etc/salt/master
861 syndic_master: 172.25.1.4    # 设置top master为server4
[root@server1 ~]# /etc/init.d/salt-master restart
Stopping salt-master daemon:                               [确定]
Starting salt-master daemon:                               [确定]
[root@server1 ~]# /etc/init.d/salt-syndic start   #需要启动syndic否则无法交换公钥
Starting salt-syndic daemon:                               [确定]


<5> server4中发现server1

   salt-key -A
   [root@server4 ~]# salt-key -L
Accepted Keys:
Denied Keys:
Unaccepted Keys:
server1
Rejected Keys:
[root@server4 ~]# salt-key -A 
The following keys are going to be accepted:
Unaccepted Keys:
server1
Proceed? [n/Y] Y
Key for minion server1 accepted.

<6>sevrer4上执行推送,推送到了server1的各个minion端

[root@server4 ~]# salt '*' test.ping    #在top master可以看到master管理的节点,减轻了master端的压力
server1:
    True
server2:
    True
 server3:
    True

你可能感兴趣的:(saltstack执行模块)