Saltstack經常使用模塊及API

  Saltstack提供了很是豐富的功能模塊,涉及操做系統的基礎功能、經常使用工具支持等,更多模塊信息能夠查看官網模塊介紹。也能夠經過sys模塊列出當前版本支持的模塊。php

salt '*' sys.list_modules

1、模塊API使用方法:html

 API的原理是經過調用 master client 模塊,實例化一個LocalClient 對象,再調用cmd()方法來實現的。如下是API實現test.ping的示例:node

>>> import salt.client
>>> client = salt.client.LocalClient()
>>> ret = client.cmd('*','test.ping')        
>>> print ret

    結果以一個標準的python字典形式的字符串返回,能夠經過eval()函數轉換成python的字典類型,方便後續的業務邏輯處理,程序運行結果以下:python

{'SN100-129': True, 'SN100-128': True, 'SN100-130': True}

2、常見的模塊介紹及使用方法:mysql

(1) Archive模塊linux

1)功能:實現系統層面的壓縮包調用,支持gzip、gunzip、rar、tar、unrar、unzip等。nginx

2)示例: 算法

#採用gzip壓縮/tmp/sourcefile.txt文件,sourcefile.txt 是在客戶端存在的文件。
[root@localhost ]# salt '*' archive.gzip /tmp/sourcefile.txt 

#採用gunzip解壓/tmp/sourcefile.txt.gz包

[root@localhost~]# salt '*' archive.gunzip /tmp/sourcefile.txt.gz 

3)API調用:sql

client.cmd('*','archive.gunzip',['/tmp/sourcefile.txt.gz'])

(2) cmd模塊apache

1)功能:實現遠程的命令行調用執行(默認具有root操做權限,使用時需評估風險)

2)示例:

#獲取因此主機的內存使用狀況
[root@localhost~]# salt '*' cmd.run "free -m"

#在SN100-128主機運行test.sh腳本,其中script/test.sh存放在file_roots指定的目錄,默認目錄爲(/srv/salt)
#該命令會作兩個動做:首先同步test.sh到minion的cache目錄(/var/cache/salt/minion/files/base/script/);其次運行該腳本,命令以下:

[root@localhost]# salt 'SN100-128' cmd.script salt://script/test.sh 

3)API調用:

client.cmd('SN100-128','cmd.run',['free -m'])

(3) cp 模塊

1) 功能:實現遠程文件、目錄的複製,以及下載URL文件等操做。

2)示例:

#將指定被控主機的/etc/hosts文件複製到被控主機本地的salt cache目錄(/var/cache/salt/minion/localfiles);

[root@localhost~]# salt '*' cp.cache_local_file /etc/hosts

個人file_roots在/srv/salt下面,目錄結構以下:
[root@localhost salt]# tree /srv/salt/
/srv/salt/
├── apache
│   └── 1.txt
├── mysql
├── nginx
│   └── nginx.conf
├── script
│   ├── test.py
│   └── test.sh
└── software
    └── soft

6 directories, 4 files
#將主服務器file_roots指定位置下的目錄複製到被控主機

[root@locahost salt]# salt '*' cp.get_dir salt://apache/ /minion/dest

#將主服務器file_roots指定位置下的文件複製到被控主機

[root@localhost ~]# salt '*' cp.get_file salt://nginx/nginx.conf /minion/dest/nginx.conf    #複製文件

[root@localhost ~]# salt '*' cp.get_file salt://nginx/nginx.conf /minion/dest/test_nginx.conf   #複製文件並重命名

#下載URL內容到被控主機指定位置(將rpm包下載到salt-minion客戶端的/tmp目錄.)

salt '*' cp.get_url http://ftp.linux.ncsu.edu/pub/epel/6/i386/epel-release-6-8.noarch.rpm  /tmp/epel-releas

3)API調用:

client.cmd('SN100-128','cp.get_file',['salt://script/test.py','/tmp/test.py'])

