Saltstack提供了非常豐富的功能模塊,設計操作系統的基礎功能,常用工具支持等,
官網模塊介紹
http://docs.saltstack.com/ref/modules/all/index.html
一 列出當前版本支持的模塊
[root@hzbj-salt-020 ~]# salt '*' sys.list_modules hzbj-tomcat-021: - acl - aliases - alternatives - apache - archive - artifactory - blockdev - btrfs - buildout - cloud - cmd - composer - config - container_resource - cp - cron - data - defaults - devmap - disk - django - dnsmasq - dnsutil - drbd - elasticsearch - environ - etcd - event
- extfs - file - gem - genesis - git - grains - group - grub - hashutil - hg - hipchat - hosts - http - img - incron - ini - introspect - ip - iptables - jboss7 - jboss7_cli - key - kmod - locale - locate - logrotate - lowpkg - lvm - match - mine - modjk - mount - network - openstack_config - pagerduty - pillar - pip - pkg - pkg_resource - postfix - publish - pyenv - raid - random - random_org - rbenv - ret - rsync - runit - rvm - s3 - saltutil - schedule - scsi - sdb - seed - serverdensity_device - service - shadow - slack - smtp - sqlite3 - ssh - state - status - supervisord - sys - sysctl - syslog_ng - system - test - timezone - user - vbox_guest - virtualenv - webutil - xfs
所有主機saltstack支持的模塊清單(打印部分)
二 模塊及api
import salt.client client = salt.client.LocalClient() ret = client.cmd('*','test.ping') print ret
(1) Archive模塊
1)功能:實現系統層面的壓壓縮包調用,支持gunzip,gzip,rar,tar,unrar,unzip等。
2)示例:
(2)cmd模塊
1)功能:實現遠程的命令行調用執行(默認具備root操作權限,使用時需要評估風險)
2)示例:
#獲取所有被控主機的內存使用情況 [root@hzbj-salt-020 ~]# salt '*' cmd.run "free -m" hzbj-tomcat-022: total used free shared buffers cached Mem: 980 715 264 0 55 476
-/+ buffers/cache: 184 796 Swap: 1983 0 1983 hzbj-tomcat-021: total used free shared buffers cached Mem: 980 884 95 0 25 206
-/+ buffers/cache: 652 327 Swap: 1983 11 1972 #在hzbj-tomcat-022主機運行test.sh腳本,其中scripts/test.sh存放在file_roots指定的目錄, #改命令會做兩個動作:首選同步test.sh到minion的cache目錄(如同步到/var/cache/salt/ #minion/files/base/script/test.sh):其次運行改腳本 [root@hzbj-salt-020 ~]# salt 'hzbj-tomcat-01' cmd.script salt://script/test.sh
3)API調用
client.cmd('hzbj-tomcat-021','cmd.run',['free -m'])
(3)cp模塊
1)功能:實現遠程文件,目錄的復制,以及下載URL文件等操作
2)示例:
將注服務器file_roots指定位置下的目錄復制到被控主機 salt '*' cp.get_dir salt://path/to/dir /minion/dest
將主服務器file_roots指定文職下的文件復制到被控主機 salt '*' cp.get_file salt://path/to/file /minion/dest
下載URL內容到被控主機指定位置 salt '*' cp.get_url http://www.salshdot.org /tmp/index.html
3)API調用
client.cmd('hzbj-tomcat-021','cp.get_file',['salt://path/to/file','/minion/dest'])
(4)cron模塊
1)功能:實現被控主機的crontab操作
2)示例
查看指定被控主機,root用戶的crontab清單 [root@hzbj-salt-020 salt]# salt 'hzbj-tomcat-021' cron.raw_cron root hzbj-tomcat-021: */2 * * * * ntpdate pool.ntp.org &>/dev/null
*/1 * * * * cd /usr/local/gse/; ./cron_gse.sh 1>/dev/null 2>&1 #指定的被控主機,root用戶添加/usr/local/weekly任務作業 [root@hzbj-salt-020 salt]# salt 'hzbj-tomcat-021' cron.raw_cron root hzbj-tomcat-021: */2 * * * * ntpdate pool.ntp.org &>/dev/null
*/1 * * * * cd /usr/local/gse/; ./cron_gse.sh 1>/dev/null 2>&1 # Lines below here are managed by Salt, do not edit * * * * 1 /usr/local/weekly #刪除指定的被控主機,root用戶添加/usr/local/weekly任務作業 [root@hzbj-salt-020 salt]# salt 'hzbj-tomcat-021' cron.rm_job root /usr/local/weekly hzbj-tomcat-021: removed
注解: 如果需要寫入sls文件里面
/usr/sbin/ntpdate pool.ntp.org >>/dev/null 2>&1:
cron.present:
- user: root
- minute: 10
minute #分鍾
hour #小時
daymonth #日
month #月
dayweek #0~6是周一到周六,周日是7
默認執行用戶“root”,時間不填寫默認是“*”,周期執行“*/2”,隨機執行“random”,identifier 是任務標識符,類似於任務ID,更改任務時指定“標識符”可方便修改
3)API調用
client.cmd('hzbj-tomcat-021','cron.set_job',['root','*','*','*','*','*','/usr/echo'])
(5)dnsutil模塊
1)功能:實現被控主機通用DNS相關操作。
2)示例:
[root@hzbj-salt-020 ~]# salt '*' dnsutil.hosts_append /etc/hosts 127.0.0.1 ad1.yuk.com 刪除指定被控主機hosts的主機批准 [root@hzbj-salt-020 ~]# salt '*' dnsutil.hosts_remove /etc/hosts ad1.yuk.com
3)API調用
client.cmd('hzbj-tomcat-021','dnsutil.hosts_append',['/etc/hosts','127.0.01','ad1.yuk.co'])
(6)file模塊
1)功能:被控主機文件操作,包括文件讀寫,權限,查找,校驗等。
(9)pkg包管理模塊
1)功能:被控主機程序包管理,如yum,apt-get
2)示例:
#為所有被控主機安裝PHP環境,根據不同系統發現版調用不同安裝工具進行部署,如redhat 平台的yum,等價於yum -y install php salt '*' pkg.install php #卸載所有被控主機的PHP salt '*' pkg.remove php #升級所有被控主機的軟件包 salt '*' pkg.upgrade
3)API調用
client.cmd('hzbj-tomcat-021','pkg.remove',['php'])
(10)Service服務模塊
1)功能:被控主機程序包服務管理
2)示例:
#開啟(enable),禁用(disable)nginx開機自啟動服務 salt '*' service.enable nginx salt '*' service.disable nginx #針對nginx服務的reload,restart,start,stop,ss操作 salt '*' service.reload nginx salt '*' service.restart nginx salt '*' service.start nginx salt '*' service.stop nginx salt '*' service.status nginx
3)API調用
client.cmd('hzbj-tomcat-021','service.stop',['nginx'])
(11) User模塊
user.present
user.present: 確保指定的賬戶名存在,並指定其對應的屬性. 這些屬性包括如下內容:
name: 指定需要管理的賬戶名.
uid: 指定uid, 如果不設置將配自動分配下一個有效的uid.
gid: 指定默認的組id(group id)
gid_from_name: 如果設置為_True_,默認的組id將自動設置為和本用戶同名的組id
groups: 分配給該用戶的組列表(a list of groups). 如果組在minion上不存在,則本state會報錯. 如果設置會空,將會刪除本用戶所屬的除了默認組之外的其他組
optional_groups: 分配給用戶的組列表。 如果組在minion上不存在,則state會忽略它.
home: 關於用戶的家目錄(home directory).
password: 設置用戶hash之后的密碼.
enforce_password: 當設置為_False_時,如果設置的_password_與用戶原密碼不同,將保持原密碼不做更改.如果沒有設置_password_選項,該選項將自動忽略掉.
shell: 指定用戶的login shell。 默認將設置為系統默認shell。
unique: UID唯一,默認為True.
system: 從_FIRST_SYSTEM_UID_和_LAST_SYSTEM_UID_間選擇一個隨機的UID.
user.absent
本部分為譯者依據官方手冊進行的補充,原文中並沒有相關內容
user.absent 用於刪除用戶.其有以下選項:
name: 指定需要刪除的用戶名.
purge: 設置清除用戶的文件(家目錄)
force: 如果用戶當前已登錄,則absent state會失敗. 設置_force_選項為True時,就算用戶當前處於登錄狀態也會刪除本用戶.
當管理用戶時,至少需要指定_user.present_或_user.absent_。 其他選項是可選的,比如_uid_、gid、_home_等選項沒有指定是,將自動使用下一個有效的或者系統默認的.
例子:
如果需要設置密碼 首選hash密碼
openssl passwd -1 -salt 'cedwards'
創建用戶
# cat cedwards.sls cedwards: user.present: - fullname: Christer Edwards - password: '$1$cedward$KcHcszCMgNJkDwSID7QRH.' - shell: /bin/bash - home: /home/cedwards - uid: 1002 - gid: 1002 - groups: - wheel
刪除用戶
# cat userdel.sls cedwards: user.absent: - purge: Ture - force: Ture
批量創建用戶
# cat useradd.sls {% set users = ['jim','hwg','hyxc'] %} {% for user in users %} {{ user }}: user.present: - shell: /bin/bash - home: /home/{{ user }} - password: '$1$cedwards$Sy0q724HkzgJsIWMsQIkq.' {% endfor %}