天天看點

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

這裡示範ansible-cmdb插件的安裝,web展示和導入MySQL資料庫的示例

安裝環境:

Centos7

IP:10.1.1.10

1、安裝apache:

[[email protected] ~]yum install httpd

因為要使用web浏覽,是以安裝apache

2、安裝ansible-cmdb插件

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

[[email protected] opt]# tar -xvf ansible-cmdb-1.27.tar.gz

[[email protected] opt]# cd ansible-cmdb-1.27

[[email protected] ansible-cmdb-1.27]# python setup.py install

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

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

如果不複制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資訊

[[email protected] ~]# mkdir out

[[email protected] ~]# ll

drwxr-xr-x. 2 root root 40 Jun 27 20:30 out

[[email protected] ~]# ansible all -m setup --tree out/

(2)、将生成的facts資訊生成web頁面

[[email protected] ~]# ansible-cmdb out/ > overview.html

[[email protected] ~]# 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主機資訊。

[[email protected] ~]# 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

[[email protected] ~]#

b、把overview.html複制到/var/www/html/test/目錄下

[[email protected] ~]# cp overview.html /var/www/html/test

可能需要改權限:

[[email protected] ~]# chown -R apache:apache /var/www/html/test/

(3)、在web頁面展示:

http://10.1.1.10/test/overview.html

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

(4)、輸出sql檔案,導入資料到mysql或者SQLite

A、輸出為sql格式的檔案:

[[email protected] ~]# ansible-cmdb -t sql out > cmdb.sql

[[email protected] ~]# 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

[[email protected] ~]#

B、将資料庫導入資料 ,這裡導入的是本機的資料庫中

[[email protected] ~]# systemctl start mysqld

[[email protected] ~]# mysql -uroot -p ansdb < cmdb.sql //将資料庫導入資料

C、登入MySQL資料庫,查詢導入資料是否成功

[[email protected] ~]# 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 //用這個ansdb資料庫

Database changed

mysql>

mysql> show tables; //檢視資料庫中的表

+-----------------+

| Tables_in_ansdb |

+-----------------+

| hosts |

+-----------------+

1 row in set (0.03 sec)

mysql> select * from hosts; //查詢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>

轉載于:https://blog.51cto.com/75368/2133443