(4) cron 模塊

1)功能:實現被控主機的crontab操做。

2)示例:

#查看指定被控主機、root用戶的crontab 清單

[root@localhost~]# salt 'SN100-128' cron.raw_cron root                
SN100-128:
    0 */2 * * *     /opt/bin/crontab/date.sh

#爲指定的被控主機、root用戶添加/usr/local/weekly任務做業

[root@localhost~]# salt 'SN100-128' cron.set_job root '*' '*' '*' '*' 1 /usr/local/weekly
SN100-128:
    new
#/usr/local/weekly文件在被控主機上是存在的

#刪除指定的被控主機、root用戶crontab 的/usr/local/weekly任務做業

[root@localhost~]# salt 'SN100-128' cron.rm_job root /usr/local/weekly
SN100-128:
    removed

3)API調用:

>>> import salt.client
>>> client = salt.client.LocalClient()
>>> ret = client.cmd('SN100-128','cron.set_job',['root','*','*','*','*','*','/usr/echo'])
>>> print ret
{'SN100-128': 'new'}

(5) dnsutil 模塊

1)功能:實現被控主機通用DNS相關操做。

2)示例:

#添加指定被控主機hosts的主機配置項

[root@localhost~]# salt '*' dnsutil.hosts_append /etc/hosts 1.1.1.1 abc.com,bcd.com,
SN100-128:
    The following line was added to /etc/hosts:
    1.1.1.1 abc.com bcd.com 
SN100-129:
    The following line was added to /etc/hosts:
    1.1.1.1 abc.com bcd.com 
SN100-130:
    The following line was added to /etc/hosts:
    1.1.1.1 abc.com bcd.com 

#刪除指定被控主機hosts的主機配置項

[root@localhost~]# salt '*' dnsutil.hosts_remove /etc/hosts abc.com
SN100-128:
    None
SN100-129:
    None
SN100-130:
    None

3)API 調用

client.cmd('*','dnsutil.hosts_append',['/etc/hosts','127.0.0.1','abc.com'])

(6) file 模塊

1)功能:被控主機文件常見操做,包括文件讀寫、權限、查找、校驗等。

2)示例:

#修改因此被控主機/etc/passwd 文件的屬組、用戶權限,等價於chown root:root /etc/passwd

salt '*' file.chown /etc/passwd root root

#複製全部被控主機本地/path/to/src 文件到/path/to/dst 文件

salt 'SN100-128' file.copy /root/filename /tmp/filename

#將SN100-128主機/root/filename文件複製到/tmp/filename.

#檢查全部被控主機/etc 目錄是否存在,存在則返回True,檢查文件是否存在使用file.file_exists方法

[root@localhost~]# salt '*' file.directory_exists /etc
SN100-128:
    True
SN100-129:
    True
SN100-130:
    True

#獲取因此被控主機/etc/passwd 的stats信息

[root@localhost~]# salt 'SN100-128' file.stats /etc/passwd 
SN100-128:
    ----------
    atime:
        1465195261.8
    ctime:
        1465195234.02
    gid:
        0
    group:
        root
    inode:
        1572021
    mode:
        0644
    mtime:
        1462760708.29
    size:
        1740
    target:
        /etc/passwd
    type:
        file
    uid:
        0
    user:
        root

#獲取全部被控主機/etc/passwd 的權限mode,如75五、644

[root@localhost~]# salt '*' file.get_mode /etc/passwd
SN100-128:
    0644
SN100-129:
    0644
SN100-130:
    0644

#在全部被控主機建立/opt/test目錄

[root@localhost~]# salt '*' file.mkdir /opt/test
SN100-128:
    None
SN100-129:
    None
SN100-130:
    None

#刪除全部被控主機的/tmp/test 文件(文件或目錄)

[root@localhost~]# salt '*' file.remove /opt/test
SN100-128:
    True
SN100-130:
    True
