1. 主机规划

salt 版本

  1. [root@salt100 ~]# salt --version
  2. salt 2018.3. (Oxygen)
  3. [root@salt100 ~]# salt-minion --version
  4. salt-minion 2018.3. (Oxygen)

netapi modules

  1. https://docs.saltstack.com/en/latest/ref/netapi/all/index.html

rest_cherrypy

  1. https://docs.saltstack.com/en/latest/ref/netapi/all/salt.netapi.rest_cherrypy.html

文章参考:

saltstack API(一) 安装并测试

参考GitHub

  1. https://github.com/yueyongyue/saltshaker

2. 必要的准备

2.1. 安装部署Python3

  1. [root@salt100 Python-3.7.]# yum install -y libffi-devel # 提前安装
  2. [root@salt100 Python-3.7.]# pwd
  3. /root/software/
  4. [root@salt100 software]# ll
  5. total
  6. -rw-r--r-- root root Apr : Python-3.7..tgz
  7. [root@salt100 software]# tar xf Python-3.7..tgz
  8. [root@salt100 software]# cd Python-3.7./
  9. [root@salt100 Python-3.7.]# ./configure # 配置
  10. [root@salt100 Python-3.7.]# make && make install # 编译 与 安装
  11. # 建立软连接
  12. [root@salt100 ~]# ln -s /usr/local/bin/python3. /usr/bin/python3
  13. [root@salt100 ~]# ll /usr/bin/python3
  14. lrwxrwxrwx root root Apr : /usr/bin/python3 -> /usr/local/bin/python3.

2.2. 安装salt-api

等到配置完毕后才能启动salt-api

  1. [root@salt100 ~]# yum install -y salt-api
  2. [root@salt100 ~]# systemctl enable salt-api.service # 开机自启动

2.3. 新建saltapi用户

  1. [root@salt100 ~]# useradd -M -s /sbin/nologin -u saltapi && echo '' | /usr/bin/passwd --stdin saltapi

2.4. 安装pip和CherryPy

  1. [root@salt100 software]# wget https://bootstrap.pypa.io/get-pip.py
  2. [root@salt100 software]# python3 get-pip.py
  3. [root@salt100 software]# pip -V # 查看pip版本
  4. [root@salt100 software]# pip install CherryPy==3.2. # 注意版本

