Ansible 插件 之 CMDB安装和使用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.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
(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 //将数据库导入数据
[[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
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安装和使用的主要内容,如果未能解决你的问题,请参考以下文章
资产管理 cmdb之ansible 获取服务器硬件软件等信息
资产管理 cmdb之ansible 获取服务器硬件软件等信息