saltstack的api使用方法

saltstack的api

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

  • salt-api的安装

  • 安装
    yum install –y salt-api

  • 加载master的配置文件

import  salt.config
master_opts = salt.config.client_config(‘/etc/salt/master’)
print(‘master_opts’) 
  • 加载minion的配置文件
import salt.config
Minion_opts = salt.config.minion_config(‘/etc/salt/minion’)
  • 在master上执行各种模块:
import salt.client               
local = salt.client.LocalClient(‘/etc/salt/minion’)
print(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('*', ['test.ping', 'cmd.run'], [[], ['whoami']])
{'192.168.48.129': {'test.ping': True, 'cmd.run': 'root'}}
  • 如果对于执行时间过长,没法直接返回的,我们就可以通过异步执行的形式进行返回cmd_async和get_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 = 1while 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
  • 客户端执行salt命令:
>>> import salt.config
>>> import salt.client
>>> caller = salt.client.Caller(‘/etc/salt/minion’)
>>> caller.cmd("test.ping")
True
  • 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']}
  • httpapi
  • 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 4096
#openssl req –new –x 509 –key key.pem –out cert.pem –days 1826
配置用户以及权限:
首先需要在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
  • 配置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'
  • 创建用户
useradd -M -s /sbin/nologin/ saltapi
echo “saltapi” |passwd saltapi --stdin

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

  • 启动salt-api
systemctl restart salt-api
netstat –anp |grep 8000
  • 使用http协议,具体的配置如下:
cat api.conf 
rest_cherrypy:
  port: 8000
  disable_ssl: True
  #ssl_crt: /etc/salt/keycrt/cert.pem
  #ssl_key: /etc/salt/keycrt/key.pem


注意以上的: disable_ssl: True
然后把ssl秘钥和公钥注释掉,eauth.conf不发生变化
  • 获取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"
        }
    ]
}
  • -
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值