如何将Python读取Ansible playbooks返回信息实现自动化?

摘要:一.背景及概要设计 当公司管理维护的服务器到达一定规模后,就必然借助远程自动化运维工具,而ansible是其中备选之一。Ansible基于Python开发,集合了众多运维工具(puppet、chef、func、fabric)的优点,实现了批
一.背景及概要设计 当公司管理维护的服务器到达一定规模后,就必然借助远程自动化运维工具,而ansible是其中备选之一。Ansible基于Python开发,集合了众多运维工具(puppet、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。Ansible是借助ssh来和远程主机通讯的,不需要在远程主机上安装client/agents。因为上手容易,配置简单、功能强大、扩展性强,在生产应用中得到了广泛的应用。使用过程中,读取、解析、判断、保存Ansible playbooks 的执行返回信息是重要一坏。本文详细描述如何实现Python读取Ansible playbooks 执行返回信息,并且保存到数据库中。 Ansible playbooks 的返回信息,有相应的格式。 例如: PLAY [play to setup web server] ***************************************************** TASK [Gathering Facts] ************************************************************** ok: [172.177.117.129] ok: [172.177.117.130] TASK [Installed the latest httpd version] *********************************************** ok: [172.177.117.129] ok: [172.177.117.130] TASK [restart service] *********************************************************** changed: [172.177.117.129] changed: [172.177.117.130] PLAY RECAP ************************************************************************** 172.177.117.129 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 172.177.117.130 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 从上面的例子可以看出,返回的运行信息还是很丰富的,从中可以得出play、task的名字、每个task执行情况,以及play运行情况的概况。 即: When you run a playbook, Ansible returns information about connections, thenamelines of all your plays and tasks, whether each task has succeeded or failed on each machine, and whether each task has made a change on each machine. At the bottom of the playbook execution, Ansible provides a summary of the nodes that were targeted and how they performed. General failures and fatal “unreachable” communication attempts are kept separate in the counts. 重点及难点:从结果中找出规律,格式化结果,怎么用正则表达式取得想要的信息。 二.表设计 通过对Ansible playbooks返回信息的分析,可以将其分成两类(或者说两部分),一是play的整体执行情况(主要信息为PLAY RECAP),另一个是每个task的执行详情。因此,我们设计了两张表。
阅读全文