美文网首页
Salt-api配置及saltypie基本操作

Salt-api配置及saltypie基本操作

作者: 万州客 | 来源:发表于2020-12-21 23:03 被阅读0次

    salt-api是我工作中经常使用的。现在想玩玩更高级的东东,从基础开始,再次积累起来~~~

    参考URL:
    https://pypi.org/project/saltypie/
    https://www.cnblogs.com/yanjieli/p/10916198.html

    一,安装salt-master, salt-minion,salt-api,salt-call

    此处不表,见本人前两章blog。

    二,创建认证用户,并设置密码

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

    三,生成自签名证书用于salt-api的远程连接

    salt-call --local tls.create_self_signed_cert
    正常输出:

    local:
        Created Private Key: "/etc/pki/tls/certs/localhost.key." Created Certificate: "/etc/pki/tls/certs/localhost.crt."
    

    四,打开salt-master配置文件的包含功能

    vim /etc/salt/master
    default_include: master.d/*.conf
    

    五,创建api配置文件,将上面生成的证书写到配置文件

    vim /etc/salt/master.d/api.conf
    rest_cherrypy:
      host: 192.168.1.211
      port: 8000
      ssl_crt: /etc/pki/tls/certs/localhost.crt
      ssl_key: /etc/pki/tls/certs/localhost.key
    

    六,创建api认证配置文件

    vim /etc/salt/master.d/auth.conf
    external_auth:
      pam:
        saltapi:
          - .*
          - '@wheel'
          - '@runner'
          - '@jobs'
    

    七,重启salt-master和启动salt-api

    systemctl restart salt-master
    systemctl start salt-api
    

    八,使用curl测试token生成和test.ping模块命令

    获取token
    curl -sSk https://192.168.1.211:8000/login -H 'Accept: application/x-yaml' -d username=saltapi -d password=saltapipwd -d eauth=pam

    return:
    - eauth: pam
      expire: 1608602843.72114
      perms:
      - .*
      - '@wheel'
      - '@runner'
      - '@jobs'
      start: 1608559643.721139
      token: c1df246e1cda26c337872ea336faa9bdfc203b61
      user: saltapi
    
    

    测试ping
    curl -sSk https://192.168.1.211:8000 -H 'Accept: application/x-yaml' -H 'X-Auth-Token: c1df246e1cda26c337872ea336faa9bdfc203b61' -d client=local -d tgt='*' -d fun=test.ping

    return:
    - 192.168.1.211: true
      192.168.1.212: true
    

    九,安装saltpie

    pip install saltypie
    如果找不到这个库,就到pypi上下载tar.gz文件,直接pip安装

    十,测试一下这个库的基本用法

    D:\Python38\python.exe D:/Code/salt-api-runner/main.py

    from saltypie import Salt
    
    salt = Salt(
        url='https://192.168.1.211:8000',
        username='saltapi',
        passwd='saltapipwd',
        trust_host=True,
        eauth='pam'
    )
    
    exe_return = salt.execute(
        client=Salt.CLIENT_LOCAL,
        target="*",
        fun='test.ping'
    )
    print('exe_return:', exe_return)
    
    async_exe_return = salt.execute(
        client=Salt.CLIENT_LOCAL,
        target="*",
        fun='test.ping',
        run_async=True
    )
    print('async_exe_return:', async_exe_return)
    jid = async_exe_return['return'][0]['jid']
    print('jid:', jid)
    jid_return = salt.lookup_job(jid)
    print('jid_return:', jid_return)
    

    这个比老是直接curl或是requests,g还是要方便耐用些~

    D:\Python38\python.exe D:/Code/salt-api-runner/main.py
    exe_return: {'return': [{'192.168.1.211': True, '192.168.1.212': True}]}
    async_exe_return: {'return': [{'jid': '20201221144803499961', 'minions': ['192.168.1.211', '192.168.1.212']}]}
    jid: 20201221144803499961
    jid_return: {'return': [{'192.168.1.212': True, '192.168.1.211': True}]}
    
    Process finished with exit code 0
    

    LOOK!!!


    2020-12-21 23_03_34-悬浮球.png

    相关文章

      网友评论

          本文标题:Salt-api配置及saltypie基本操作

          本文链接:https://www.haomeiwen.com/subject/metjnktx.html