saltstack(三) --- salt-httpapi

以下操作均在master上操作

1. 安裝api

netapi modules(httpapi)有三種,分別是rest_cherrypy、rest_tornado、rest_wsig,接下來要講的是rest_cherrypy
doc:https://docs.saltstack.com/en/latest/ref/netapi/all/salt.netapi.rest_cherrypy.html
yum install -y salt-api
pip install cherrypy

?

2. 生成證書

[root@localhost ~]# yum install -y openssl
[root@localhost ~]# cd /etc/salt/
[root@localhost salt]# mkdir keycrt
[root@localhost salt]# cd keycrt/
[root@localhost keycrt]# openssl genrsa -out key.pem 4096
[root@localhost keycrt]# openssl req -new -x509 -key key.pem -out cert.pem -days 1826

?

3. 配置salt-api的配置文件

[root@localhost keycrt]# cd /etc/salt/master.d/
[root@localhost master.d]# cat api.conf
rest_cherrypy:                                            //還有好多可以寫的參數,參考docport: 8000ssl_crt: /etc/salt/keycrt/cert.pemssl_key: /etc/salt/keycrt/key.pem------------------------------------------------------>
[root@localhost master.d]# cat eauth.conf
external_auth:pam:saltapi:                                           //認證的用戶名- .*- '@wheel'- '@runner'----------------------------------------------------->      //創建用戶名
[root@localhost master.d]# useradd -M -s /sbin/nologin saltapi
[root@localhost master.d]# echo "saltapi" |passwd saltapi --stdin

?

4. 啟動api

[root@localhost master.d]# systemctl restart salt-master
[root@localhost master.d]# systemctl start salt-api
[root@localhost master.d]# netstat -lnp |grep 8000
tcp        0      0 0.0.0.0:8000            0.0.0.0:*               LISTEN      36821/python

?

5. 獲取token

# Time (in seconds) for a newly generated token to live. Default: 12 hours
#token_expire: 43200
#token有效期為12個小時,可以在master配置文件更改

?

5.1 https方式

[root@localhost master.d]# curl -X POST -k https://192.168.123.106:8000/login -d username='saltapi' -d password='saltapi' -d eauth='pam' |python -m json.tool% Total    % Received % Xferd  Average Speed   Time    Time     Time  CurrentDload  Upload   Total   Spent    Left  Speed
100   240  100   197  100    43   1450    316 --:--:-- --:--:-- --:--:--  1459
{"return": [{"eauth": "pam","expire": 1517772071.637639,"perms": [".*","@wheel","@runner"],"start": 1517728871.637638,"token": "55d8ccc1ab3f8ba069b6fbe21cae1686c4d5823e","user": "saltapi"}]
}

?

通過工具postman提交post請求,基本上是圖片,懶得貼了

?

5.2 http方式

顯式禁用證書驗證,不需要生成證書

[root@localhost master.d]# cat api.conf     //更改配置文件
rest_cherrypy:port: 8000disable_ssl: True
#  ssl_crt: /etc/salt/keycrt/cert.pem
#  ssl_key: /etc/salt/keycrt/key.pem-------------------------------------------------------------->
[root@localhost master.d]# systemctl restart salt-master
[root@localhost master.d]# systemctl restart salt-api------------------------------------------------------------->
[root@localhost master.d]# curl -X POST -k http://192.168.123.106:8000/login -d username='saltapi' -d password='saltapi' -d eauth='pam' |python -m json.tool% Total    % Received % Xferd  Average Speed   Time    Time     Time  CurrentDload  Upload   Total   Spent    Left  Speed
100   240  100   197  100    43   2594    566 --:--:-- --:--:-- --:--:--  2626
{"return": [{"eauth": "pam","expire": 1517774657.797506,"perms": [".*","@wheel","@runner"],"start": 1517731457.797506,"token": "62dbdca57f854b624802d44601426808c8855b3c","user": "saltapi"}]
}

?

6. 執行模塊

[root@localhost master.d]# curl -X POST -k https://192.168.123.106:8000/login -d username='saltapi' -d password='saltapi' -d eauth='pam' |python -m json.tool% Total    % Received % Xferd  Average Speed   Time    Time     Time  CurrentDload  Upload   Total   Spent    Left  Speed
100   240  100   197  100    43   1281    279 --:--:-- --:--:-- --:--:--  1287
{"return": [{"eauth": "pam","expire": 1517775225.766237,"perms": [".*","@wheel","@runner"],"start": 1517732025.766237,"token": "3643e2f1b04e3280e1aa9cffec9eaaab98feff13","user": "saltapi"}]
}[root@localhost master.d]# curl -X POST -k https://192.168.123.106:8000 -H 'Accept: application/x-yaml' -H 'X-Auth-Token: 3643e2f1b04e3280e1aa9cffec9eaaab98feff13' -d client='local' -d tgt='*' -d fun='test.ping'
return:
- 192.168.123.107: true[root@localhost master.d]# curl -X POST -k https://192.168.123.106:8000 -H 'Accept: application/x-yaml' -H 'X-Auth-Token: 3643e2f1b04e3280e1aa9cffec9eaaab98feff13' -d client='local' -d tgt='*' -d fun='cmd.run' -d arg='uptime'
return:
- 192.168.123.107: ' 16:22:24 up 1 day,  1:40,  2 users,  load average: 0.00, 0.01,0.05'

