Saltstack_使用指南07_远程执行-执行模块
1. 主机规划
服务器名称 | 操作系统版本 | 内网IP | 外网IP(模拟) | Hostname | 部署模块 |
---|---|---|---|---|---|
salt100 | CentOS7.5 | 172.16.1.100 | 10.0.0.100 | salt100 | salt-master、salt-minion |
salt01 | CentOS7.5 | 172.16.1.11 | 10.0.0.11 | salt01 | salt-minion |
salt02 | CentOS7.5 | 172.16.1.12 | 10.0.0.12 | salt02 | salt-minion |
salt03 | CentOS7.5 | 172.16.1.13 | 10.0.0.13 | salt03 | salt-minion |
远程执行教程文档
https://docs.saltstack.com/en/latest/topics/tutorials/modules.html
所有模块文档
https://docs.saltstack.com/en/latest/ref/modules/all/index.html#all-salt-modules
模块在机器上存在的位置
[[email protected] modules]# pwd
/usr/lib/python2.7/site-packages/salt/modules
[[email protected] modules]# ll network.py
-rw-r--r-- 1 root root 56636 Oct 8 23:56 network.py
注意事项
修改了master或者minion的配置文件,那么必须重启对应的服务。
2. 使用格式
# salt调用包括三个主要组成部分:
salt '<target>' <function> [arguments]
2.1. 指定目标
上一篇文章详细说过,这里简单说下
# target组件允许你过滤minion运行以下功能。默认的是minion ID,如下:
salt '*' test.ping
salt '*.example.org' test.ping
# 也可以使用grains:
salt -G 'os:Ubuntu' test.ping
# 也可以使用正则表达式:
salt -E 'virtmach[0-9]' test.ping
# 也可以使用列表:
salt -L 'foo,bar,baz,quo' test.ping
# 或者多个目标类型可以使用复合指定:
salt -C '[email protected]:Ubuntu and webser* or [email protected]*' test.ping
2.2. 指定执行模块
# function是由模块提供的一些功能。Salt中有大量可用functions。列出所有可用的functions如下:
salt '*' sys.doc
# 一些例子如下:
# 显示当前所有可用的 minion
salt '*' test.ping
# 运行随意的shell命令:
salt '*' cmd.run 'uname -a'
2.3. 执行参数
# 使用空格作为分隔符
salt '*' cmd.exec_code python 'import sys; print sys.version'
# 可选的,关键字参数也被支持:
salt '*' pip.install salt timeout=5 upgrade=True
# 改格式为: kwarg=argument
3. 使用示例
3.1. network
https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.network.html#module-salt.modules.network
3.2. service
https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.rh_service.html#module-salt.modules.rh_service
3.3. cp【可使用Salt-cp代替】
https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.cp.html#module-salt.modules.cp
另请参考:saltstack cp模块使用
基本解释:
salt根目录:在master中 file_roots 定义的路径
例如:假设在master中有如下定义:
file_roots:
base:
- /srv/salt
那么:salt://vimrc 指的实际路径是:/srv/salt/vimrc,这样做的好处是,可以满足state系统中环境的概念。
示例:
[[email protected] other]# pwd
/srv/salt/other
[[email protected] other]# ll /srv/salt/other/hosts
-rw-r--r-- 1 root root 276 Nov 25 17:59 /srv/salt/other/hosts
[[email protected] other]# salt -L 'salt01,salt02' test.ping
salt01:
True
salt02:
True
[[email protected] other]# salt -L 'salt01,salt02' cp.get_file salt://other/hosts /tmp/hehe # 使用cp模块,拷贝到指定 minion
salt-cp使用
salt-cp -L 'salt01,salt02' /etc/hosts /tmp/kkk # 使用 salt-cp 拷贝【建议使用,方便一些】
3.4. state
https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.state.html#module-salt.modules.state