SaltStackapiの使用法

SaltStackは、REST API形式でapiプロジェクトを公式に提供します。これにより、Saltとサードパーティシステムの統合が容易になります。

次の操作の前提は、salt-masterおよびsalt-apiサービスをインストールしていることです。

1. salt-masterを構成し、rest_cherrypyサービスを有効にします。
ここでは怠け者です。SSLは使用されていないため、SSLは直接無効になっています。オンライン環境ではsslを使用することをお勧めします。

rest_cherrypy:
  port: 8181
  host: 0.0.0.0
  disable_ssl: True

2.pam認証を構成します。

external_auth:
  pam:
    saltuser:
      - .*
      - '@runner'
      - '@wheel'
      - '@jobs'

上記の2つの変更は/ etc / salt / masterファイルにあります

3.認証されたユーザーを作成し、パスワードを設定します

useradd -M -s /sbin/nologin saltuser

4. salt-masterを再起動し、salt-apiを起動します

systemctl restart salt-master
systemctl restart salt-api

5.ソルトリスニングポートを表示します

[root@qd01-stop-saltmaster001 ~]# ss -ltnp
State       Recv-Q Send-Q                                        Local Address:Port                                                       Peer Address:Port
LISTEN      0      30                                                        *:8181                                                                  *:*                   users:(("salt-api",pid=13833,fd=11))
LISTEN      0      1000                                                      *:4505                                                                  *:*                   users:(("salt-master",pid=12235,fd=18))
LISTEN      0      1000                                                      *:4506                                                                  *:*                   users:(("salt-master",pid=12332,fd=32))

6.ログインを確認し、トークン文字列を取得します

[root@saltmaster001 ~]#  curl -sS http://localhost:8181/login -H 'Accept: application/x-yaml' -d username=saltuser -d password=saltuser  -d eauth=pam
return:
- eauth: pam
  expire: 1610484091.7311294
  perms:
  - .*
  - '@runner'
  - '@wheel'
  - '@jobs'
  start: 1610440891.731129
  token: 49d2bedbddf71dd6c4af3c2f5e09797b2cf0d9aa
  user: saltuser

7.apiを介してtest.pingテストを実行します

[root@saltmaster001 ~]# curl -sSk http://localhost:8181 -H 'Accept: application/x-yaml' -H 'X-Auth-Token: 49d2bedbddf71dd6c4af3c2f5e09797b2cf0d9aa'  -d client=local -d tgt='*monitor004*' -d fun=test.ping
return:
- monitor004: true

ご覧のとおり、戻り値はcmdで直接test.pingを実行した場合と同じです。

8.apiを介してcmd.runを実行します

[root@saltmaster001 ~]# curl -sSk http://localhost:8181 -H 'Accept: application/x-yaml' -H 'X-Auth-Token: 49d2bedbddf71dd6c4af3c2f5e09797b2cf0d9aa'  -d client=local -d tgt='qd01-stop-monitor004*' -d fun=cmd.run -d arg='uptime'
return:
- monitor004: ' 16:44:51 up 586 days, 12:47,  0 users,  load
    average: 0.00, 0.00, 0.00'

詳細については、https://docs.saltstack.com/en/latest/ref/netapi/all/salt.netapi.rest_cherrypy.html#a-rest-api-for-saltを参照してください。

おすすめ

転載: blog.51cto.com/1648324/2588772