?

7. 執行runner

[root@localhost master.d]# curl -X POST -k https://192.168.123.106:8000 -H 'Accept: application/x-yaml' -H 'X-Auth-Token: 3643e2f1b04e3280e1aa9cffec9eaaab98feff13' -d client='runner' -d fun='manage.status'     
return:
- down: []up:- 192.168.123.107

?

_modules:

[root@bogon _modules]# cat jd.py
#!/usr/bin/env pythonimport codecsdef hello(key ,value, param):return {'key': key, 'value': value, 'param': param}def world(name):return {'name': name}def meminfo():with codecs.open('/proc/meminfo') as fd:for line in fd:if line.startswith('MemAvailable'):result = str(int(line.split()[1])/1024.0) + 'M'return {'MemAvailable': result}

?

_runner:

[root@bogon _runner]# cat testparam.py
#!/usr/bin/env pythonimport time
import salt.clientdef get(minion, function, params):__opts__ = salt.config.client_config('/etc/salt/master')conf_file = __opts__['conf_file']localclient = salt.client.LocalClient(conf_file)jid = localclient.cmd_async(minion, function, params.split(','))wait_time = 0sleep_interval = 1while wait_time < __opts__['timeout']:print('wait {0} seconds'.format(wait_time))result = localclient.get_cache_returns(jid)if result:print(type(result))return resulttime.sleep(sleep_interval)wait_time += sleep_intervaldef get_no_param(minion, function):__opts__ = salt.config.client_config('/etc/salt/master')conf_file = __opts__['conf_file']localclient = salt.client.LocalClient(conf_file)jid = localclient.cmd_async(minion, function)wait_time = 0sleep_interval = 1while wait_time < __opts__['timeout']:print('wait {0} seconds'.format(wait_time))result = localclient.get_cache_returns(jid)if result:print(type(result))return resulttime.sleep(sleep_interval)wait_time += sleep_interval

?

8. 判斷token是否過期

攜帶token訪問https://192.168.123.106/stats,如果狀態碼為200,token沒過期,狀態碼為401,token過期

轉載于:https://www.cnblogs.com/tobeone/p/8434946.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/281234.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/281234.shtml
英文地址,請注明出處:http://en.pswp.cn/news/281234.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

c++實現二叉搜索樹

自己實現了一下二叉搜索樹的數據結構。記錄一下&#xff1a; #include <iostream>using namespace std;struct TreeNode{int val;TreeNode *left;TreeNode *right;TreeNode(int value) { valvalue; leftNULL; rightNULL; } };class SearchTree{public:SearchTree();~Sear…

一款自用的翻譯小工具,開源了

一款自用的翻譯小工具&#xff0c;開源了TranslationTool作者&#xff1a;WPFDevelopersOrg - 唐宋元明清|驚鏵原文鏈接&#xff1a;https://github.com/Kybs0/TranslationTool此項目使用WPF MVVM開發。框架使用大于等于.NET461。Visual Studio 2019。最初是支持以下&#xff1…

JS使用按位異或方式加密字符串

按位異或加密字符串&#xff0c;字符串加解密都是該函數 缺陷是加密密鑰使用的字符最好不要出現需要加密的字符串中的字符&#xff0c;一旦出現原字符與加密字符一樣額情況&#xff0c;異或結果為0&#xff0c;導致不能還原字符串&#xff0c;可以考慮更改算法避免這種情況 im…

SCSS 實用知識匯總

1、變量聲明 $nav-color: #F90; nav {//$width 變量的作用域僅限于{}內$width: 100px;width: $width;color: $nav-color; }.a {//報錯&#xff0c;$width未定義width: $width; } 2、父選擇器& scss代碼&#xff1a; article a {color: blue;&:hover { color: red } } 編…

【ELK集群+MQ】通用部署方案以及快速實現MQ發布訂閱服務功能

前言&#xff1a;大概一年多前寫過一個部署ELK系列的博客文章&#xff0c;前不久剛好在部署一個ELK的解決方案&#xff0c;我順便就把一些基礎的部分拎出來&#xff0c;再整合成一期文章。大概內容包括&#xff1a;搭建ELK集群&#xff0c;以及寫一個簡單的MQ服務。如果需要看一…

python容錯

#try: except: else: #為什么叫容錯呢&#xff0c;先說說錯誤&#xff0c;這里說的錯誤并不是因為馬虎或者什么原因在腳本中留下的bug&#xff0c;這個不能容掉&#xff0c;所謂容掉就是略過這個錯誤&#xff0c;要在測試時候發現并修正&#xff0c;需要容錯的錯誤是在腳本執行…

git stash參數介紹

git stash 用于暫存工作區未提交的內容&#xff0c;便于在同時開發多個分支需要切換時保存當前分支進度。 list 語法 git stash list [<options>] &#xff0c;與git log功能類似&#xff0c;列出儲藏列表&#xff0c;options 參數可以參考git log的參數 show 語法 git …

