Saltstack Series 3: Saltstack common module and API

Explanation

salt '*' sys.list_modules # list the modules support the current version

API works by calling master client module, instantiating a LocalClient object, then call cmd () method to achieve.
The following example test.ping API implementation of :( other API calls can only be changed cmd)

Copy the code
salt.client Import 

Client salt.client.LocalClient = () 

RET = client.cmd ( '*', 'test.ping)     
within #cmd format:' <manipulation target> "," <module> "," [parameters] '. Example: '*', 'cmd.run', [ '-H DF'] 

Print RET
Copy the code

 

 Common Module

Archive module

Function: realization archive system call level, support gzip, gunzip, rar, tar, unrar, unzip , etc.

Example:

# Package using gunzip it sourcefile.txt.gz 
Salt '*' archive.gunzip sourcefile.txt.gz 

# gzip-compressed file sourcefile.txt salt '*' archive.gzip sourcefile.txt

API call:

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

 

cmd module

Features: remote command-line invocation execution (the default operating with root privileges, need to assess the risks when used)

Example:

# Get the desired memory usage charged with a host of 
Salt '*' cmd.run 'as Free -m' # wx run test.py script on the host, which script / test.py file_roots stored in the specified directory (default is in / srv / salt, custom defined in the / etc / salt / master file), # 
the command will do two actions: first synchronize test.py to a minion of the cache directory; get up to run the script 
salt 'wx' cmd.script salt: //script/test.py

API call:

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

 

cp module

Function: remote file, copy directory, and download files, and so URL

Example:

Copy the code
# Copy the host will be charged / etc / hosts file to the controlled local host salt cache directory (/ var / Cache / Salt / Minion / LocalFiles /) 
Salt '*' cp.cache_local_file / etc / hosts # the master copy the directory under file_roots specified location to the next accused of a host / minion / directory 
Salt '*' cp.get_dir Salt: // Script / / minion / # to copy the files in the specified location to the controlled master file_roots host / minion /test.py file (file is the file name) 
Salt '*' cp.get_dir Salt: //script/test.py /minion/test.py # download URL of the content to the controlled host specified location (/tmp/index.html ) 
Salt '*' cp.get_url HTTP: //www.slashdot.ort /tmp/index.html



Copy the code

API call:

client.cmd('*','cp.get_file',['salt://script/test.py','/minion/test.py'])

 

cron module

Function: to achieve crontab operation controlled host

Example:

Copy the code
# Display the accused host, root user's crontab operations 
Salt 'WX' cron.raw_cron root 
# is charged with the specified host, root users to add / usr / local / weekly task zuoye Salt 'WX' cron.set_job root '*' ' * '' * '' * '1 / usr / local / weekly
# delete host for the accused, root user's crontab / usr / local / weekly task zuoye Salt' WX 'cron.rm_job root / usr / local / weekly
Copy the code

API call:

client.cmd('wx','cron.set_job',['root','*','*','*','*',1,'/usr/local/weekly'])

 

dnsutil module

Function: to achieve universal host controlled DNS operations

 

Example:

# Add Host configuration items for the accused host hosts of 
salt 'WX' dnsutil.hosts_append / etc / hosts 127.0.0.1 adl.yuk.com, ad2.yuk.com 
# delete the specified host configuration item accused the host of hosts of salt ' wx 'dnsutil.hosts_remove / etc / hosts ad1.yuk.com

API call:

clietn.cmd('wx','dnsutil.hosts_append',['/etc/hosts','127.0.0.1','ad1.yuk.com','ad2.yuk.com'])

 

file module

Function: accused of a host of common file operations, including file read and write permissions, find, check

Example:

Copy the code
All controlled check hosts # / etc / fstab file md5 xxxxxxxxxxxxx whether the value, the return value is True consistent 
Salt '*' file.check_hash / etc / fstab md5 = xxxxxxxxxxxxxxxxxxxxx 

# controlled check all the encrypted information in the host file, supports md5, sha1, sha224, shs256, sha384, sha512 encryption algorithm 
Salt '*' file.get_sum / etc / passwd MD5 

# change all charged with hosts / etc / group belongs, user rights passwd file, equivalent to the chown root: root / etc / passwd 
Salt '*' file.chown / etc / passwd root root 

# copy all charged with the host / path / to / src file to the local / path / to / dst file 
salt '*' file.copy / path / to / src / path / to / dst 

# check all charged with hosts / etc directory exists, there is the return True, to check whether a file exists using the method file.file_exists 
Salt '*' file.directory_exists / etc 

# get all accused host / etc / passwd the stats information 
Salt '*' file.stats / etc / passwd 

# get all charged with hosts / etc permissions / passwd the mode, such as 755,644 
Salt '*' File.get_mode / etc / passwd 

# change all charged with hosts / etc / passwd permissions mode to 0644
Salt '*' file.set_mode / etc / passwd 0644 

# in all charged with the host to create / opt / test directory of 
Salt '*' file.mkdir / opt / test 

# /etc/httpd/httpd.conf hosts file to all accused warn LogLevel parameter value is modified to info 
Salt '*' file.sed /etc/httpd/httpd.conf 'LogLevel warn' 'LogLevel info' 

# additional content to all accused hosts file /tmp/test/test.conf 'maxclient 100' 
Salt '*' file.append /tmp/test/test.conf 'maxclient 100' 

# delete all charged with the host / tmp / foo file 
salt '*' file.remove / tmp / foo
Copy the code

API call:

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

 

iptables module

Function: charged with iptables support host

Example:

Copy the code
# Append all host controlled (the append), insert (insert) iptables rules, wherein the input INPUT chain 
Salt '*' = iptables.append filter rule INPUT '- m The RELATED --state State, the ESTABLISHED -j ACCEPT' 
Salt '*' = iptables.insert position the INPUT filter rule. 3 = '- m the RELATED --state State, the ESTABLISHED -j ACCEPT' 

# remove all host controlled chain designated number (position = 3) or the presence of specified rules. 3 
Salt '*' = iptalbes.delete position the INPUT filter. 3 
Salt '*' = iptables.delete filter rule the INPUT '- m --state RELATEC State, the ESTABLISHED -j ACCEPT' 

# save all the rules of the host to the host-side controlled local hard disk (/ etc / sysconfig / iptables) 
Salt '*' iptables.save / etc / sysconfig / iptables
Copy the code

API call:

client.cmd('*','iptables.append',['filter','INPUT','rule=\'-p tcp --sport 80 -j ACCEPT\''])

 

network module

Function: Returns the alleged network information for a host

Example:

Copy the code
# In the host for the accused to obtain dig, ping, traceroute directory domain information 
Salt 'WX' network.dig www.qq.com 
Salt 'WX' network.ping www.qq.com 
Salt 'WX' network.traceroute www.qq. COM 

# Get mac address specified host charged 
Salt 'WX' network.hwaddr eth0 

# detect whether the host specified charged 10.0.0.0/16 subnet belongs returns True 
Salt 'WX' network.in_subnet 10.0.0.0/ 16 

# Gets the host card configuration information of the accused 
salt 'wx' network.interfaces 

# obtain the IP address of the host configuration information for the accused 
salt 'wx' network.ip_addrs 

# Gets the accused host subnet information 
salt 'wx' network.subnets
Copy the code

API call:

client.cmd('wx','network.ip_addrs')

 

pkg package management module

Function: controlled package management host, such as: yum, apt-getdegn

Example:

Copy the code
# PHP installation for all controlled host environment, call different installation tool yum deployments, such as redhat platform depending on the system release, equivalent to -y install PHP yum 
Salt '*' pkg.install PHP 

# unload all accused of hosts PHP environment 
Salt '*' pkg.remove PHP 

# upgrade all packages charged with a host of 
salt '*' pkg.upgrade
Copy the code

API call:

client.cmd('*','pkg.remove',['php'])

 

service service module

Function: charged Hosting Package Service Management

Example:

Copy the code
# Turn on (enable), disabled (disable) nginx boot from the startup script 
salt '*' service.enable nginx 
salt '*' service.disable nginx 

# services for nginx reload, restart, start, stop, status operating 
salt '*' Nginx service.reload 
Salt '*' service.restart Nginx 
Salt '*' service.start Nginx 
Salt '*' service.stop Nginx 
Salt '*' service.status Nginx
Copy the code

API call:

client.cmd('*','service.stop',['nginx'])

 

Other modules

In addition to the above modules, saltstack also provides a user (system user module), Group (Systems group of modules), Partition (system partition module), puppet (puppet Management Module), System (system restart, shutdown modules), TimeZone (time zone management module), nginx (nginx management module), mount (mount the file system module) and so on. Of course, we can also meet the needs of a Python extension modules.

 


References:

According to Lau Si "Python automated operation and maintenance technology and best practices" Finishing

Guess you like

Origin www.cnblogs.com/andy6/p/11705007.html