Saltstack的api && httpapi

一、Saltstack的api

Salt-api有两种方式,一种是函数的形式,有人家定义好的函数,我们可以直接调用,直接写python代码调用函数或者类就可以了。 第二种形式是salt-api有封装好的http协议的,我们需要启动一个服务端。

安装:

yum install y salt-api

1、加载master的配置文件

import  salt.config
master_opts = salt.config.client_config(/etc/salt/master)
print(master_opts)

2. 加载minion的配置文件

import salt.config
Minion_opts = salt.config.minion_config(/etc/salt/minion)

3. 在master上执行各种模块:

>>> import salt.client               
>>> local = salt.client.LocalClient(/etc/salt/minion)#不加也可以,加上最好
>>> local.cmd('*', "test.ping")      
{'192.168.48.129': True}
>> local.cmd('*', "cmd.run", "w")
{'192.168.48.129': ' 12:17:38 up  5:58,  1 user,  load average: 0.00, 0.01, 0.05\nUSER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT\nroot     pts/0    192.168.48.1     11:14    2:50   0.89s  0.89s python'}

如果一次要执行多个模块

local.cmd('*', 'cmd.run', ['ifconfig']) ##执行一个


local.cmd('*', ['test.ping', 'cmd.run'], [[], [ifconfig]])  ## test.ping 的参数为空, cmd.run 的参数为ifconfig
{'192.168.48.129': {'test.ping': True, 'cmd.run': 'root'}}

自定义的模块:

>>> local.cmd('*', "jd.meminfo", "")
{'192.168.48.129': {'meminfo': '0.31'}}

4. 如果对于执行时间过长,没法直接返回的,我们就可以通过异步执行的形式进行返回

cmd_asyncget_cache_returns(jid)
一下代码只能只能在master上执行,而且是只能在master上才可以使用

__opts__ = salt.config.minion_config('/etc/salt/minion')
conf_file = __opts__['conf_file']
client = salt.client.LocalClient(conf_file)
jid = client.cmd_async(minion, function, params)
wait_time = 0
sleep_interval = 1
while wait_time < timeout:
    print('wait {0} seconds'.format(sleep_interval))
    time.sleep(sleep_interval)
    returns = client.get_cache_returns(jid)
    if returns:
        return returns
    wait_time += sleep_interval 

5. 客户端执行salt命令:

>>> import salt.config
>>> import salt.client
>>> caller = salt.client.Caller(/etc/salt/minion)
>>> caller.cmd("test.ping")
True

类似shell命令的salt-call,可以在minion端执行salt的命令,测试连通性什么的

  1. Master端执行salt-run
>>> import salt.config                    
>>> import salt.runner
>>> __opts__ = salt.config.client_config('/etc/salt/master')
>>> runermaster = salt.runner.RunnerClient(__opts__) 
>>> runnerMaster.cmd('jobs.list_jobs', [])
查看所有的
>>>runnerMaster.cmd('manage.status')        
down:
up:
    - 192.168.48.129
{'down': [], 'up': ['192.168.48.129']}

二、Saltstack的httpapi

1、在官网跟新yum源信息:

2、yum安装

yum install -y gcc make python-devel libffi-devel salt-api openssl
Pip install cherrypy

生成证书:

#cd /etc/salt
#mkdir keycrt
#cd keycrt
#openssl genrsa out key.pem 4098
#openssl req new x 509 key key.pem out cert.pem days 1826
  1. 配置用户以及权限:

首先需要在master上检查配置文件:

default_include: master.d/*.conf
 interface: 192.168.48.128
 conf_file: /etc/salt/master
 pki_dir: /etc/salt/pki/master
 auto_accept: True
 file_roots:
    base:
      - /srv/salt/
 log_file: /var/log/salt/master
 log_level_logfile: debug

b. 配置salt-api的配置文件:

[root@localhost master.d]# cd /etc/salt/master.d/
[root@localhost master.d]# ls
api.conf  eauth.conf
[root@localhost master.d]# 
[root@localhost master.d]# cat api.conf 
rest_cherrypy:
  port: 8000
  ssl_crt: /etc/salt/keycrt/cert.pem
  ssl_key: /etc/salt/keycrt/key.pem
[root@localhost master.d]# cat eauth.conf 
external_auth:
  pam:
    saltapi:
      - .*
      - '@wheel'
      - '@runner'

创建用户: c. 创建用户

useradd -M -s /sbin/nologin/ saltapi
echo saltapi |passwd saltapi --stdin

注意这个应用名和上面的saltapi要对应一致。

启动salt-api

systemctl restart salt-api
netstat anp |grep 8000

获取tocken:

http协议的:

curl -X POST -k http://192.168.48.134:8000/login -d username='saltapi' -d password='saltapi' -d eauth='pam' |python -mjson.tool
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   240  100   197  100    43     45      9  0:00:04  0:00:04 --:--:--    45
{
    "return": [
        {
            "eauth": "pam",
            "expire": 1517235285.554001,
            "perms": [
                ".*",
                "@wheel",
                "@runner"
            ],
            "start": 1517192085.554001,
            "token": "105ee1f28109d67855ce7898e75e173a678f5174",
            "user": "saltapi"
        }
    ]
}

获取tocken:

https协议的:

[root@localhost master.d]# curl -X POST -k https://192.168.48.131:8000/login -d username='saltapi' -d password='saltapi' -d eauth='pam' |python -mjson.tool
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   240  100   197  100    43    908    198 --:--:-- --:--:-- --:--:--   912
{
    "return": [
        {
            "eauth": "pam",
            "expire": 1517235312.537542,
            "perms": [
                ".*",
                "@wheel",
                "@runner"
            ],
            "start": 1517192112.537541,
            "token": "bd5922438e9ae10db039816728c2b86f9462a0bb",
            "user": "saltapi"
        }
    ]
}

通过postman来获取tocken:

Headers里面是用来存放headers的信息的
Body里面来存放数据的,常用的data数据就是x-www-form-urlencoded

form-data 是用来存放页面form表单数据的

只要salt-api不重启,tocken就不会过期,salt-api重启以后,tocken就会过期

通过postman来获取执行module:

通过curl来获取执行module:

root@ling-virtual-machine:/etc/salt/master.d# curl -k http://192.168.48.134:8000 -H "Accept: application/x-yaml" -H "X-Auth-Token: ec623ed62de7dd62cfdadb94ad0044b7f46c9549" -d client='local' -d tgt='*' -d fun='test.ping'
return:
192.168.48.129: true

运行runner

root@ling-virtual-machine:/etc/salt/master.d# curl -k http://192.168.48.134:8000 -H "Accept: application/x-yaml" -H "X-Auth-Token: ec623ed62de7dd62cfdadb94ad0044b7f46c9549" -d client='runner' -d fun='manage.status'            
return:
- down: []
  up:
  - 192.168.48.129

你可能感兴趣的:(Saltstack的api && httpapi)