saltstack使用指南----常用执行模块
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了saltstack使用指南----常用执行模块相关的知识,希望对你有一定的参考价值。
saltstack常用执行模块:
cron模块
archive模块
cmd模块
cp模块
dnsutil模块
file模块
group模块
network模块
service模块
pkg模块
user模块
一、cron模块:
功能:实现被控主机的crontab操作
[[email protected] ~]# salt ‘*‘ sys.list_functions cron
izwz9f8xrvty50quc2gq50z:
- cron.list_tab
- cron.ls
- cron.raw_cron
- cron.rm
- cron.rm_env
- cron.rm_job
- cron.set_env
- cron.set_job
- cron.set_special
- cron.write_cron_file
- cron.write_cron_file_verbose
[[email protected] ~]# salt ‘*‘ sys.doc cron.list_tab
‘cron.list_tab:‘
Return the contents of the specified user‘s crontab
CLI Example:
salt ‘*‘ cron.list_tab root
查看minion端定时任务
[[email protected] ~]# salt ‘*‘ cron.list_tab root
为指定的用户设置cron定时任务
[[email protected] ~]# salt ‘*‘ cron.set_job root ‘*‘ ‘*‘ ‘*‘ ‘*‘ 1 /root/a.sh
二、archive模块:
功能:实现系统层面的压缩包调用,支持gzip、gunzip、rar、tar、unrar、unzip等
[[email protected] ~]# salt ‘*‘ sys.list_functions archive
izwz9f8xrvty50quc2gq50z:
- archive.cmd_unzip
- archive.cmd_zip
- archive.gunzip
- archive.gzip
- archive.rar
- archive.tar
- archive.unrar
- archive.unzip
- archive.zip
zip/unzip
[[email protected] tmp]# salt ‘*‘ archive.zip /opt/a.zip /tmp/sourcefile1,/tmp/sourcefile2
[[email protected] ~]# salt ‘*‘ archive.unzip /opt/a.zip /tmp/
tar
[[email protected] ~]# salt ‘*‘ archive.tar zcvf /opt/file.tar.bz /tmp/file_1,/tmp/file_2
注:tar不支持模块解压
gzip/gunzip
[[email protected] ~]# salt ‘*‘ archive.gzip /tmp/b.txt
[[email protected] ~]# salt ‘*‘ archive.gunzip /tmp/a.txt.gz
三、cmd模块:
功能:实现远程的命令行调用执行(默认具备root操作权限,使用时需评估风险)
[[email protected] ~]# salt ‘*‘ sys.list_functions cmd
izwz9f8xrvty50quc2gq50z:
- cmd.exec_code
- cmd.exec_code_all
- cmd.has_exec
- cmd.retcode
- cmd.run
- cmd.run_all
- cmd.run_chroot
- cmd.run_stderr
- cmd.run_stdout
- cmd.script
- cmd.script_retcode
- cmd.shell
- cmd.shells
- cmd.tty
- cmd.which
- cmd.which_bin
cmd.run
[[email protected] ~]# salt ‘*‘ cmd.run ‘free -m‘
cmd.script
[[email protected] ~]# salt ‘*‘ cmd.script /root/a.sh
cmd.shell
[[email protected] ~]# salt ‘*‘ cmd.shell "sed -i s/zhang/123/g /root/b.txt"
四、cp模块:
功能:实现远程文件、目录的复制,以及下载URL文件等操作
[[email protected] ~]# salt ‘*‘ sys.list_functions cp
izwz9f8xrvty50quc2gq50z:
- cp.cache_dir
- cp.cache_file
- cp.cache_files
- cp.cache_local_file
- cp.cache_master
- cp.get_dir
- cp.get_file
- cp.get_file_str
- cp.get_template
- cp.get_url
- cp.hash_file
- cp.is_cached
- cp.list_master
- cp.list_master_dirs
- cp.list_master_symlinks
- cp.list_minion
- cp.list_states
- cp.push
- cp.push_dir
- cp.recv
cp.cache_local_file
将master端的/etc/hosts文件复制到minion端的salt cache目录(/var/cache/salt/minion/localfiles/)
[[email protected] salt]# salt ‘*‘ cp.cache_local_file /etc/hosts
cp.get_dir
将master端salt主目录下面的目录复制到minion端的/opt目录下
[[email protected] salt]# salt ‘*‘ cp.get_dir salt://user/ /opt/
cp.get_url
下载URL内容到被控主机指定位置(/tmp/index.html)
salt ‘*‘ cp.get_url http://www.slashdot.ort /tmp/index.html
五、dnsutil模块:
功能:实现被控主机通用DNS操作
添加minion端的hosts域名解析信息
[[email protected] opt]# salt ‘*‘ dnsutil.hosts_append /etc/hosts 10.1.6.16 www.baidu.com
删除minion端的hosts域名解析信息
[[email protected] opt]# salt ‘*‘ dnsutil.hosts_remove /etc/hosts www.baidu.com
六、file模块:
主机常见的文件操作,包括文件读写、权限、查找、校验
[[email protected] ~]# salt ‘*‘ sys.list_functions file
izwz9f8xrvty50quc2gq50z:
- file.access
- file.append
- file.basename
- file.blockreplace
- file.check_file_meta
- file.check_hash
- file.check_managed
- file.check_managed_changes
- file.check_perms
- file.chgrp
- file.chown
- file.comment
- file.comment_line
- file.contains
- file.contains_glob
- file.contains_regex
- file.contains_regex_multiline
- file.copy
- file.delete_backup
- file.directory_exists
- file.dirname
- file.diskusage
- file.extract_hash
- file.file_exists
- file.find
- file.get_devmm
- file.get_diff
- file.get_gid
- file.get_group
- file.get_hash
- file.get_managed
- file.get_mode
- file.get_selinux_context
- file.get_sum
- file.get_uid
- file.get_user
- file.gid_to_group
- file.grep
- file.group_to_gid
- file.is_blkdev
- file.is_chrdev
- file.is_fifo
- file.is_link
- file.join
- file.lchown
- file.link
- file.list_backup
- file.list_backups
- file.list_backups_dir
- file.lstat
- file.makedirs
- file.makedirs_perms
- file.manage_file
- file.mkdir
- file.mknod
- file.mknod_blkdev
- file.mknod_chrdev
- file.mknod_fifo
- file.move
- file.normpath
- file.open_files
- file.pardir
- file.patch
- file.path_exists_glob
- file.prepend
- file.psed
- file.readdir
- file.readlink
- file.remove
- file.remove_backup
- file.rename
- file.replace
- file.restore_backup
- file.restorecon
- file.rmdir
- file.search
- file.sed
- file.sed_contains
- file.seek_read
- file.seek_write
- file.set_mode
- file.set_selinux_context
- file.source_list
- file.stats
- file.statvfs
- file.symlink
- file.touch
- file.truncate
- file.uid_to_user
- file.uncomment
- file.user_to_uid
- file.write
file.chown
修改所有minion端的/etc/passwd文件的属组、用户权限,等价于chown root:root /etc/passwd
[[email protected] ~]# salt ‘*‘ file.chown /etc/passwd root root
file.copy
将所有minion端的/root/a.sh文件复制到/opt/目录下
[[email protected] ~]# salt ‘*‘ file.copy /root/a.sh /opt/a.sh
将所有minion端的/root/mysql目录复制到/opt目录下
[[email protected] ~]# salt ‘*‘ file.copy /root/mysql /opt/mysql recurse=True
file.directory_exists
检查所有被控主机/etc目录是否存在,存在则返回True,检查文件是否存在使用file.file_exists方法
[[email protected] ~]# salt ‘*‘ file.directory_exists /etc
file.stats
获取所有被控主机/etc/passwd的stats信息
[[email protected] ~]# salt ‘*‘ file.stats /etc/passwd
file.get_mode
获取所有被控主机/etc/passwd的权限mode,如755,644
[[email protected] ~]# salt ‘*‘ file.get_mode /etc/passwd
修改所有被控主机/etc/passwd的权限mode为0644
[[email protected] ~]# salt ‘*‘ file.set_mode /etc/passwd 0644
file.mkdir
在所有被控主机创建/opt/test目录
[[email protected] ~]# salt ‘*‘ file.mkdir /opt/test
file.sed
将所有被控主机/etc/httpd/httpd.conf文件的LogLevel参数的warn值修改为info
[[email protected] ~]# salt ‘*‘ file.sed /etc/httpd/httpd.conf ‘LogLevel warn‘ ‘LogLevel info‘
file.append
给所有被控主机的/tmp/test/test.conf文件追加内容‘maxclient 100’
[[email protected] ~]# salt ‘*‘ file.append /tmp/test/test.conf ‘maxclient 100‘
file.remove
删除所有被控主机的/tmp/foo文件
[[email protected] ~]# salt ‘*‘ file.remove /tmp/foo
file.touch
在所有的minion端的/opt目录下创建c.txt文件
[[email protected] script]# salt ‘*‘ file.touch /opt/c.txt
file.move
将所有minion端/root目录下的a.sh文件移动到/opt目录下
[[email protected] script]# salt ‘*‘ file.move /root/a.sh /opt
file.rename
将所有minion端的/root目录下的a.sh文件重命名为/opt目录下的b.sh
[[email protected] script]# salt ‘*‘ file.rename /root/a.sh /opt/b.sh
file.find
查找所有minion端根目录下所有以.tar.tz结尾的且文件大小大于2m的文件
[[email protected] script]# salt ‘*‘ file.find / type=f name=*.tar.gz size=+2m
查找所有minion端的/var目录下30天以内且大于10m的文件
[[email protected] script]# salt ‘*‘ file.find /var mtime=-30d size=+10m print=path,size,mtime -30d==>30天以内
七、group模块:
[[email protected] script]# salt ‘*‘ sys.list_functions group
izwz9f8xrvty50quc2gq50z:
- group.add
- group.adduser
- group.chgid
- group.delete
- group.deluser
- group.getent
- group.info
- group.members
在所有的minion端创建一个gid为1000的zabbix组
[[email protected] script]# salt ‘*‘ group.add zabbix 1000
在所有的minion端删除zabbix组
[[email protected] script]# salt ‘*‘ group.delete zabbix
八、network模块:
[[email protected] script]# salt ‘*‘ sys.list_functions network
izwz9f8xrvty50quc2gq50z:
- network.active_tcp
- network.arp
- network.connect
- network.default_route
- network.dig
- network.get_bufsize
- network.get_hostname
- network.get_route
- network.hw_addr
- network.hwaddr
- network.in_subnet
- network.interface
- network.interface_ip
- network.interfaces
- network.ip_addrs
- network.ip_addrs6
- network.ip_in_subnet
- network.ipaddrs
- network.ipaddrs6
- network.is_loopback
- network.is_private
- network.mod_bufsize
- network.mod_hostname
- network.netstat
- network.ping
- network.reverse_ip
- network.routes
- network.subnets
- network.traceroute
- network.wol
查询minion端dig、ping、traceroute目录域名信息
[[email protected] script]# salt ‘*‘ network.dig www.sina.com
[[email protected] script]# salt ‘*‘ network.ping www.sina.com
[[email protected] script]# salt ‘*‘ network.traceroute www.sina.com
查询minion端网卡eth0的物理地址
[[email protected] script]# salt ‘*‘ network.hw_addr eth0
查询minion端的ip地址
[[email protected] script]# salt ‘*‘ network.ip_addrs
查询minion端主机的子网信息
[[email protected] script]# salt ‘*‘ network.subnets
九、pkg模块:
[[email protected] script]# salt ‘*‘ sys.list_functions pkg
izwz9f8xrvty50quc2gq50z:
- pkg.available_version
- pkg.check_db
- pkg.clean_metadata
- pkg.del_repo
- pkg.diff
- pkg.download
- pkg.expand_repo_def
- pkg.file_dict
- pkg.file_list
- pkg.get_locked_packages
- pkg.get_repo
- pkg.group_diff
- pkg.group_info
- pkg.group_install
- pkg.group_list
- pkg.hold
- pkg.install
- pkg.latest_version
- pkg.list_holds
- pkg.list_pkgs
- pkg.list_repo_pkgs
- pkg.list_repos
- pkg.list_upgrades
- pkg.mod_repo
- pkg.modified
- pkg.normalize_name
- pkg.owner
- pkg.purge
- pkg.refresh_db
- pkg.remove
- pkg.unhold
- pkg.upgrade
- pkg.upgrade_available
- pkg.verify
- pkg.version
- pkg.version_cmp
在所有的minion端安装httpd服务
[[email protected] script]# salt ‘*‘ pkg.install httpd
卸载所有minion端的httpd服务
[[email protected] script]# salt ‘*‘ pkg.remove httpd
在所有minion端下载httpd软件包
[[email protected] script]# salt ‘*‘ pkg.download httpd
十、service模块:
[[email protected] packages]# salt ‘*‘ sys.list_functions service
izwz9f8xrvty50quc2gq50z:
- service.available
- service.disable
- service.disabled
- service.enable
- service.enabled
- service.execs
- service.force_reload
- service.get_all
- service.get_disabled
- service.get_enabled
- service.mask
- service.missing
- service.reload
- service.restart
- service.show
- service.start
- service.status
- service.stop
- service.systemctl_reload
- service.unmask
在所有的minion端启动httpd服务
[[email protected] packages]# salt ‘*‘ service.start httpd
在所有的minion端关闭httpd服务
[[email protected] packages]# salt ‘*‘ service.stop httpd
在所有的httpd端查看httpd服务的状态
[[email protected] packages]# salt ‘*‘ service.status httpd
在所有的httpd端重新加载一次服务
[[email protected] packages]# salt ‘*‘ service.reload httpd
十一、user模块:
[[email protected] packages]# salt ‘*‘ sys.list_functions user
izwz9f8xrvty50quc2gq50z:
- user.add
- user.chfullname
- user.chgid
- user.chgroups
- user.chhome
- user.chhomephone
- user.chloginclass
- user.chroomnumber
- user.chshell
- user.chuid
- user.chworkphone
- user.delete
- user.get_loginclass
- user.getent
- user.info
- user.list_groups
- user.list_users
- user.rename
[[email protected] packages]# salt ‘*‘ sys.doc user.add
‘user.add:‘
Add a user to the minion
CLI Example:
salt ‘*‘ user.add name <uid> <gid> <groups> <home> <shell>
当然,salt常用的执行模块还有很多,后面会根据实际的操作会继续记录和总结salt模块,敬请期待!
以上是关于saltstack使用指南----常用执行模块的主要内容,如果未能解决你的问题,请参考以下文章