SN100-129:
    True

#給全部被控主機的/tmp/test/test.conf 文件追加內容"Maxclient 100"

 salt '*' file.append /tmp/test/test.conf "maxclient 100"  

#將全部被控主機/etc/httpd/httpd.conf 文件的LogLevel 參數warn 值修改爲info

salt '*' file.sed /etc/httpd/httpd.conf 'LogLevel warn' 'LogLevel info'

#校驗全部被控主機文件的加密信息、支持md五、sha一、sha22四、sha25六、sha38四、sha512加密算法

salt '*' file.get_sum /etc/passwd md5

3) API調用

client.cmd('*','file.remove',['/tmp/foo'])

(7) network 模塊

1)功能:返回被控主機網絡信息。

2)示例:

#在指定被控主機'SN100-128'獲取dig、ping、traceroute目錄域名信息.

salt 'SN100-128' network.dig www.9888.cn

salt 'SN100-128' network.ping www.9888.cn

 salt 'SN100-128' network.traceroute www.9888.cn

#獲取指定被控主機‘SN100-128’的 MAC 地址

[root@localhost~]# salt 'SN100-128' network.hwaddr eth0
SN100-128:
    00:50:56:87:b1:54

#獲取指定被控主機‘SN100-128’的網卡配置信息

salt 'SN100-128' network.interfaces

#獲取指定被控主機'SN100-128'的IP地址配置信息

[root@localhost~]# salt '*' network.ip_addrs
SN100-128:
    - 10.10.100.128

#獲取指定被控主機‘SN100-128’的子網信息

[root@localhost~]# salt 'SN100-128' network.subnets
SN100-128:
    - 10.10.100.0/24

#檢測指定被控主機'SN100-128'是否屬於10.0.0.0/16子網範圍,屬於則返回True.

salt '*' network.in_subnet 10.0.0.0/16

3) API調用:

>>> import salt.client
>>> 
>>> client = salt.client.LocalClient()
>>> ret = client.cmd('SN100-128','network.ip_addrs')
>>> 
>>> print ret
{'SN100-128': ['10.10.100.128']}
>>> 

(8) pkg 包管理模塊

1)功能:被控主機程序包管理,如yum、apt-get等。

2)示例:

#爲被控主機安裝PHP環境,根據不一樣系統發行版調用不一樣安裝工具進行部署,如redhat平臺的yum,等價於yum -y install php

salt 'SN100-128' pkg.install php

#卸載被控端主機的PHP環境

salt 'SN100-128' pkg.remove php

#升級被控端主機的軟件包

salt '*' pkg.upgrade

3)API調用:

client.cmd('SN100-128','pkg.remove',['php'])

(9) Service 服務模塊

1)功能:被控主機程序包服務管理。

2)示例:

#開啓(enable)、禁用(disable) nginx開機自啓動服務

salt 'SN100-128' service.enable nginx
salt 'SN100-128' service.disable nginx

#針對nginx服務的reload、restart、start、stop、status操做

salt '*' service.reload nginx
salt '*' service.restart nginx
salt '*' service.start nginx
salt '*' service.stop nginx
salt '*' service.status nginx

3) API調用:

client.cmd('SN100-128','service.stop',['nginx'])

其餘模塊:

經過上面介紹的幾個經常使用模塊,基本上已經覆蓋平常運維操做。Saltstack還提供瞭如下模塊.

user(系統用戶模塊)

group(系統組模塊)

partition(系統分區模塊)

puppet(puppet管理模塊)

system(系統重啓、關機模塊)

timezone(時區管理模塊)

nginx(Nginx管理模塊)

iptables(被控主機iptables支持模塊)

mount(文件系統掛載模塊) 等等...

更多模塊查看官網介紹:

http://docs.saltstack.com/ref/modules/all/index.html#all-salt-modules

固然也能夠經過python擴展功能模塊來知足需求...

相關文章
相關標籤/搜索