Source: Python Automated operations development
Test: Import salt.clientclient = salt.client.localclient () ret = client.cmd (' * ', ') Test.ping ') Print ret (1) Archive Module 1) function: To achieve the system level of compression package calls, support Gunzip, gzip, RAR, tar, Unrar, unzip and so on. 2) Example: #采用gzunzip解压/tmp/sourcefile.txt.gz package salt ' * ' archive.gunzip /tmp/sourcefile.txt.gz# with gzip compression /tmp/sourcefile.txt file salt ' * ' archive.gzip /tmp/sourcefile.txt3) API call: Client.cmd (' * ', ' archive.gunzip ', ['/tmp/sourcefile.txt.gz ']) (2) CMD Module 1) function: Implement remote command line invocation execution (the default is root, you need to evaluate the risk when using). 2) Example: #获取所有被控主机的内存使用情况salt ' * ' cmd.run "free -m" #在SN2013-08-021 Host runs test.sh script, where script/ test.sh is stored in the directory specified by the file_roots, #该命令会做两个动作: First synchronize the test.sh to the Minion cache directory (such as sync to/var/cache/salt/#minion/files/base/script/ test.sh); Next run the script ' sn2013-08-021 ' cmd.script salt://script/test.sh3) API call: Client.cmd (' sn2013-08-021 ', ' Cmd.run ', [' free -m ']) (3) CP Module 1) function: Remote file, directory replication, download URL files and other operations. 2) Example: #将指定被控主机的/etc/hosts file to the Salt cache directory that is local to the managed host (/var/cache/salt/minion/localfiles/);salt ' * ' cp.cache_local_file /etc/hosts# copy the primary server File_roots directory under the specified location to the managed host salt ' * ' cp.get_dir salt://path/to/dir/ /minion/dest# Copy the file from the primary server file_roots the specified location to the managed host salt ' * ' cp.get_ file salt://path/to/file /minion/dest# download URL content to the host specified location salt ' * ' cp.get_url http:// WWW.SLASHDOT.ORG /TMP/INDEX.HTML3) API call: Client.cmd (' sn2013-08-021 ', ' cp.get_file ', [' salt:// path/to/file ', '/minion/dest ']) (4) Cron Module 1) function: Realize the crontab operation of the controlled host. 2) Example: #查看指定被控主机, root user crontab manifest salt ' sn2013-08-022 ' cron.raw_cron root# add/usr/for the specified managed host, root user local/weekly task Job salt ' sn2013-08-022 ' cron.set_job root ' * ' ' * ' ' * ' ' * ' 1 /usr/local/weekly# Delete the specified managed host, root user crontab/usr/local/weekly task job salt ' sn2013-08-022 ' CRON.RM_JOB ROOT /USR/LOCAL/WEEKLY3) API call: Client.cmd (' sn2013-08-021 ', ' cron.set_job ', [' root ', ' * ', ' * ', ' * ', ' * ', ' * ', '/usr/echo ']) (5) Dnsutil Module 1) function: To implement general DNS-related operations of the controlled host. 2) Example: #添加Specifies the host configuration entry for the hosted hosts salt ' * ' dnsutil.hosts_append /etc/hosts 127.0.0.1 ad1.yuk.com,ad2.yuk.com# Remove Host configuration entry for specified host hosts salt ' * ' dnsutil.hosts_remove /etc/hosts AD1.YUK.COM3) API call: Client.cmd (' * ', ' dnsutil.hosts_append ', ['/etc/hosts ', ' 127.0.0.1 ', ' ad1.yuk.co ']) (6) File Module 1) function: The host files are controlled common operations, including file read and write, permissions, find, check and so on. 2) Example: If the MD5 of the #校验所有被控主机/etc/fstab file is 6254e84e2f6ffa54e0c8d9cb230f5505, a consistent return truesalt ' * ' file.check_hash /etc/fstab md5=6254e84e2f6ffa54e0c8d9cb230f5505# verifies the encrypted information of all controlled host files, supports MD5, SHA1, sha224, SHA256, sha384, SHA512 encryption Algorithm salt ' * ' file.get_sum /etc/passwd md5# modify the group and user rights of all controlled host/etc/passwd files, equivalent to Chown root:root /etc/passwdsalt ' * ' file.chown /etc/passwd root root# copy all controlled host local/ PATH/TO/SRC file to local/path/to/dst file salt ' * ' file.copy /path/to/src /path/to/dst# check all hosts/ If the ETC directory exists, the presence returns true to check if the file exists using the File.file_exists method salt ' * ' file.directory_exists /etc# get all the managed hosts/ St of ETC/PASSWDATS Information salt ' * ' file.stats /etc/passwd# gain access to all controlled hosts/etc/passwd mode, such as 755, 644salt ' * ' file.get_mode /etc/passwd# Modify the permissions of all controlled hosts/etc/passwd mode is 0644salt ' * ' file.set_mode /etc/ passwd 0644# Create/opt/test directory salt ' * ' file.mkdir /opt/test# all controlled hosts in all controlled hosts/etc/httpd/ The warn value of the LogLevel parameter of the httpd.conf file is modified to infosalt ' * ' file.sed /etc/httpd/httpd.conf ' LogLevel warn ' ' loglevel info ' #给所有被控主机的/tmp/test/test.conf file append "maxclient 100" salt ' * ' file.append /tmp/test/test.conf "maxclient 100" #删除所有被控主机的/tmp/foo file salt ' * ' FILE.REMOVE /TMP/FOO3) API call: Client.cmd (' * ', ' file.remove ', ['/tmp/foo ']) (7) Iptables Module 1) function : Controlled host iptables support. 2) Example: #在所有被控端主机追加 (append), inserting (insert) iptables rule, where input is salt ' * ' for the input chain iptables.append filter Input rule= '-m state --state related,established -j accept ' salt ' * ' Iptables.insert filter&nbSp;input position=3 rule= '-m state --staterelated,established -j accept ' # Delete the specified chain number 3 (position=3) or specify the existence of the rule salt ' * ' iptables.delete filter input position= on all managed side hosts 3salt ' * ' iptables.delete filter input rule= '-m state --state related, Established -j accept ' #保存所有被控端主机规则到本地硬盘 (/etc/sysconfig/iptables) salt ' * ' iptables.save  /ETC/SYSCONFIG/IPTABLES3) API call: Client.cmd (' sn2013-08-022 ', ' iptables.append ', [' Filter ', ' INPUT ', ' Rule=\ '-p tcp --sport 80 -j accept\ ']) (8) Netwrok Module 1) Function: Returns the network information of the controlled host. 2) Example: #在指定被控主机 ' sn2013-08-022 ' Get dig, ping, traceroute directory domain name information salt ' sn2013-08-022 ' network.dig www.qq.comsalt ' sn2013-08-022 ' network.ping www.qq.comsalt ' sn2013-08-022 ' network.traceroute www.qq.com# gets the MAC address of the specified managed host ' sn2013-08-022 ' salt ' sn2013-08-022 ' network.hwaddr eth0# detection Specifies whether the managed host ' sn2013-08-022 ' belongs to the 10.0.0.0/16 subnet range and belongs to the TRuesalt ' sn2013-08-022 ' network.in_subnet 10.0.0.0/16# gets the NIC configuration information for the specified host ' sn2013-08-022 ' salt ' sn2013-08-022 ' network.interfaces# Gets the IP address configuration information for the specified managed host ' sn2013-08-022 ' salt ' sn2013-08-022 ' Network.ip_addrs# gets the subnet information for the specified managed host ' sn2013-08-022 ' salt ' sn2013-08-022 ' network.subnets3) API call: Client.cmd ( ' sn2013-08-022 ', ' Network.ip_addrs ') (9) PKG Package Management Module 1) function: Managed host package management, such as Yum, Apt-get, etc. 2) Example: #为所有被控主机安装PHP环境, depending on the distribution of the system to invoke different installation tools for deployment, such as the Redhat platform Yum, equivalent to yum -y install phpsalt ' * ' pkg.install php# Uninstall all host-controlled PHP environments salt ' * ' pkg.remove php# upgrade all host-controlled packages salt ' * '  PKG.UPGRADE3) API call: Client.cmd (' sn2013-08-022 ', ' pkg.remove ', [' php ']) (ten) Service Module 1) function: Managed host Package service. 2) Example: #开启 (enable), disable (disable) nginx Start-up service salt ' * ' service.enable nginxsalt ' * ' service.disable nginx# for Nginx service Reload, restart, start, stop, status Operation salt ' * ' service.reload nginxsalt ' * ' service.restart nginxsalt ' * ' service.start nginxsalt ' * ' service.stop nginxsalt ' * ' service.status  NGINX3) API call: Client.cmd (' sn2013-08-022 ', ' service.stop ', [' Nginx '])
This article is from the "Struggle Bar" blog, please be sure to keep this source http://lvnian.blog.51cto.com/7155281/1873949
Saltstack API and the corresponding Python template