多語言報表的改動方法

在定義上傳RTF模板的時候&#xff0c;會有一個是否可翻譯的選項&#xff0c;選擇之后。就能夠上傳xlf文件作為翻譯內容。 對于已經存在的多語言類型報表&#xff0c;稍作改動之后再上傳&#xff0c;可能會出現下面現象&#xff1a; 進程出現了“未完畢”的提示 想要改動非常eas…

自定義Cell的流程

1、.h文件 // // 文 件 名:CHBackupGateWayCell.h // // 版權所有:Copyright © 2018 lelight. All rights reserved. // 創 建 者:lelight // 創建日期:2018/12/19. // 文檔說明: // 修 改 人: // 修改日期: //#import <UIKit/UIKit.h>NS_ASSUME_NONNULL_BEGINclass…

JS實時監聽DOM元素變化 - MutationObserver

使用 MutationObserver API實時監聽DOM元素變化 創建 MutationObserver 實列&#xff0c;接受一個用于監聽到DOM元素變化的回調函數 const handleListenChange (mutationsList, observer) > {console.log(mutationsList, observer) } const mutationObserver new Mutati…

LightOJ - 1027 A Dangerous Maze —— 期望

題目鏈接&#xff1a;https://vjudge.net/problem/LightOJ-1027 1027 - A Dangerous MazePDF (English)StatisticsForumTime Limit: 2 second(s)Memory Limit: 32 MBYou are in a maze; seeing n doors in front of you in beginning. You can choose any door you like. The p…

MASA MAUI Plugin (六)集成個推,實現本地消息推送[Android] 篇

背景MAUI的出現&#xff0c;賦予了廣大.Net開發者開發多平臺應用的能力&#xff0c;MAUI 是Xamarin.Forms演變而來&#xff0c;但是相比Xamarin性能更好&#xff0c;可擴展性更強&#xff0c;結構更簡單。但是MAUI對于平臺相關的實現并不完整。所以MASA團隊開展了一個實驗性項目…

第八天

配置文件 Vi /etc/fstab /dev/vg01/lv01 /dir01 ext4 defaults mount -a 掃描 使用交換空間 1.創建分區 2.mkswap /dev/sda創建交換分區 3.swapon /dev/sda啟用交換分區 Linux系統啟動過程 1、引導程序 BIOS自檢 &#xff08;硬件自檢&#xff09; 2、G…

iOS 通知中心(NSNotificationCenter)

NSNotificationCenter 在這里第一步和第二步的順序可以互換&#xff0c;一般樓主我喜歡先在需要發送消息的頁面發送消息&#xff0c;然后再在需要監聽的頁面注冊監聽。要注意的是不管是通知中心還是KVO都需要在頁面銷毀之前移除監聽。 注冊觀察者/*** 觀察者注冊消息通知*…

vue-router和react-router嵌套路由layout配置方案的區別

最近在學習react&#xff0c;在路由這一塊有點看不懂&#xff0c;第一感覺是靈活性很大&#xff0c;想怎么來就怎么來&#xff0c;但問題也來了&#xff0c;稍微復雜一點就GG了&#xff0c;不如vue的傻瓜式配置來的方便。 先說一下vue的路由配置方式&#xff0c;目錄結構如下&…

微軟加更.NET7中文手冊,都有哪些新亮點?

11月8號發布了.NET7&#xff0c;從底層性能改進&#xff0c;到上層API升級&#xff0c;讓.NET7綜合性能再度提升&#xff01;同時發布了最新的C#11&#xff0c;也帶來了很多小驚喜。如何快捷學習最新的.NET7和C#11&#xff1f;答案只有一個&#xff0c;微軟官方中文文檔&#x…

jquery對json的各種遍歷

http://caibaojian.com/jquery-each-json.html轉載于:https://www.cnblogs.com/pxffly/p/8442448.html

中級工程師之路

前言&#xff1a;之前在問答中問了一個問題 畢業半年感覺沒什么進步該怎么辦&#xff1f; 這個問題一直讓我感覺比計較焦慮。于是在一個關于面試經驗的博客中找到了一些靈感。就是通過問題進行學習&#xff0c;對自身的知識體系進行整理和補充。以問題作為切入點&#xff0c;不…

Vue在渲染函數createELement和JSX中使用插槽slot

Vue對于插槽有兩個專門的APIvm.$slots和vm.$scopedSlots&#xff0c;分別是普通插槽和作用域插槽&#xff0c;使用JSX語法或渲染函數的時候&#xff0c;定義插槽將使用上述兩個API。 渲染函數createElement 普通插槽和作用域插槽在定義上相差不大&#xff0c;但是在使用方法上…

.NET Conf China 2022 第一批講師陣容大揭秘!整個期待了!

目光看過來2022年12月3-4日一場社區性質的國內規模最大的線上線下.NET Conf 2022技術大會即將盛大開幕目前大會正緊鑼密鼓地進行中第一批大咖講師及主題已確定小編迫不及待想和大家分享分享嘉賓很大咖分享內容很硬核戳戳小手期待ing孔令磊維宏股份 首席架構師 十多年數控領域研…