saltstack(二)

saltstack-api

salt-api使用有2种方式,一种是函数形式,也就是通过python代码直接调用已经定义好的函数,第二种是封装好的http协议,启动一个服务端直接通过http访问。

安装

yum install -y salt-api

函数形式调用

使用salt '*' sys.list_modules可以查看所有可用的模块。

加载master配置文件
1
2
3
import  salt.config
master_opts = salt.config.client_config(‘/etc/salt/master’)
print(master_opts)
加载minion配置文件
1
2
3
import salt.config
minion_opts = salt.config.minion_config(‘/etc/salt/minion’)
print(minion_opts)
master执行模块
1
2
3
4
import salt.client
client = salt.client.LocalClient()
ret = client.cmd('*', 'test.ping')
print ret #输出结果:{'slave': True}

cmd中命令格式'<操作目标>','<模块>','[参数]'。 例:'*','cmd.run',['df -h']
对于其他模块的API调用,只需要改变cmd命令即可。

  • cmd模块
    client.cmd('*', 'cmd.run', ['free -m'])
    也可以一次执行多个模块或者传递多个参数
    client.cmd('*', ['cmd.run'],[['df -h'], ['whoami']])
    client.cmd('*', ['test.ping', 'cmd.run'],[[], ['whoami']])
  • cp模块
    client.cmd('*','cp.get_file',['salt://script/test.py','/minion/test.py'])
  • cron模块
    client.cmd('slave','cron.set_job',['root','*','*','*','*',1,'/usr/local/weekly'])
  • dnsutil模块
    clietn.cmd('slave','dnsutil.hosts_append',['/etc/hosts','127.0.0.1','slave','slave2'])
  • file模块
    client.cmd('*','file.remove',['/tmp/foo'])
  • iptables模块
    client.cmd('*','iptables.append',['filter','INPUT','rule=\'-p tcp --sport 80 -j ACCEPT\''])
  • network模块
    client.cmd('slave','network.ip_addrs')
  • pkg模块
    client.cmd('*','pkg.remove',['php'])
  • service模块
    client.cmd('*','service.stop',['nginx'])
    异步执行
    对于执行时间过长,没法直接返回的,可以通过异步执行的形式进行返回。
    使用cmd_asyncget_cache_returns(jid)实现。
    1
    2
    3
    4
    import salt.client
    local = salt.client.LocalClient('/etc/salt/master')
    jid = local.cmd_async('*', 'network.ip_addrs')
    local.get_cache_returns(jid) #输出结果:{'slave': {'ret': ['192.168.145.130']}}
minion端执行salt命令
1
2
3
import salt.client
caller = salt.client.Caller()
caller.cmd('test.ping')

其执行类似于sal-call,可以在minion端执行命令。

salt.runner使用

在master端

1
2
3
4
5
import salt.runner
import salt.config
__opts__ = salt.config.client_config('/etc/salt/master')
runnermaster = salt.runner.RunnerClient(__opts__)
runnermaster.cmd('manage.status')

grains

在master端

1
2
3
4
5
6
7
8
>>> import salt.config
>>> import salt.loader
>>> __opts__ = salt.config.minion_config("/etc/salt/minion")
>>> __grains__ = salt.loader.grains(__opts__)
>>> __grains__['id']
'master'
>>> __grains__['server_id']
685245236

1
2
3
4
5
6
7
8
9
import salt.config
import salt.loader

__opts__ = salt.config.minion_config('/etc/salt/minion')
__grains__ = salt.loader.grains(__opts__)
__opts__['grains'] = __grains__
__utils__ = salt.loader.utils(__opts__)
__salt__ = salt.loader.minion_mods(__opts__, utils=__utils__) #交互模式无法使用,原因见下面
__salt__['test.ping']()
内置变量

在python交互环境中,下面这些内置变量是不生效,只要在自定义的模块中使用才生效。
__opts__ 配置文件
__salt__ 执行modules

1
2
__salt__['cmd.run']('fdisk -l')        ##__salt__[模块](参数)
__salt__['network.ip_addrs']()

说明:salt是个字典,它里面装了minion上所有的modules,salt的key是一个个的模块名称,value则是模块里面的一个个函数
__pillar__ pillar
__grains__ grains
__context__

1
2
if not 'cp.fileclient' in __context__:
__context__['cp.fileclient'] = salt.fileclient.get_file_client(__opts__)

http api

salt-api使用cherrypy框架和salt接口实现的。

安装

yum -y install salt-api

配置

创建用于salt-api的用户

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

master配置文件

1
2
vi /etc/salt/master +12
default_include: master.d/*.conf #注释取消

创建配置文件

1
2
mkdir -p /etc/salt/master.d/
touch api.conf eauth.conf

更新配置文件内容
cat /etc/salt/master.d/api.conf

1
2
3
4
5
rest_cherrypy:
port: 8000
disable_ssl: True
#ssl_crt: /etc/salt/keycrt/cert.pem
#ssl_key: /etc/salt/keycrt/key.pem

cat /etc/salt/master.d/eauth.conf

1
2
3
4
5
6
external_auth:
pam:
saltapi: #此处为前面创建的用户
- .*
- '@wheel'
- '@runner'

启动salt-api
systemctl start salt-api
启动之后可以看到8000端口已经在监听状态,且salt-api服务已启动

1
2
3
4
5
6
[root@master master.d]# netstat -lntp|grep 8000
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 24008/python
[root@master master.d]# ps aux|grep salt-api
root 23998 0.0 2.8 295044 28012 ? Ss 19:22 0:00 /usr/bin/python /usr/bin/salt-api
root 24008 0.3 3.1 1673212 31092 ? Sl 19:22 0:01 /usr/bin/python /usr/bin/salt-api
root 24231 0.0 0.0 112664 972 pts/0 R+ 19:28 0:00 grep --color=auto salt-api

上面配置文件使用的是http协议,若要使用https协议,则需要生成证书

1
2
3
4
5
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

同时更新/etc/salt/master.d/api.conf内容即可。
cat /etc/salt/master.d/api.conf

1
2
3
4
5
rest_cherrypy:
port: 8000
#disable_ssl: True
ssl_crt: /etc/salt/keycrt/cert.pem
ssl_key: /etc/salt/keycrt/key.pem

调用

登录,获取token
说明:saltapi重启token会发生变化,不重启则不会变化。

1
curl -k http://192.168.145.129:8000/login -H "Accept: application/x-yaml"  -d username='saltapi'  -d password='saltapi' -d eauth='pam'

执行结果如下

1
2
3
4
5
6
7
8
9
10
return:
- eauth: pam
expire: 1530662531.464414
perms:
- .*
- '@wheel'
- '@runner'
start: 1530619331.464413
token: 25a29f8fd30c611f399370ab1be8a0b4f83d1e06
user: saltapi

使用获取的token查询硬盘使用情况

1
2
3
4
5
6
curl -k http://192.168.145.129:8000 \
-H "Accept: application/x-yaml" \
-H "X-Auth-Token: 25a29f8fd30c611f399370ab1be8a0b4f83d1e06" \
-d client=local \
-d tgt='*' \
-d fun='status.diskusage'

模块调用

1
2
3
4
5
6
7
curl -k http://192.168.145.129:8000 \
-H "Accept: application/x-yaml" \
-H "X-Auth-Token: 25a29f8fd30c611f399370ab1be8a0b4f83d1e06" \
-d client=local \
-d tgt='*' \
-d fun='cmd.run' \
-d arg="free -m"

Recommended Posts