root@ubuntu:/etc/salt# vim master
找到下面的內(nèi)容并解除注釋:
file_roots: base:
- /srv/salt
然后殺死salt-master,并重啟:salt-master -d.
root@ubuntu:/etc/salt# mkdir /srv/salt
root@ubuntu:/etc/salt#cd /srv/salt
root@ubuntu:/srv/salt# vim top.sls
寫入下面的代碼:
base: 'os:ubuntu': - match: grain - webserver
創(chuàng)建sls文件
也是在這個目錄下創(chuàng)建webserver.sls,包含下面內(nèi)容:
apache2: # ID declaration
pkg: # state declaration
- installed # function declaration
注意: The package name for the Apache httpd web server may differ depending on OS or distro — for example, on Fedora it ishttpd
but on Debian/Ubuntu it isapache2
.
第一行的apache這是任意定義的,只是定義要被安裝的包的名字
第二行定義的是我們要用的stalt states,我們用了 pkg state來確保包被安裝
第三行稱為函數(shù)聲明,定義了pkg狀態(tài)模塊中調(diào)用的函數(shù)
在所有的minions上安裝apache,執(zhí)行以下命令:
salt '*' state.apply
由于我的salt里面沒有state.apply,我使用了state.highstate代替经伙,我將上面的webserver.sls中的apache2改成了nginx:
nginx:
pkg:
- installed
**Install the package**
root@ubuntu:/srv/salt# salt ubuntu1 state.highstate
----------
State: - pkg
Name: nginx
Function: installed
Result: True
Comment: The following packages were installed/updated: nginx.
Changes: libgd3: { new : 2.1.0-3ubuntu0.6
old :
}
nginx-core: { new : 1.4.6-1ubuntu3.7
old :
}
libxpm4: { new : 1:3.5.10-1ubuntu0.1
old :
}
nginx-common: { new : 1.4.6-1ubuntu3.7
old :
}
libvpx1: { new : 1.3.0-2
old :
}
nginx: { new : 1.4.6-1ubuntu3.7
old :
}
libtiff5: { new : 4.0.3-7ubuntu0.7
old :
}
libjpeg-turbo8: { new : 1.3.0-0ubuntu2
old :
}
libjpeg8: { new : 8c-2ubuntu8
old :
}
libjbig0: { new : 2.0-2ubuntu4.1
old :
}
Summary
------------
Succeeded: 1
Failed: 0
------------
Total: 1
要是出錯了题篷,minions可以用下面的命令排查錯誤:
root@ubuntu1:~# salt-minion -l debug
[DEBUG ] Reading configuration from /etc/salt/minion
[DEBUG ] loading log_handlers in ['/var/cache/salt/minion/extmods/log_handlers', '/usr/lib/python2.7/dist-packages/salt/log/handlers']
[DEBUG ] Skipping /var/cache/salt/minion/extmods/log_handlers, it is not a directory
[DEBUG ] Configuration file path: /etc/salt/minion
[INFO ] Setting up the Salt Minion "ubuntu1"
[DEBUG ] Created pidfile: /var/run/salt-minion.pid
[DEBUG ] Chowned pidfile: /var/run/salt-minion.pid to user: root
.....
總結(jié)下:
首先vim /etc/salt/master,找到file_roots:行 并打開與之相關(guān)的注釋廓俭,如:file_roots:
base: #這個是全局的
- /srv/salt
#還可以繼續(xù)往下面加会宪,如下:
dev:
- /srv/salt/dev
然后重啟master.
第二赏参,cd /srv/salt,創(chuàng)建top.sls文件俏站,內(nèi)容包括要調(diào)用的state模塊讯蒲,如下:
'ubuntu1':
- foobarcom
第三,創(chuàng)建webserver.sls狀態(tài)文件乾翔,我們以安裝apache為例:
apache2:
pkg.installed: []
service.running:
- require:
- pkg: apache2
執(zhí)行命令:salt ubuntu1 state.highstate
ubuntu1:
----------
State: - pkg
Name: apache2
Function: installed
Result: True
Comment: Package apache2 is already installed
Changes:
----------
State: - service
Name: apache2
Function: running
Result: True
Comment: The service apache2 is already running
Changes:
Summary
------------
Succeeded: 2
Failed: 0
------------
Total: 2
如果有dev下有多個狀態(tài)文件爱葵,可以base下面的top.sls文件中加載狀態(tài)文件施戴。