3. 添加https证书

  1. [root@salt100 certs]# pwd
  2. /etc/pki/tls/certs
  3. [root@salt100 certs]# ll
  4. total
  5. lrwxrwxrwx. root root Nov : ca-bundle.crt -> /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
  6. lrwxrwxrwx. root root Nov : ca-bundle.trust.crt -> /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt
  7. -rwxr-xr-x. root root Apr make-dummy-cert
  8. -rw-r--r--. root root Apr Makefile
  9. -rwxr-xr-x. root root Apr renew-dummy-cert
  10. [root@salt100 certs]# make testcert
  11. umask ; \
  12. /usr/bin/openssl genrsa -aes128 > /etc/pki/tls/private/localhost.key
  13. Generating RSA private key, bit long modulus
  14. .........................................................................+++
  15. ........................+++
  16. e is (0x10001)
  17. Enter pass phrase: # 键入加密短语
  18. Verifying - Enter pass phrase: # 确认加密短语
  19. umask ; \
  20. /usr/bin/openssl req -utf8 -new -key /etc/pki/tls/private/localhost.key -x509 -days -out /etc/pki/tls/certs/localhost.crt
  21. Enter pass phrase for /etc/pki/tls/private/localhost.key: # 再次输入相同的加密短语
  22. You are about to be asked to enter information that will be incorporated
  23. into your certificate request.
  24. What you are about to enter is what is called a Distinguished Name or a DN.
  25. There are quite a few fields but you can leave some blank
  26. For some fields there will be a default value,
  27. If you enter '.', the field will be left blank.
  28. -----
  29. Country Name ( letter code) [XX]:
  30. State or Province Name (full name) []:
  31. Locality Name (eg, city) [Default City]:
  32. Organization Name (eg, company) [Default Company Ltd]:
  33. Organizational Unit Name (eg, section) []:
  34. Common Name (eg, your name or your server's hostname) []:
  35. Email Address []:
  36. [root@salt100 certs]# ll
  37. total
  38. lrwxrwxrwx. root root Nov : ca-bundle.crt -> /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
  39. lrwxrwxrwx. root root Nov : ca-bundle.trust.crt -> /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt
  40. -rw------- root root Mar : localhost.crt
  41. -rwxr-xr-x. root root Apr make-dummy-cert
  42. -rw-r--r--. root root Apr Makefile
  43. -rwxr-xr-x. root root Apr renew-dummy-cert
  44. [root@salt100 certs]# cd /etc/pki/tls/private/ # 进入目录
  45. [root@salt100 private]# ll
  46. total
  47. -rw------- root root Mar : localhost.key
  48. [root@salt100 private]# openssl rsa -in localhost.key -out localhost_nopass.key # 生成无密码秘钥
  49. Enter pass phrase for localhost.key: # 输入和之前一样的加密短语
  50. writing RSA key
  51. [root@salt100 private]# ll
  52. total
  53. -rw------- root root Mar : localhost.key
  54. -rw-r--r-- root root Mar : localhost_nopass.key

4. 添加配置文件

配置文件存放位置

  1. [root@salt100 ~]# vim /etc/salt/master
  2. ##### Primary configuration settings #####
  3. ##########################################
  4. # This configuration file is used to manage the behavior of the Salt Master.
  5. # Values that are commented out but have an empty line after the comment are
  6. # defaults that do not need to be set in the config. If there is no blank line
  7. # after the comment then the value is presented as an example and is not the
  8. # default.
  9.  
  10. # Per default, the master will automatically include all config files
  11. # from master.d/*.conf (master.d is a directory in the same directory
  12. # as the main master config file).
  13. #default_include: master.d/*.conf # 默认配置即可
  14. …………

添加配置文件

  1. [root@salt100 master.d]# pwd
  2. /etc/salt/master.d
  3. [root@salt100 master.d]# ll
  4. total
  5. -rw-r--r-- root root Mar : api.conf
  6. -rw-r--r-- root root Mar : eauth.conf
  7. [root@salt100 master.d]# cat eauth.conf
  8. external_auth:
  9. pam:
  10. saltapi:
  11. - .*
  12. - '@wheel' # to allow access to all wheel modules
  13. - '@runner' # to allow access to all runner modules
  14. - '@jobs' # to allow access to the jobs runner and/or wheel module
  15. [root@salt100 master.d]# cat api.conf
  16. rest_cherrypy:
  17. port:
  18. ssl_crt: /etc/pki/tls/certs/localhost.crt
  19. ssl_key: /etc/pki/tls/private/localhost_nopass.key

5. 重启salt-master

  1. [root@salt100 ~]# systemctl restart salt-master.service # 使配置生效

6. 启动salt-api

  1. [root@salt100 master.d]# systemctl start salt-api.service
  2. [root@salt100 ~]# netstat -lntup | grep 'salt' # 端口查看
  3. tcp 0.0.0.0: 0.0.0.0:* LISTEN /salt-api
  4. tcp 0.0.0.0: 0.0.0.0:* LISTEN /salt-master Z
  5. tcp 0.0.0.0: 0.0.0.0:* LISTEN /salt-master M

7. 使用PAM进行登录验证

  1. [root@salt100 master.d]# curl -k https://172.16.1.100:8000/login \
  2. -H 'Accept: application/x-yaml' \
  3. -d username='saltapi' \
  4. -d password='' \
  5. -d eauth='pam'
  6. return:
  7. - eauth: pam
  8. expire: 1554173316.621825
  9. perms:
  10. - .*
  11. - '@wheel'
  12. - '@runner'
  13. - '@jobs'
  14. start: 1554130116.621824
  15. token: 6bb11fd17c3476cb7d07373113c93faaa9c27f9a
  16. user: saltapi

这个token使我们需要的,方便后文操作

8. 得到指定minion的grains信息

  1. [root@salt100 master.d]# curl -k https://172.16.1.100:8000/minions/salt01 \
  2. -H 'Accept: application/x-yaml' \
  3. -H 'X-Auth-Token: 6bb11fd17c3476cb7d07373113c93faaa9c27f9a'
  4. ## 返回如下信息
  5. return:
  6. - salt01:
  7. SSDs: []
  8. biosreleasedate: //
  9. biosversion: '6.00'
  10. cpu_flags:
  11. ………………

9. 获取minion状态【上下线状态】

  1. ## 备注: client='runner' 代表在master执行 client='local' 代表在minion执行
  2. [root@salt100 ~]# curl -k https://172.16.1.100:8000/ \
  3. -H 'Accept: application/x-yaml' \
  4. -H 'X-Auth-Token: 6bb11fd17c3476cb7d07373113c93faaa9c27f9a' \
  5. -d client='runner' \
  6. -d fun='manage.status'
  7. ## 返回如下信息
  8. return:
  9. - down: []
  10. up:
  11. - salt01
  12. - salt02
  13. - salt03
  14. - salt100

10. test.ping测试

  1. curl -k https://172.16.1.100:8000 \
  2. -H 'Accept: application/x-yaml' \
  3. -H 'X-Auth-Token: 6bb11fd17c3476cb7d07373113c93faaa9c27f9a'\
  4. -d client=local \
  5. -d tgt='*' \
  6. -d fun=test.ping
  7. ## 返回如下信息
  8. return:
  9. - salt01: true
  10. salt02: true
  11. salt03: true
  12. salt100: true

11. 查看jobs信息

在标签1执行

  1. [root@salt100 ~]# salt 'salt01' cmd.run 'whoami && sleep 300'

在标签2执行

  1. [root@salt100 ~]# curl -k https://172.16.1.100:8000/jobs \
  2. > -H 'Accept: application/x-yaml' \
  3. > -H 'X-Auth-Token: 6bb11fd17c3476cb7d07373113c93faaa9c27f9a'
  4. return:
  5. - '':
  6. Arguments: []
  7. Function: test.ping
  8. StartTime: , Apr ::21.862530
  9. Target: '*'
  10. Target-type: glob
  11. User: sudo_yun
  12. '':
  13. Arguments: []
  14. Function: test.ping
  15. StartTime: , Apr ::00.770358
  16. Target: '*'
  17. Target-type: glob
  18. User: saltapi
  19. '':
  20. Arguments:
  21. - whoami && sleep
  22. Function: cmd.run
  23. StartTime: , Apr ::53.892493
  24. Target: salt01
  25. Target-type: glob
  26. User: sudo_yun
  27. '':
  28. Arguments:
  29. - ''
  30. Function: saltutil.find_job
  31. StartTime: , Apr ::58.925816
  32. Target:
  33. - salt01
  34. Target-type: list
  35. User: sudo_yun
  36. '':
  37. Arguments: []
  38. Function: saltutil.running
  39. StartTime: , Apr ::06.139505
  40. Target: '*'
  41. Target-type: glob
  42. User: root
  43. '':
  44. Arguments:
  45. - ''
  46. Function: saltutil.find_job
  47. StartTime: , Apr ::08.955596
  48. Target:
  49. - salt01
  50. Target-type: list
  51. User: sudo_yun
  52. '':
  53. Arguments:
  54. - ''
  55. Function: saltutil.find_job
  56. StartTime: , Apr ::18.970482
  57. Target:
  58. - salt01
  59. Target-type: list
  60. User: sudo_yun
  61. [root@salt100 ~]#
  62. [root@salt100 ~]# curl -k https://172.16.1.100:8000/jobs/20190401232353892493 \
  63. -H 'Accept: application/x-yaml' \
  64. -H 'X-Auth-Token: 6bb11fd17c3476cb7d07373113c93faaa9c27f9a'
  65. ## 返回如下信息
  66. info:
  67. - Arguments:
  68. - whoami && sleep
  69. Function: cmd.run
  70. Minions:
  71. - salt01
  72. Result: {}
  73. StartTime: , Apr ::53.892493
  74. Target: salt01
  75. Target-type: glob
  76. User: sudo_yun
  77. jid: ''
  78. return:
  79. - {}

12. 其他常用操作

  1. # salt 'salt01' state.sls web.apache ,执行 apache.sls # yum 部署httpd
  2. curl -k https://172.16.1.100:8000/ \
  3. -H 'Accept: application/x-yaml' \
  4. -H 'X-Auth-Token: 6bb11fd17c3476cb7d07373113c93faaa9c27f9a' \
  5. -d client=local \
  6. -d tgt='salt01' \
  7. -d fun=state.sls \
  8. -d arg='web.apache'
  9.  
  10. # salt -L 'salt01,salt02,salt03' test.ping
  11. curl -k https://172.16.1.100:8000 \
  12. -H 'Accept: application/x-yaml' \
  13. -H 'X-Auth-Token: 6bb11fd17c3476cb7d07373113c93faaa9c27f9a'\
  14. -d client=local \
  15. -d tgt='salt01,salt02,salt03' \
  16. -d expr_form='list' \
  17. -d fun=test.ping
  18.  
  19. # salt -G 'host:salt01' cmd.run ifconfig
  20. curl -k https://172.16.1.100:8000 \
  21. -H 'Accept: application/x-yaml' \
  22. -H 'X-Auth-Token: 6bb11fd17c3476cb7d07373113c93faaa9c27f9a'\
  23. -d client=local \
  24. -d tgt='host:salt01' \
  25. -d expr_form='grain' \
  26. -d fun=cmd.run \
  27. -d arg='ifconfig'
  28.  
  29. # 以json格式输出
  30. # salt -G 'host:salt01' cmd.run ifconfig
  31. curl -k https://172.16.1.100:8000 \
  32. -H 'Accept: application/json' \
  33. -H 'X-Auth-Token: 6bb11fd17c3476cb7d07373113c93faaa9c27f9a'\
  34. -d client=local \
  35. -d tgt='host:salt01' \
  36. -d expr_form='grain' \
  37. -d fun=cmd.run \
  38. -d arg='ifconfig'

13. 参数解释

  1. client 模块,python处理salt-api的主要模块,‘client interfaces <netapi-clients>’
  2. local : 使用‘LocalClient <salt.client.LocalClient>’ 发送命令给受控主机,等价于saltstack命令行中的'salt'命令
  3. local_async : local不同之处在于,这个模块是用于异步操作的,即在master端执行命令后返回的是一个jobid,任务放在后台运行,通过产看jobid的结果来获取命令的执行结果。
  4. runner : 使用'RunnerClient<salt.runner.RunnerClient>' 调用salt-master上的runner模块,等价于saltstack命令行中的'salt-run'命令
  5. runner_async : 异步执行runner模块
  6. wheel : 使用'WheelClient<salt.wheel.WheelClient>', 调用salt-master上的wheel模块,wheel模块没有在命令行端等价的模块,但它通常管理主机资源,比如文件状态,pillar文件,salt配置文件,以及关键模块<salt.wheel.key>功能类似于命令行中的salt-key
  7. wheel_async : 异步执行wheel模块
  8. 备注:一般情况下local模块,需要tgtarg(数组),kwarg(字典),因为这些值将被发送到minions并用于执行所请求的函数。而runnerwheel都是直接应用于master,不需要这些参数。
  9. tgt : minions
  10. fun : 函数
  11. arg : 参数
  12. expr_form : tgt的匹配规则
  13. 'glob' - Bash glob completion - Default
  14. 'pcre' - Perl style regular expression
  15. 'list' - Python list of hosts
  16. 'grain' - Match based on a grain comparison
  17. 'grain_pcre' - Grain comparison with a regex
  18. 'pillar' - Pillar data comparison
  19. 'nodegroup' - Match on nodegroup
  20. 'range' - Use a Range server for matching
  21. 'compound' - Pass a compound match string

Saltstack_使用指南18_API的更多相关文章

  1. Saltstack_实战指南02_各主机Pillar信息指定

    1. 实战项目GitHub地址 该项目已经放在了GitHub上,地址如下: https://github.com/zhanglianghhh/salt-example-lnmp 2. 主机规划 3. ...

  2. Saltstack_实战指南01_系统规划

    1. 实战项目GitHub地址 之前<Saltstack_使用指南>详细讲解了saltstack的使用.那么从这节开始实战讲解,当然不会再像之前那样详细说明了.只是讲一些系统规划之类的信息 ...

  3. Saltstack_使用指南17_salt-ssh

    1. 主机规划 salt 版本 [root@salt100 ~]# salt --version salt (Oxygen) [root@salt100 ~]# salt-minion --versi ...

  4. Saltstack_使用指南16_syndic

    1. 主机规划 salt 版本 [root@salt100 ~]# salt --version salt (Oxygen) [root@salt100 ~]# salt-minion --versi ...

  5. Saltstack_使用指南12_配置管理-jinja模板

    1. 说明 下文的案例是根据上一篇文章进行的修改.因此请优先读取上一章博文内容<Saltstack_使用指南11_配置管理-状态之间依赖关系> 2. 主机规划 salt 版本 [root@ ...

  6. Saltstack_使用指南07_远程执行-执行模块

    1. 主机规划 远程执行教程文档 https://docs.saltstack.com/en/latest/topics/tutorials/modules.html 所有模块文档 https://d ...

  7. Saltstack_使用指南06_远程执行-指定目标

    1. 主机规划 Targeting Minions文档 https://docs.saltstack.com/en/latest/contents.html 另请参见:自动化运维神器之saltstac ...

  8. Saltstack_使用指南05_数据系统-Pillar

    1. 主机规划 Pillar文档 https://docs.saltstack.com/en/latest/topics/pillar/index.html 注意事项 修改了master或者minio ...

  9. Saltstack_使用指南04_数据系统-Grains

    1. 主机规划 Grains文档 https://docs.saltstack.com/en/latest/topics/grains/index.html 注意事项 修改了master或者minio ...

随机推荐

  1. uiautomatorviewer提示Unable to connect to adb. Check if adb is installed correctly解决方法

    转自:https://www.jianshu.com/p/c8581a70d1bc 解决方案: 1.打开  "E:\android-sdk_r24.4.1-windows\android-s ...

  2. redlock算法及其问题

    背景 redlock算法是为了解决什么问题呢? 在单redis实例实现分布式锁时,可能会出现线程A设置完锁后,master挂掉,slave提升为master,因为异步复制的特性,线程A设置的锁丢失了, ...

  3. Hive初步认识,理解Hive(一)

    Hive初步认识,理解Hive(一) 用了有一段时间的Hive了,之前一直以为hive是个数据库,类似Mysql.Oracle等数据库一样,其实不然. Hive是实现Hadoop 的MapReduce ...

  4. 前端小白webpack学习(三)

    不写不知道,一写发现自己真是罗里吧嗦,尽量改进 之前写了webpack的主要概念和一些使用,今天再记一下webpack的plugins和loaders的使用 7.webpack plugins使用 例 ...

  5. 【5000 Stars 福利】微信接口动态 WebApi 使用说明

    前言 作为中国 C# 开源项目中 Watch/Star/Fork 最高的项目之一,Senparc.Weixin SDK  凝聚了盛派微信团队持续7年的付出,和大量开发者的无私贡献,以及数万开发者的使用 ...

  6. Prometheus学习系列(九)之Prometheus 存储

    前言 本文来自Prometheus官网手册 和 Prometheus简介 存储 Prometheus是一个本地磁盘时间序列数据库,但也可选择与远程存储系统集成,其本地时间序列数据库以自定义格式在磁盘上 ...

  7. Java时区问题

    Java时区相关 时间格式 UTC是以原子时计时,更加精准,适应现代社会的精确计时.不过一般使用不需要精确到秒时,视为等同.GMT是前世界标准时,UTC是现世界标准时.每年格林尼治天文台会发调时信息, ...

  8. ES-结构化查询

    参考: https://es.xiaoleilu.com/054_Query_DSL/55_Request_body_search.html 请求体查询 GET /_search {} 分页 GET ...

  9. SQL Server重建索引与重组索引会更新统计信息吗?

    在SQL Server中重建索引(Rebuild Index)与重组索引(Reorganize Index)会触发统计信息更新吗? 那么我们先来测试.验证一下: 我们以AdventureWorks20 ...

  10. Liu Junqiao:工作中用到的命令以及问题汇总

    工作中用到的命令以及问题汇总 2019-11-29 查看系统运行时间,这个问题是因为我们在阿里云上有个机器,在某一天发现这台机器上有的服务莫名奇妙的停了,然后排查时怀疑机器被重启过用如下如下命令查看了 ...