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

这里演示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】安装和使用

猜你喜欢

转载自blog.51cto.com/75368/2133443