1. 程式人生 > >Ansible 插件 之 【CMDB】安裝和使用

Ansible 插件 之 【CMDB】安裝和使用

sql數據庫 shadow wget free 演示 httpd users pro HA

這裏演示ansible-cmdb插件的安裝,web展示和導入MySQL數據庫的示例
安裝環境:
Centos7
IP:10.1.1.10

1、安裝apache:
[root@Node1 ~]yum install httpd
因為要使用web瀏覽,所以安裝apache

2、安裝ansible-cmdb插件

[root@Node1 opt]# wget https://files.pythonhosted.org/packages/37/1b/1fcff0a38a4e07d9d3f75113494ec0b25fd271b650bda52907ae1a80cbfb/ansible-cmdb-1.27.tar.gz

[root@Node1 opt]# tar -xvf ansible-cmdb-1.27.tar.gz

[root@Node1 opt]# cd ansible-cmdb-1.27
[root@Node1 ansible-cmdb-1.27]# python setup.py install

安裝完成後,把解壓目錄的src(/ansible-cmdb-1.27/src)目錄下的ansible-cmdb.py文件復制到/usr/bin/目錄下

如果不復制ansible-cmdb.py文件復制到/usr/bin/目錄,下面生成html文件或其他文件格式的時候會
報錯,報錯提示為:Couldn‘t find /usr/bin/ansible-cmdb.py in . or /usr/bin/../lib/ansible-cmdb/ or /usr/bin/../lib/ansiblecmdb/ (cwd=/opt/ansible-cmdb-1.27/src)

3、使用

(1)、生成所有主機得facts信息
[root@Node1 ~]# mkdir?out
[root@Node1 ~]# ll
drwxr-xr-x. 2 root root 40 Jun 27 20:30 out

[root@Node1 ~]# ansible all -m setup --tree out/

(2)、將生成的facts信息生成web頁面
[root@Node1 ~]# ansible-cmdb out/ > overview.html
[root@Node1 ~]# ll
drwxr-xr-x. 2 root root 40 Jun 27 20:30 out
-rw-r--r--. 1 root root 118584 Jun 27 20:43 overview.html

a、以資產列表得形式統計出ansible主機信息。

[root@Node1 ~]# ansible-cmdb -t txt_table --columns name,os,ip,mem,cpus out/
Name OS IP Mem CPUs


10.1.1.12 CentOS 7.3.1611 192.168.43.94 0g 1
localhost CentOS 7.3.1611 192.168.43.187 0g 1
[root@Node1 ~]#

b、把overview.html復制到/var/www/html/test/目錄下
[root@Node1 ~]# cp overview.html /var/www/html/test
可能需要改權限:
[root@Node1 ~]# chown -R apache:apache /var/www/html/test/

(3)、在web頁面展示:
http://10.1.1.10/test/overview.html

(4)、輸出sql文件,導入數據到mysql或者SQLite

A、輸出為sql格式的文件:
[root@Node1 ~]# ansible-cmdb -t sql out > cmdb.sql

[root@Node1 ~]# ll
-rw-r--r--. 1 root root 1749 Jun 27 21:34 cmdb.sql
drwxr-xr-x. 2 root root 40 Jun 27 20:30 out
-rw-r--r--. 1 root root 118584 Jun 27 20:43 overview.html

[root@Node1 ~]#

B、將數據庫導入數據
[root@Node1 ~]# systemctl start mysqld

[root@Node1 ~]# mysql -uroot -p ansdb < cmdb.sql //將數據庫導入數據

[root@Node1 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.21 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql>
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| ansdb |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.53 sec)

mysql> use ansdb
Database changed

mysql>
mysql> show tables;
+-----------------+
| Tables_in_ansdb |
+-----------------+
| hosts |
+-----------------+
1 row in set (0.03 sec)

mysql> select * from hosts;
+-----------+-------+----------------+---------+------------+--------+-----------------------+---------------+----------------+-----------+-----------+------------------------------------------+-------+------+------------+-----------+
| name | fqdn | main_ip | os_name | os_version | system | kernel | arch_hardware | arch_userspace | virt_type | virt_role | cpu_type | vcpus | ram | disk_total | disk_free |
+-----------+-------+----------------+---------+------------+--------+-----------------------+---------------+----------------+-----------+-----------+------------------------------------------+-------+------+------------+-----------+
| 10.1.1.12 | Node1 | 192.168.43.94 | CentOS | 7.3.1611 | Linux | 3.10.0-514.el7.x86_64 | x86_64 | x86_64 | VMware | guest | Intel(R) Core(TM) i3-2330M CPU @ 2.20GHz | 2 | 0.5 | 50 | 47.1 |
| localhost | Node1 | 192.168.43.187 | CentOS | 7.3.1611 | Linux | 3.10.0-514.el7.x86_64 | x86_64 | x86_64 | VMware | guest | Intel(R) Core(TM) i3-2330M CPU @ 2.20GHz | 2 | 0.5 | 50 | 46.9 |
+-----------+-------+----------------+---------+------------+--------+-----------------------+---------------+----------------+-----------+-----------+------------------------------------------+-------+------+------------+-----------+
2 rows in set (0.00 sec)

mysql>
技術分享圖片

Ansible 插件 之 【CMDB】安裝和使用