7.10 Ansible API 最全使用文档(中文) ===================================== |image0| 大家都知道 Ansible 是一个轻量级的部署工具,这里的轻量体现在哪里呢? master 与 节点间使用ssh通信,也不需要像salt那样需要在客户端装minion,然后在master和minion各起一个服务。 在以前 ansible 都是使用的命令行来执行playbook,但在数百个分布式集群部署,如果一台一台登陆进去使用命令行部署,效率极低。 因此,如果能使用 api 的方式进行playbook的调用,再对 api 做一层封装,会极大的提高效率。 无意中使用 pip search 进行搜索,还真的有 ansible-api |image1| 这是 2018 年10月份才开始的项目,目前来说成熟度还不够。 对于 ansible-api 有几点需要注意: 1. ansible-api 需要依赖 python3.7+ 和 最新的 openssl,对ansible环境发动较大。 2. ansible-api 成熟度还不够,在部署过程中,有遇到几个坑,需要修改源码解决。 3. ansible-api 返回结果为json串,会包含所有节点的所有playbook 的所有task 的信息,虽然全,但信息量可能过大。 7.10.1 基础环境准备 ------------------- 使用最新的 openssl ,这一点让我在部署时候花了很多的时间,踩了很多的坑。在这里进行总结整理。 先根据(\ `https://www.jianshu.com/p/3ec24f563b81)将 `__ openssl 也要升级到 1.1.1 .. code:: shell # 安装依赖库 yum install -y zlib zlib-dev openssl-devel sqlite-devel bzip2-devel libffi libffi-devel gcc gcc-c++ # 安装最新版本的openssl wget http://www.openssl.org/source/openssl-1.1.1.tar.gz tar -zxvf openssl-1.1.1.tar.gz cd openssl-1.1.1 ./config --prefix=$HOME/openssl shared zlib make && make install # 设置环境变量LD_LIBRARY_PATH echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/openssl/lib" >> $HOME/.bash_profile source $HOME/.bash_profile ansible-api 要求 python3.7,所以需要再安装 python3.7 .. code:: shell mkdir /root/python37 && cd /root/Python-3.7.2 && wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz && tar -xvf Python-3.7.2.tgz cd /root/Python-3.7.2 && mkdir /usr/local/python3 ./configure --prefix=/usr/local/python3 --with-openssl=$HOME/openssl make && make install ln -s /usr/local/python3/bin/python3 /usr/bin/python3 ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3 7.10.2 部署 ansible-api ----------------------- 手动配置一下 ansible-api 的配置 .. code:: shell # ============================================== # Config file for Ansible-Api # # A restful HTTP API for ansible # I am not a part of ansible official code # ============================================== ## # Base configuration part # [default] #listen host host = 0.0.0.0 #listen port port = 8765 #signature string for api call sign_key = wangbm #log path if using daemon mode log_path = /var/log/ansible-api.log #worker number (default: 1) workers = 1 #response of a task will be timeout (sec, default: 3600 [1 hour], task will NOT break off after timeout) timeout = 3600 #websocket subprotocols ws_sub = #ip white list (multiple separated by space, leave a blank for all allowed) allow_ip = ## # the path part for playbooks and scripts # [directory] #your playbook path (file *.yml in this dir will be worked) playbook = /root/deployment/ #your script path (file *.sh in this dir will be worked) script = ansible-api 会调用 ansible 库的命令,这个过程不能指定 ansible.cfg 的文件路径(其默认是从 ``/etc/ansible/ansible.cfg`` 读取)。所以需要将我们当前的配置文件(\ ``/root/deployment/ansible.cfg``\ )拷贝至\ ``/etc/ansible/ansible.cfg`` 然后由于原生的 ansible-api 的bug,需要修改代码,在如下函数位置(\ ``/usr/local/python3/lib/python3.7/site-packages/ansible_api/callback.py``\ )添加一个参数 |image2| 通过执行命令,即可开启 ansible server .. code:: shell /usr/local/python3/bin/ansible-api -c /etc/ansible/api.cfg -d & 个人使用命令行启动的方式,不太优雅,可以写一个 service 文件,用服务的方式进行管理。 服务开启后,如何调用呢? 在 github 仓库里,有提供一个简易的文档(https://github.com/lfbear/ansible-api/wiki/http-api-usage) 你可以使用 postman 进行测试,也可以使用 curl 发送请求: :: curl -X POST \ http://127.0.0.1:8765/playbook \ -H 'cache-control: no-cache' \ -d '{ "n": "wangbm", # playbook 的名字 "h": "all", # 要执行 playbook 的节点 "f": "backup_info.yml", # yml文件名字,不需要使用绝对路径 "s": "735f88138d00c7eda6271f96fe99fa45", # 数字签名 "c": 5 }' 里面的参数都好理解,就 ``s`` 这个参数,这里要注意一下。 还记得我在ansible-api.cfg 的配置里,有一个配置项是 ``sign_key=wangbm`` 那这里的 ``s`` 怎么计算呢? 可以使用如下这条shell命令 .. code:: shell shell :echo -n 'wangbmlocalhostbackup_info.ymlwangbm'|md5sum |cut -d ' ' -f1 发送了请求后,返回的结果如下 |image3| rc 为0,表示所有节点都没有出现 fatal 致命错误(有设置 ignore_errors 的错误也会返回0). rc 为非0,表示有 fatal 致命错误,说明有部分节点部署/升级失败。 |image4| -------------- |image5| .. |image0| image:: http://image.iswbm.com/20200602135014.png .. |image1| image:: http://image.iswbm.com/20190716111523.png .. |image2| image:: http://image.iswbm.com/20190716112113.png .. |image3| image:: http://image.iswbm.com/20190716112824.png .. |image4| image:: http://image.iswbm.com/20190716112838.png .. |image5| image:: http://image.iswbm.com/20200607174235.png