找回密码
 注册
查看: 550|回复: 7

ansible-cmdb 部署安装 从ansible事实收集输出生成主机概述

[复制链接]

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
发表于 2023-3-27 11:00:00 | 显示全部楼层 |阅读模式
一、概述0 n; x+ ^6 i" M; R
Ansible-cmdb 获取 Ansible 事实收集的输出并将其转换为包含系统配置信息的静态 HTML 概览页面(和其他内容)。
( p5 V0 S. s7 C; K/ R3 U  ?" c! }. Y/ T. |! O
它支持多种类型的输出(html、csv、sql 等)并使用自定义数据扩展 Ansible 收集的信息。对于每个主机,它还显示组、主机变量、自定义变量和机器本地事实。; W" ~- r, ]6 L( I
: ?/ @- G0 y) P. J9 l  F- E
安装要求:% D5 e( @& m! @

Ansible-cmdb 需要 Python v2.7+ / 3.0+.

理论上,它应该适用于任何可以运行Python的系统,包括BSD,Linux,Windows,Solaris和MacOS。在实践中,ansible-cmdb 是在 Ubuntu 16.04 上开发的,并在 Debian、Ubuntu 和 Centos 的最新稳定版本上进行了测试。

安装

Ansible-cmdb可以使用python包管理器pip进行安装。还有适用于各种 Linux 发行版的独立软件包。或者,您可以使用brew或普通的make install.

通过点

通过 Pip 安装:

为您的发行版安装 pip(如果您还没有)。

通过 Pip 安装 Ansible-cmdb:

sudo pip install ansible-cmdb

您也可以通过 Pip 升级 Ansible-cmdb:

% `# D# l3 {$ i; K8 H9 ?. X" B+ A
sudo pip install --upgrade ansible-cmdb% J! x; `" @3 ?4 ^7 t: F: v

1、pip安装

pip install ansible-cmdb

或者

pip install --upgrade ansible-cmdb

: U; l1 h/ V! E
2 [4 Z; i& E8 B5 o& |; Z* R; Z  _
brew install ansible-cmdb


; ]2 H* t4 g  O: ]

其他操作系统安装:

tar -vxzf ansible-cmdb*.tar.gz
; k9 x. h9 Y9 |! S! W" q4 B& m! kcd ansible-cmdb*
+ n$ {; x1 S/ a' ?# s# `* [1 P4 }. N2 R8 I$ Z* _, o" X
sudo make install

4 }; i; T* Z: e' {  Z: S" v7 h

Installation from Git repository:

使用git方式安装:

git clone https://github.com/fboender/ansible-cmdb.git

https://github.com/fboender/ansible-cmdb.git  

0 h% J6 h- M5 v$ B) ^) T! w


2 X( ^0 Z) \- C. f5 O6 L

; J) B8 V1 g! k  o8 k3 e. R

[root@jumpserver3 ~]# git clone https://github.com/fboender/ansible-cmdb.git

Cloning into 'ansible-cmdb'...

remote: Enumerating objects: 2774, done.

remote: Counting objects: 100% (38/38), done.

remote: Compressing objects: 100% (23/23), done.

remote: Total 2774 (delta 15), reused 25 (delta 15), pack-reused 2736

Receiving objects: 100% (2774/2774), 1.47 MiB | 0 bytes/s, done.

Resolving deltas: 100% (1576/1576), done.

[root@jumpserver3 ~]# ls

ansible-cmdb  jumpserver-v3.0.3  jumpserver-v3.0.3.tar.gz  qemu-guest-agent-1.5.3-ksyun.x86_64.rpm  quick_start.sh  sudo-1.9.5-3.el7.x86_64.rpm

[root@jumpserver3 ~]#

. s# H- n' L5 G9 {

下载安装包:

https://github.com/fboender/ansible-cmdb/releases/download/1.31/ansible-cmdb-1.31.tar.gz

; O( c4 q. A. D& E0 w- f


7 @, q" k' Q0 y9 F- S5 h) q/ d5 t

cd ansible-cmdb

( Y. o$ m+ y* H

sudo bash -c ". build.sla && install"

6 ^( N! g! Q( d+ ?

4 N4 M" Q  E" c( w+ Z% m8 ~) E
[root@jumpserver3 ansible-cmdb]# sudo bash -c ". build.sla && install "# A1 D8 m6 z$ Q
Installed in /usr/local/
  \6 i$ R* h, v1 F# m
' E- ]# V, l: F. `4 c0 S
# W* D% b8 O2 J7 B+ Y: t( [2 g" y% k
" o* ?: J( o' O3 Y4 W  g% }% g  k$ S# j5 U/ {3 ?9 N3 M

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2023-3-27 11:00:01 | 显示全部楼层
1、pip安装
) @, i# }0 K$ O& ]& apip install ansible-cmdb
, S) ]1 w. V  I7 w3 c+ y& L1 |或者9 X4 A$ \- r/ c% g& B4 V! M3 O5 \; r
pip install --upgrade ansible-cmdb
! H2 a" f. k( M4 ~, ~% S- [/ S# u, z- n/ L, H
2、下载安装包$ i, T( B/ o6 B+ U
https://github.com/fboender/ansible-cmdb/
/ {- ?4 I( {2 F! I+ u+ [" X; swget https://github.com/fboender/ansi ... le-cmdb-1.17.tar.gz
" U3 T1 `! C8 t2 w% R3 vtar -vxzf ansible-cmdb*.tar.gz/ \: T8 Y/ a$ Z6 P9 d) V
cd ansible-cmdb*
& v3 c0 Q9 |' Csudo make install- i8 _2 y" J* k( K. M5 x

9 _2 n  ]: B9 W3、git直接拉取
! x4 @) a, r3 J3 Zgit clone https://github.com/fboender/ansible-cmdb.git
% b4 z0 A4 Y% i& N; H, Pcd ansible-cmdb
: Z" U6 M. l4 `$ Jsudo bash -c ". build.sla && install"8 r1 ?# y0 k' i$ P( A* t

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2023-3-27 11:00:02 | 显示全部楼层
命令参数' r: ?* c9 T/ O' M+ f8 X
登录后复制
" X9 q9 C# |- `/ O; o" L0 PUsage: ansible-cmdb [option] <dir> > output.html6 F& L& j5 v; z% x( K' r/ n  K$ {
Options:5 |5 b, u; t3 V) P* g$ C. P  q
  --version             show program's version number and exit0 i, Z: @5 W1 o; M
  -h, --help            show this help message and exit7 Z6 [3 c" _- Y' r% C& b1 A
  -t TEMPLATE, --template=TEMPLATE0 F5 |! f( m2 s0 R0 g4 W9 X% H  L" ^
                        Template to use. Default is 'html_fancy'- Y& G8 ~# X+ J
  -i INVENTORY, --inventory=INVENTORY& S" k0 b+ @8 }6 c3 d8 q6 _( [0 w+ x
                        Inventory to read extra info from
* K! g1 g2 w5 A6 a5 l# q  -f, --fact-cache      <dir> contains fact-cache files) i8 V: K6 K% m$ H7 n
  -p PARAMS, --params=PARAMS
/ C! [; {/ @' C                        Params to send to template
, j8 W$ Q. J5 Y1 ^5 S  -d, --debug           Show debug output: s) K+ z1 h- ~$ {; Y
  -q, --quiet           Don't report warnings
( w7 l! w: {) s6 e6 e1 _& X  -c COLUMNS, --columns=COLUMNS
: W- g% @; D# O2 c+ G( @' N% ^; j                        Show only given columns# i( N; B% l+ W; e4 Y8 Q  ?# M
  -C CUST_COLS, --cust-cols=CUST_COLS0 ~: f# `( r9 V
                        Path to a custom columns definition file& E! s! Z# [% c# [2 V
  -l LIMIT, --limit=LIMIT3 h  @  P' ~: \1 Z% j7 d, w
                        Limit hosts to pattern
  u: |2 O% u6 }. l2 Z; p  --exclude-cols=EXCLUDE_COLUMNS
! n- R. t  O, n                        Exclude cols from output
0 F5 X- @5 u. j4 |% i5 L1 ^; k
% N5 _1 }2 E, F% Q3 ^; X0 R* T! ^! l; p, m2 c
[root@jumpserver3 ansible-cmdb]# ansible-cmdb --help
3 f" Y$ Y8 _, \% I" c- UUsage: ansible-cmdb.py [option] <dir> > output.html" U1 F' D: V* g% `
, {( c0 X/ G0 L% u# E
Options:
7 {( E( F+ V8 l6 {4 Q  --version             show program's version number and exit! w/ C6 v) H1 e
  -h, --help            show this help message and exit- V: k5 s. h' Z- [! X1 l( D
  -t TEMPLATE, --template=TEMPLATE
! K. G4 }" w+ A1 h                        Template to use. Default is 'html_fancy'* n1 n3 b% |& M0 f
  -i INVENTORY, --inventory=INVENTORY
) w$ ?1 m# A' v$ }) W                        Inventory to read extra info from  x+ k6 _1 E* @
  -f, --fact-cache      <dir> contains fact-cache files+ o9 D9 n( I" H
  -p PARAMS, --params=PARAMS% J3 V% c. D: T' i, g7 r' h' O
                        Params to send to template
( O: a; e7 n5 q- y  S% a6 I  -d, --debug           Show debug output, N( a- @3 l3 d8 T) i4 n
  -q, --quiet           Don't report warnings
  `: C/ A5 X( l! M% K9 E% a$ S  -c COLUMNS, --columns=COLUMNS
6 n4 V* O- u5 W* T2 V                        Show only given columns4 p- m& f& x! B6 Z
  -C CUST_COLS, --cust-cols=CUST_COLS
9 }. O7 h6 V/ e  Q: q                        Path to a custom columns definition file1 P9 O; l7 n. F0 U. c! ^
  -l LIMIT, --limit=LIMIT
; I( `/ G- C) d# b5 b                        Limit hosts to pattern8 g; ^" j/ ~5 R/ M. l/ G' t% G0 j
  --exclude-cols=EXCLUDE_COLUMNS
' u7 R2 s" ], R0 c                        Exclude cols from output
* N& s4 r4 W2 k( g2 G$ x[root@jumpserver3 ~]# pip install -I "ansible<2.9"
( \- U* {/ P* @; A/ aWARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.2 u5 r) i/ F+ Y
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
4 W* N8 f3 d7 K3 w0 X3 H8 ~" I  qTo avoid this problem you can invoke Python with '-m pip' instead of running pip directly.4 f) T+ I2 {; m# k) p
Collecting ansible<2.9
3 Y0 z- G0 [2 y  Downloading ansible-2.8.20.tar.gz (12.7 MB)9 ]* w) E4 z4 O* b, N# ]& {
     |████████████████████████████████| 12.7 MB 4.5 kB/s
) P4 k. R9 C8 J; xCollecting jinja2
+ g$ j5 Y  O. C# m  N  Downloading Jinja2-3.0.3-py3-none-any.whl (133 kB)
; ?/ N6 d: h! ?4 r     |████████████████████████████████| 133 kB 1.8 MB/s + x& ]: d" S5 ^5 o+ g0 W
Collecting PyYAML! u1 s; R" \: G
  Using cached PyYAML-6.0-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (603 kB)
4 R  ^7 u2 j# @1 m  Y9 wCollecting cryptography# c, V/ l, K1 {  x/ {
  Downloading cryptography-40.0.0-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.7 MB)2 {( }- I$ `' ]4 v7 z; p0 b
     |████████████████████████████████| 3.7 MB 54 kB/s
' ~/ l$ ?0 S4 k  F: ^1 {1 HCollecting cffi>=1.12
5 w$ L- F# M& l1 N2 G1 ?& q) Y  Downloading cffi-1.15.1-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (402 kB)4 b% K, |( |+ F
     |████████████████████████████████| 402 kB 1.8 MB/s + \% N$ z( [; {5 ]: ^
Collecting pycparser
. W5 {, d( a/ X' `* I# `1 z0 S  Downloading pycparser-2.21-py2.py3-none-any.whl (118 kB)1 \  z4 [: X1 E/ ^$ i/ D3 S" A5 D
     |████████████████████████████████| 118 kB 1.9 MB/s 2 g" Z7 E' I1 I+ T7 z( ^
Collecting MarkupSafe>=2.0
3 q! w* G5 N- G5 m  Downloading MarkupSafe-2.0.1-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (30 kB)0 A3 R( f  k8 i# F" `! q1 j; h
Using legacy 'setup.py install' for ansible, since package 'wheel' is not installed." h) I( x& u0 O$ d5 j
Installing collected packages: pycparser, MarkupSafe, cffi, PyYAML, jinja2, cryptography, ansible
6 F' O: V' |$ g! @! g4 U    Running setup.py install for ansible ... done9 {1 Z& W- h8 [6 V' y& A7 Z
Successfully installed MarkupSafe-2.0.1 PyYAML-6.0 ansible-2.8.20 cffi-1.15.1 cryptography-40.0.0 jinja2-3.0.3 pycparser-2.21
6 E1 r, Q) Z6 Z6 QWARNING: You are using pip version 20.3.4; however, version 21.3.1 is available.
) {. F" m; c2 P+ o/ y8 ~8 ]8 ~/ \' `You should consider upgrading via the '/usr/bin/python3 -m pip install --upgrade pip' command.
# e% [$ f, r  M1 Y9 q9 l+ x1 v' ^; L4 u( k7 O) \: w

, w; A% V  r4 H; A: d, z. R1 y  @[root@jumpserver3 ~]# pip list5 L+ ]1 e' K( O. c/ o; X
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
' ~2 V$ N* {$ ]4 B1 ?  r# rPlease see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.$ t, p+ G) @* e+ b! {5 F
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.: K* H1 P/ L4 A0 M
Package            Version
5 |! i1 Q( B; d. U# F7 s------------------ --------
7 b7 E* _4 H6 X9 `7 ]" Fansible            2.8.208 q0 N/ p1 o4 J: V% e# s+ ?. V/ b
asgiref            3.4.1! }, A, ?5 o+ L
async-timeout      4.0.2
1 {' b9 W1 X+ q3 N( @cffi               1.15.19 Z* w4 a6 n" ]4 W
cryptography       40.0.02 x' ]' I2 p0 C" @: x. [: Q1 @  i
Django             3.2.18
& \& t8 k. {3 Kgmssl              3.2.2$ [( _2 G3 a/ c+ z6 X9 m$ c$ B; G* z
importlib-metadata 4.8.3" D1 x5 k/ D5 W% F! `+ b# j
Jinja2             3.0.32 E' w5 E! [6 k4 C
MarkupSafe         2.0.1
7 A6 O, Y0 l7 G6 ]4 Lpackaging          21.3/ _! I  Z% c' j& M  i$ H* x' i, Q
pip                20.3.4
0 l- C  m2 @. u0 O4 J- ypycparser          2.21
9 M! r+ p+ j% l- ~pycryptodomex      3.17
' t2 Q5 q+ ]2 K- B- n" w; H9 S3 fpyparsing          3.0.9& T3 N% N- e" L' D. U
pytz               2022.7.1' w% D6 D) U; X7 X0 M/ a- x
PyYAML             6.0
: Z6 `( a2 \! T! D( X6 ?# d8 dredis              4.3.56 ^: S! V$ |( d9 e
setuptools         39.2.0
% [6 o5 O& \+ r( \sqlparse           0.4.3( {3 i$ _' z$ M5 d) G# G
typing-extensions  4.1.1' F6 A. P- q& A! u; V4 C
zipp               3.6.0
0 {2 }6 K$ t7 J4 f5 J- r/ k5 [WARNING: You are using pip version 20.3.4; however, version 21.3.1 is available.3 X  c; P4 g. J, }. W& i
You should consider upgrading via the '/usr/bin/python3 -m pip install --upgrade pip' command.9 G" I& T/ f& ~9 p! @2 F
[root@jumpserver3 ~]# ansible + w# F0 Z) X6 G# ^% y- ]
/usr/local/lib/python3.6/site-packages/ansible/parsing/vault/__init__.py:44: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography. The next release of cryptography will remove support for Python 3.6.
9 Q8 I9 ^, k" \$ b$ z; z  from cryptography.exceptions import InvalidSignature& H  t$ M5 N4 L" [! E
Usage: ansible <host-pattern> [options]0 P* j3 Z* g" a/ O: i$ n) h
" u7 O& |, y: p" g1 r+ H1 }
Define and run a single task 'playbook' against a set of hosts
* k) _+ q2 X& D  {( K4 {/ a( \' p  D( N, A/ ?2 Q" J
Options:+ c: y+ T" C9 u  I0 c- u
  -a MODULE_ARGS, --args=MODULE_ARGS  k! ]! \" |$ F
                        module arguments' G& J$ ?  s. V
  --ask-vault-pass      ask for vault password
$ r# J1 g) ~% T2 q( j  -B SECONDS, --background=SECONDS5 M* W" ]) v! C6 G8 T+ t
                        run asynchronously, failing after X seconds
) @" I3 ~7 b6 W4 {) y9 D                        (default=N/A)6 e# R! Q3 O9 m. \, w1 M  M# A
  -C, --check           don't make any changes; instead, try to predict some
. v# X* y0 v' }/ Q& m3 B  W& g8 @                        of the changes that may occur
. m( B5 `& \( g8 b7 Y# ?  -D, --diff            when changing (small) files and templates, show the
5 b" O/ ^, S2 P                        differences in those files; works great with --check% g+ N0 Z: o. w5 u( t# \8 q
  -e EXTRA_VARS, --extra-vars=EXTRA_VARS
7 e: A9 Y+ Q0 z0 b: F! I" n0 u* V                        set additional variables as key=value or YAML/JSON, if* V1 o$ R$ v4 i0 `; P/ `1 J9 }! u
                        filename prepend with @5 {' }( U- l1 V5 Q4 ]
  -f FORKS, --forks=FORKS! _( R! V9 r- f: X, A
                        specify number of parallel processes to use
3 o, M$ D0 u7 B0 w) G1 z8 j                        (default=5)
% z1 D2 [9 ?& r+ i9 l% g9 C8 [  -h, --help            show this help message and exit
% ~0 k% c4 V9 a4 X9 B8 f9 b$ |: A  -i INVENTORY, --inventory=INVENTORY, --inventory-file=INVENTORY  _, P+ h8 `5 `6 ^( d5 v) r
                        specify inventory host path or comma separated host
- z  R6 w6 n& f                        list. --inventory-file is deprecated5 z6 @/ t/ m( F7 K% S7 z$ w
  -l SUBSET, --limit=SUBSET* V, G6 x. B+ _% Y" K; I
                        further limit selected hosts to an additional pattern4 r( B4 j, g2 v+ c( A7 P7 P
  --list-hosts          outputs a list of matching hosts; does not execute: {  i; x2 U, p# Q2 y" a
                        anything else
' L( Q  U3 D' o; h% a, M7 v  -m MODULE_NAME, --module-name=MODULE_NAME
  b- I0 p! [9 W9 R8 q: C8 w                        module name to execute (default=command)
! f2 ^7 m1 R( F! E% X! F' z6 U  -M MODULE_PATH, --module-path=MODULE_PATH
3 ~: U' p* T/ e4 C) a9 y+ V                        prepend colon-separated path(s) to module library (def
% V- ~+ @, e5 E* @* d+ k& Y0 c& U                        ault=~/.ansible/plugins/modules:/usr/share/ansible/plu+ {7 Q" y1 t& R$ Y( K: Z8 h
                        gins/modules)
+ C- F; _4 X  C3 q+ G, c) e% N- K2 {  -o, --one-line        condense output, Q- F; s) d5 {( h& E
  --playbook-dir=BASEDIR& l9 `  G, Q2 w' O& ?
                        Since this tool does not use playbooks, use this as a8 D4 N* @+ a& F9 I
                        substitute playbook directory.This sets the relative
, C- Q- q2 u) N& w( S3 K4 V                        path for many features including roles/ group_vars/
" K+ r1 @0 d0 L: D6 g1 v% q) B" N                        etc.
% X  B. H. V" s" n2 u5 X( R  -P POLL_INTERVAL, --poll=POLL_INTERVAL
; m- u6 v" p) V/ l3 ^                        set the poll interval if using -B (default=15)8 k! a0 x; L2 A6 q& d2 |' p, G: j
  --syntax-check        perform a syntax check on the playbook, but do not# n# ]* [  p4 o$ e, o
                        execute it+ m  g9 j3 d- h. [9 a
  -t TREE, --tree=TREE  log output to this directory
( N( _& j6 ^9 U' o4 r  --vault-id=VAULT_IDS  the vault identity to use" f, r0 Z+ L: G
  --vault-password-file=VAULT_PASSWORD_FILES  }; k: `  |; q
                        vault password file& T0 r8 F3 c$ j# g( [) J  V7 c
  -v, --verbose         verbose mode (-vvv for more, -vvvv to enable
2 h! J" G( Q  `& a5 Y6 v2 `                        connection debugging)
' ~6 y% |$ O+ ?5 H! n) [  --version             show program's version number, config file location,+ N& h6 B& R- L( s7 Y
                        configured module search path, module location,* f& C6 c/ T' m' p, K& Q
                        executable location and exit4 g! S0 V9 q' |9 a' N( m* O

( x. ?# J* u2 b* y3 L$ J; Z/ g  Privilege Escalation Options:3 n+ ?" G. a% J5 W4 }
    control how and which user you become as on target hosts
# u/ X' H* U( y- X3 w0 C1 R  @  p. H6 k* \2 [% @/ r
    -b, --become        run operations with become (does not imply password7 E. i! N0 j" v3 F6 `9 l, C  L
                        prompting)% E& F5 {% [9 B: q+ ?
    --become-method=BECOME_METHOD
- o. B& B# X$ }5 T& i6 E" u                        privilege escalation method to use (default=sudo), use
' `7 H0 A% i& _- h                        `ansible-doc -t become -l` to list valid choices.
: x  }' f, Z% E% D. q. F- `/ ]    --become-user=BECOME_USER
0 t/ v) B  |4 ^+ s& [$ o                        run operations as this user (default=root)
4 U& q- h/ }% ]( R. [" C- N. A/ [    -K, --ask-become-pass
0 r, H) ~. [) N' m8 C! L: K! \6 }                        ask for privilege escalation password# j; f& C: [% |' z. x
& `6 X5 A; Z; W
  Connection Options:
3 \. C, y0 J7 v. J# M# `& u    control as whom and how to connect to hosts9 ?/ _& U# q/ v# R' k2 \" S

5 {# g& r2 O5 ?% [. h    -k, --ask-pass      ask for connection password
: J  P6 a2 P1 z( u! Y    --private-key=PRIVATE_KEY_FILE, --key-file=PRIVATE_KEY_FILE
4 l( i3 w- \9 a7 `" ]9 Z* W/ T                        use this file to authenticate the connection
7 m  e7 `: @; [    -u REMOTE_USER, --user=REMOTE_USER# A8 g1 w5 X0 B6 A$ P1 X% ^! M
                        connect as this user (default=None)  e4 x9 O( H9 ?' o
    -c CONNECTION, --connection=CONNECTION* v* W* l' `6 o, _
                        connection type to use (default=smart)+ [  S& L& M) W* I7 O0 t
    -T TIMEOUT, --timeout=TIMEOUT$ u9 y& M5 g' K, n  f4 W
                        override the connection timeout in seconds
$ F3 ]$ b  o" Q1 k8 v  l                        (default=10)
! ?% `7 L) q3 d0 @    --ssh-common-args=SSH_COMMON_ARGS2 V. g: L* }. E
                        specify common arguments to pass to sftp/scp/ssh (e.g.- O; |# v; I6 B8 b. D! d/ n
                        ProxyCommand)
! s" o1 k7 j' A( _+ Q4 b1 i    --sftp-extra-args=SFTP_EXTRA_ARGS
; _+ L1 g9 A1 b                        specify extra arguments to pass to sftp only (e.g. -f,% q: x0 N0 `5 N; K# ^8 ?
                        -l)
2 a, O2 j8 [. E! L: d% C    --scp-extra-args=SCP_EXTRA_ARGS
6 W1 |* p' @9 a3 l5 h                        specify extra arguments to pass to scp only (e.g. -l)
/ @; H& H9 x, {; i  y& b    --ssh-extra-args=SSH_EXTRA_ARGS7 @3 j* O! P& b3 Y# Z5 W' y5 d+ M  f
                        specify extra arguments to pass to ssh only (e.g. -R)
. ^9 f( t- Z1 D7 X
3 \' d" |# r! l" {* DSome modules do not make sense in Ad-Hoc (include, meta, etc)) t7 O- [6 x3 n
ERROR! Missing target hosts
! D) i2 {! r% ^$ n" I) A[root@jumpserver3 ~]# 3 l- R8 f- d$ e" f

* o2 b# }% `: ^, o: `0 i1 P
! ?/ L6 }) f- o' d7 n% P  h" ?; X' u5 o% _0 ~9 [0 h( G

; Q8 z6 K1 x' t( G& M; G

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2023-3-27 11:00:03 | 显示全部楼层

首先,为主机生成 Ansible 输出:

mkdir out
* s& [: N0 }3 o( f: @ansible -m setup --tree out/ all, W5 _0 w! C& Y
( W1 v8 _  x8 H% L. h

接下来,在生成的 out/ 目录上调用 ansible-cmdb 以生成 CMDB 概述页面:

ansible-cmdb out/ > overview.html
注意

ansible-cmdb 是一个 shell 包装器脚本,它尽最大努力找到要使用的正确 Python 二进制文件和 ansible-cmdb.py 脚本的正确位置。如果无法做到这一点,您可以手动指定正确的 python 解释器和脚本的路径。例如:

/opt/python3/bin/python /opt/ansible-cmdb/src/ansible-cmdb.py
9 h7 Q! [- ?+ S% `; L

默认情况下,使用 html_fancy 模板,该模板生成包含所有主机概述的输出,以及每个主机的详细信息部分。

现在,您可以在浏览器中打开overview.html以查看结果。

完全使用Usage: ansible-cmdb [option] <dir> > output.htmlOptions:  --version             show program's version number and exit  -h, --help            show this help message and exit  -t TEMPLATE, --template=TEMPLATE                        Template to use. Default is 'html_fancy'  -i INVENTORY, --inventory=INVENTORY                        Inventory to read extra info from  -f, --fact-cache      <dir> contains fact-cache files  -p PARAMS, --params=PARAMS                        Params to send to template  -d, --debug           Show debug output  -q, --quiet           Don't report warnings  -c COLUMNS, --columns=COLUMNS                        Show only given columns  -C CUST_COLS, --cust-cols=CUST_COLS                        Path to a custom columns definition file  -l LIMIT, --limit=LIMIT                        Limit hosts to pattern  --exclude-cols=EXCLUDE_COLUMNS                        Exclude cols from output库存扫描

Ansible-cmdb 可以读取您的清单文件(默认情况下为hosts)、清单目录动态清单,并从中提取有用的信息,例如:

  • 主机所属的所有组。
  • 主机变量。这些是每个主机的可选键/值对,可在 playbook 中使用。它们由ansible-cmdb扫描,并被添加到“hostvars”部分下的主机发现的事实中。
    2 z4 _( x  O. T2 a

读取清单是使用 -i 开关到 -i 完成的。它采用单个参数:主机文件、包含主机文件的目录或动态清单脚本的路径。您可以通过用逗号分隔来指定多个库存文件(不要包含空格!

例如:

$ ansible-cmdb -i ./hosts out/ > overview.html

如果该位置存在host_vars和group_vars目录,则会自动包含这些目录。

主机和组变量

Ansible-cmdb 读取并包含清单中的主机和组变量。当您使用 -i 选项将 ansible-cmdb 指向主机清单(通常是hosts文件)时,-i 会自动包含来自host_vars和group_vars目录中的信息(如果在同一目录中找到)。

变量是否包含在输出中取决于所使用的模板。在html_fancy模板中,变量将在“自定义变量”标题下可用。

“html_fancy”模板支持四个特殊变量:

  • groups:主机所属的 Ansible 组的列表。
  • dtap:主机是开发、测试、验收还是生产系统。
  • comment:对主持人的评论。
  • ext_id:主机的外部唯一标识符。- {% O) N/ S& `6 D5 ^, y

例如,假设我们有以下hosts文件:

[cust.megacorp]db1.dev.megacorp.com   dtap=dev  comment="Old database server"db2.dev.megacorp.com   dtap=dev  comment="New database server"test.megacorp.com      dtap=testacc.megacorp.com       dtap=acc  comment="24/7 support"megacorp.com           dtap=prod comment="Hosting by Foo" ext_id="SRV_10029"[os.redhat]megacorp.comacc.megacorp.comtest.megacorp.comdb2.dev.megacorp.com[os.debian]db1.dev.megacorp.com

主机 acc.megacorp.com 将有组“cust.megacorp”和“os.redhat”,将有一个评论说它有24/7支持,并将被标记为acc服务器。Megacorp.com 主机将具有“SRV_10029”的外部ID,这是与Foo公司就托管进行沟通所必需的。

这些变量包含在html_fancy模板的主机概览表中。

有关主机变量的更多信息,请参阅有关主机变量的官方 Ansible 文档。

指定模板

Ansible-CMDB提供了多个模板。您可以使用 -t 或 ---template 参数选择模板:

ansible-cmdb -t tpl_custom out/ > overview.html

“html_fancy”模板是默认模板。

模板可以通过名称或 .tpl 文件的相对/绝对路径来引用。这使您可以实现自己的模板。例如:

$ ansible-cmdb -t /home/fboender/my_template out/ > my_template.html模板参数

某些模板支持影响其输出的参数。参数是使用 -p 或 --parameter 选项指定到 ansible-cmdb 的。-p --parameter可以通过用逗号分隔来指定多个参数。参数中不得有空格。

例如,要指定具有本地 Javascript 库和闭合树的html_fancy模板:

ansible-cmdb -t html_fancy -p local_js=1,collapsed=1 out > overview.html可用模板

Ansible-cmdb 目前提供了以下开箱即用的模板:

  • html_fancy:包含所有主机的动态现代 HTML 页面。
  • html_fancy_split:一个动态的现代 HTML 页面,每个主机的详细信息都在一个单独的文件中。
  • txt_table:可用主机的快速文本表摘要,其中包含一些最少的信息。
  • json:转储所有主机,包括 JSON 格式的组、变量、自定义信息。
  • csv:CSV 模板输出主机的 CSV 文件。
  • 降价:降价模板以markdown格式生成主机信息。
  • sql:SQL 模板生成一个可以加载到 SQLite 或 MySQL 数据库中的.sql文件。
    3 }1 P5 O$ t% w# K

html_fancy:

html_fancy当前是默认模板。

一个花哨的HTML页面,使用jQuery和数据表为您提供所有主机的可搜索,可排序的表概述,只需单击一下即可获得详细信息。

它采用可选参数:

  • local_js=0|1:从本地磁盘加载资源(默认=0)。如果设置,将从本地磁盘而不是通过网络加载资源。
  • collapsed=0|1:控制默认情况下是否折叠主机信息。默认情况下,值 1 将折叠所有主机信息(默认值='0')。
  • host_details=0|1:是否呈现主机详细信息(默认值 = 1)
  • skip_empty=0|1:跳过未收集任何事实(无法访问等)的主机。(默认值 = 0).4 R/ J# T+ a# w- W+ V5 B

html_fancy_split:

此模板与html_fancy模板基本相同,但它生成一个 cmdb/ 目录,其中包含一个index.html文件和一个单独的 html 文件,用于每个主机的详细信息。如果有大量主机并且html_fancy模板呈现速度太慢,这将非常有用。

用法:

ansible-cmdb -t html_fancy_split -i hosts out/

它接受与html_fancy模板相同的参数。

.sql:

sql 模板生成一个可以加载到 SQLite 或 MySQL 数据库中的.sql文件。

$ ansible-cmdb -t sql -i hosts out > cmdb.sql$ echo "CREATE DATABASE ansiblecmdb" | mysql$ mysql ansiblecmdb < cmdb.sql事实缓存

Ansible 可以在运行剧本时缓存来自主机的事实。这是在 Ansible 中配置的,如下所示:

[defaults]fact_caching=jsonfilefact_caching_connection = /path/to/facts/dir

您可以通过指定 -f (----fact-cache) 选项将这些缓存的事实用作带有 ansible-cmdb 的事实目录:

$ ansible-cmdb -f /path/to/facts/dir > overview.html

请注意,---fact-cache 选项将应用于您指定的所有事实目录。这意味着您不能混合使用事实缓存事实目录和正常setup的事实目录。此外,如果您希望手动扩展事实(请参阅Extending章节),则必须省略ansible_facts键并将项目放在 JSON 的根目录中。

限制主机

可以使用 -l 参数来限制输出中包含的主机。这与 Ansible 的限制基本相同。它支持“all”结构,单个主机,组和使用!的反转。它目前不支持通配符(例如 db*.example.com)或主机扩展(例如 db[0-3].example.com) 为了使主机限制起作用,您必须将 -i 参数指向您的主机清单。

一些例子:

# Include only hosts in the group 'cust.acme'ansible-cmdb -i hosts -l 'cust.acme' out/ > cmdb.html# Include all hosts except those in the group 'cust.acme', but include# host 'foo.example.com'ansible-cmdb -i hosts -l 'all:!cust.acme:foo.example.com' out/ > cmdb.html

某些模板(如txt_table和html_fancy)支持列。如果模板支持列,则可以使用 ----columns / -c 命令行选项来指定要显示的列。

---columns 采用逗号分隔的列列表(没有空格!),应该显示。列必须由其 id 字段指定。有关模板支持哪些 id 字段的信息,请查看模板。通常是列标题,但为小写,空格替换为下划线。

例如:

$ ansible-cmdb -t txt_table --columns name,os,ip,mem,cpus facts/Name                    OS             IP             Mem  CPUs----------------------  -------------  -------------  ---  -  jib.electricmonk.nl     Linuxmint 17   192.168.0.3    16g  1  app.uat.local           Debian 6.0.10  192.168.57.1   1g   1  eek.electricmonk.nl     Ubuntu 14.04   192.168.0.10   3g   1  db01.prod.local         Debian 6.0.10  192.168.58.1   0g   1  debian.dev.local        Debian 6.0.10  192.168.56.2   1g   1  db02.prod.local         Debian 6.0.10  192.168.58.2   0g   1  centos.dev.local        CentOS 6.6     192.168.56.8   1g   1  win.dev.local           Windows 2012   10.0.0.3       4g   0  host5.example.com       Debian 6.0.10  192.168.57.1   1g   1  db03.prod.local         Debian 6.0.10  192.168.58.3   0g   1  zoltar.electricmonk.nl  Ubuntu 14.04   194.187.79.11  4g   2

您可以使用 ----exclude-cols 选项来排除特定列。它的工作原理与 ---columns 相同。例如:

ansible-cmdb -t html_fancy_split \             --exclude-cols mem_usage,swap_usage,disk_usage,physdisk_size \             -i hosts \             facts/

如果要添加自定义列,请参阅自定义列部分。

扩展事实

您可以指定需要扫描事实的多个目录。这使您可以覆盖、扩展和填写主机上缺失的信息。您还可以使用它来创建全新的主机或向主机添加自定义事实。

扩展事实与普通 Ansible 事实文件基本相同。当您指定多个事实目录时,Ansible-cmdb 会按顺序扫描所有事实目录并覆盖事实。

请注意,主机必须仍存在于主机文件中,否则它不会生成任何内容。

如果使用 ----fact-cache 选项,则必须省略 ansible_facts 键并将项放在 JSON 的根目录中。这也意味着您只能扩展本机 ansible 事实,而不能扩展 ansible-cmdb 从hosts文件中读取的信息。

覆盖并填写事实

有时 Ansible 无法为主机正确收集某些事实。例如,OpenBSD 事实不包括userspace_architecture事实。您可以手动将其添加到主机。

为您的扩展事实创建一个目录:

$ mkdir out_extend

在其中为主机创建一个文件。该文件的名称必须与hosts文件中显示的名称相同:

$ vi out_extend/openbsd.dev.local{  "ansible_facts": {      "ansible_userspace_architecture": "x86_64"  }}

生成输出时指定两个目录:

./ansible-cmdb out/ out_extend/ > overview.html

您的 OpenBSD 主机现在将包含“用户空间架构”事实。

手动主机

例如,假设您有 100 台 Linux 机器,但只有一台 Windows 机器。不值得在那台Windows机器上设置ansible,但您仍然希望它出现在您的概述中......

为您创建自定义事实目录:

$ mkdir out_manual

在其中为您的 Windows 主机创建一个文件:

$ vi out_manual/win.dev.local{  "groups": [  ],  "ansible_facts": {    "ansible_all_ipv4_addresses": [      "10.10.0.2",      "191.37.104.122"    ],     "ansible_default_ipv4": {      "address": "191.37.104.122"    },     "ansible_devices": {    },     "ansible_distribution": "Windows",     "ansible_distribution_major_version": "2008",     "ansible_distribution_release": "",     "ansible_distribution_version": "2008",     "ansible_domain": "win.dev.local",     "ansible_fips": false,     "ansible_form_factor": "VPS",     "ansible_fqdn": "win.dev.local",     "ansible_hostname": "win",     "ansible_machine": "x86_64",     "ansible_nodename": "win.dev.local",     "ansible_userspace_architecture": "x86_64",     "ansible_userspace_bits": "64",     "ansible_virtualization_role": "guest",     "ansible_virtualization_type": "xen",     "module_setup": true  },   "changed": false}

现在,您可以通过指定两个事实目录来创建包括 Windows 主机在内的概述:

./ansible-cmdb out/ out_manual/ > overview.html自定义事实

您可以将自定义事实(不要与“自定义变量”混淆)添加到主机。默认情况下,这些事实将显示在html_fancy模板中的“自定义事实”标题下。

请注意,这些与托管本地事实不同。主机本地事实是指 Ansible 从主机的每个 //etc/ansible/facts.d 目录中读取的事实。这些也包含在 Ansible-cmdb 的html_fancy模板中,但在“托管本地事实”标题下。此处介绍的自定义事实是在运行 ansible-cmdb 的主机上手动定义的,与 Ansible 本身关系不大。

假设您想将有关已安装软件的信息添加到您的事实中。

为您创建自定义事实目录:

$ mkdir out_custom

在其中为要添加自定义事实的主机创建一个文件:

$ vi custfact.test.local{  "custom_facts": {    "software": {      "apache": {        "version": "2.4",        "install_src": "backport_deb"      },      "mysql-server": {        "version": "5.5",        "install_src": "manual_compile"      },      "redis": {        "version": "3.0.7",        "install_src": "manual_compile"      }    }  }}

为此,必须在custom_facts键下列出事实。

生成概览:

./ansible-cmdb out/ out_custom/ > overview.html

软件项目将列在“自定义事实”标题下。

自定义列

您可以使用 -C (----cust-cols) 选项将自定义列添加到主机概述中。这允许您指定 jsonxs 表达式或 Mako 模板片段来提取和显示自定义主机事实。

自定义列目前仅受html_fancy和html_fancy_split模板支持。

-C 选项采用一个参数,该参数是包含自定义列定义的文件的路径。该文件的语法是Python(即使它看起来像JSON)。可以在存储库中的 examples/cust_cols.conf 文件中找到一个示例:

[    # Show whether AppArmor is enabled    {        "title": "AppArmor",        "id": "apparmor",        "sType": "string",        "visible": False,        "jsonxs": "ansible_facts.ansible_apparmor.status"    },    # Show the nameservers configured on the host    {        "title": "Nameservers",        "id": "nameservers",        "sType": "string",        "visible": True,        "tpl": """          <ul>            <%            # Get ansible_facts.ansible_dns.nameservers            facts = host.get('ansible_facts', {})            dns = facts.get('ansible_dns', {})            nameservers = dns.get('nameservers', [])            %>            % for nameserver in nameservers:              <li>${nameserver}</li>            % endfor          </ul>        """    },    # Show the nameservers configured on the host, but use jsonxs.    {        "title": "Nameservers2",        "id": "nameservers2",        "sType": "string",        "visible": True,        "tpl": """          <ul>            <%            # Get ansible_facts.ansible_dns.nameservers using jsonxs            nameservers = jsonxs(host, 'ansible_facts.ansible_dns.nameservers', default=[])            %>            % for nameserver in nameservers:              <li>${nameserver}</li>            % endfor          </ul>        """    }]

这定义了两个新列:“AppArmor”和“名称服务器”。每列由以下键/值组成:

  • 标题是显示为列用户友好title的内容。必填.
  • id 键必须具有唯一值,以区分列。必填
  • sType 值确定如何在主机概述中对值进行排序。可能的值包括string和 num。必填
  • visible 确定默认情况下列是否处于活动状态(显示)。必填
  • jsonxs 表达式(如果指定)指向每个主机的事实文件中的条目,并确定将为每个主机的列值显示的内容。找出 jsonxs 表达式的最简单方法是在 json 编辑器中打开收集的事实文件之一。有关如何编写 jsonxs 表达式的信息,请参阅 jsonxs自选
  • tpl 表达式(如果指定)是 Mako 模板片段。此模板中提供了单个变量host。访问主机信息时必须小心。如果其中一个主机缺少您尝试访问的信息,模板将不会呈现,ansible-cmdb 将崩溃(通常显示“KeyError”消息)。您应该始终使用 get() 方法并指定默认值。E.g. host.get('ansible_facts', {}).get('ansible_dns', host.get('ansible_facts', {}).get('ansible_dns', {}).get('nameservers', [])或者(并推荐)是使用 jsonxs 访问您的信息(并指定 default=...)。请参阅上面的示例。自选
    - u0 P, A2 W$ e1 _# k' t9 S, b

jsonxs 或 tpl 是必需的。

列排序由 DataTables Javascript 库处理。必须注意具有 num 排序类型 (sType) 的列。如果列值中出现任何非数字字符 ([0-9]),排序将中断。

如果您希望对不同于向用户显示的值进行排序,则可以将string sType 与列值开头的隐藏字段结合使用。请注意,string排序很特殊,因为“100”被认为小于“20”,因为“1”小于“2”。您可以通过在隐藏字段中放置字符串可排序的值来更正此问题。例如,您可以将左侧的值归零:

sort_uptime = "{0:012d}".format(uptime)

或者,您可以将值映射到 0.0 到 1.0 之间的固定浮点数范围。隐藏排序助手的完整示例,它显示了人性化的正常运行时间,但也允许排序(谢谢 Kariton;请参阅问题 https://github.com/fboender/ansible-cmdb/issues/235):

{    "title": "Uptime",    "id": "uptime",    "sType": "string",    "visible": False,    "tpl": """      <%      def ConvertSectoDay(n):          weeks = n // (7 * 24 * 3600 )          n = n % (7 * 24 * 3600)          days = n // (24 * 3600)          n = n % (24 * 3600)          hours = n // 3600          n %= 3600          minutes = n // 60          n %= 60          seconds = n          return("%d Weeks, %d Days, %d Hours, %d Minutes, %d Seconds" % (weeks, days, hours, minutes, seconds))      num_uptime = int(jsonxs(host, 'ansible_facts.ansible_uptime_seconds', default=0))      sort_uptime = "{0:012d}".format(num_uptime)      uptime = ConvertSectoDay(int(jsonxs(host, 'ansible_facts.ansible_uptime_seconds', default=0)))      %>      ## hidden sort helper      <span style="display:none">${sort_uptime}</span>      <span>${uptime}</span>    """},

要使用自定义列文件,请执行以下操作:

ansible-cmdb -C example/cust_cols.conf -i example/hosts example/out/ > cmdb.html

在浏览器中打开cmdb.html文件时,您可能需要在新列显示或出现奇怪行为之前点击右上角的“清除设置”按钮。

自定义模板

可以创建自定义模板来构建完全不同的CMDB或增强现有CMDB。Ansible-cmdb 使用 Mako 模板引擎来渲染输出。

例如,如果要将自定义列添加到html_fancy模板(请注意,仅使用 ----cust-cols 选项更容易。有关更多信息,请参见上文):

  • 在新目录中复制默认html_fancy模板。在这里,我们将使用 ansible-cmdb git 存储库中的文件。

    $ mkdir ~/mytemplate$ cp ~/ansible-cmdb/src/ansiblecmdb/data/tpl/html_fancy.tpl ~/mytemplate/$ cp ~/ansible-cmdb/src/ansiblecmdb/data/tpl/html_fancy_defs.html ~/mytemplate/
  • 编辑html_fancy_defs.html文件并向 cols = 部分添加一个条目。在此示例中,我们将为“BIOS 版本”添加一列。

    <%  cols = [   ...   {"title": "Product Serial","id": "prodserial",    "func": col_prodserial,     "sType": "string", "visible": False},   {"title": "BIOS version",  "id": "bios_version",  "func": col_bios_version,   "sType": "string", "visible": True}, ]
  • 现在您需要实现col_biosversion模板函数。在同一html_fancy_defs.html文件中,搜索“## Column functions”部分。添加一个名为 col_biosversion 的列模板函数:

       <%def name="col_dtap(host, **kwargs)">     ${jsonxs(host, 'hostvars.dtap', default='')}   </%def>+  <%def name="col_bios_version(host, **kwargs)">+    ${jsonxs(host, 'ansible_facts.ansible_bios_version', default='')}+  </%def>
  • 最后,呈现自定义模板。为此,您必须与自定义模板位于同一目录中!.

    $ ansible-cmdb/src/ansible-cmdb -t ./html_fancy -i ~/ansible/hosts ~/ansible/out/ > cmdb.html7 m  a8 P! B4 a/ J' i# A  l2 k

如果要修改html_fancy_split模板,则需要再复制几个文件:

ansible-cmdb (master) $ mkdir ~/mytemplateansible-cmdb (master) $ cp src/ansiblecmdb/data/tpl/html_fancy_defs.html ~/mytemplate/ansible-cmdb (master) $ cp src/ansiblecmdb/data/tpl/html_fancy_split_detail.tpl ~/mytemplate/ansible-cmdb (master) $ cp src/ansiblecmdb/data/tpl/html_fancy_split_overview.tpl ~/mytemplate/ansible-cmdb (master) $ cp src/ansiblecmdb/data/tpl/html_fancy_split.py ~/mytemplate/

这个模板有点特别,因为它不是.tpl,而是Python脚本。您可以通过将 -t 参数指向该脚本来使用它:

    $ cd ~/mytemplate    $ ansible-cmdb -t ./html_fancy_split.py -i ~/ansible/hosts ~/ansible/out/% {% g( K1 M. p

0 T& s. r% _$ N1 p7 X

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2023-3-27 11:00:04 | 显示全部楼层
输出的 HTML 文件在其他计算机上不起作用。
1 z2 J) N% n9 r8 e" G* E9 q8 a当您传输 ansible-cmdb 的输出 HTML 文件并尝试在另一台计算机上的浏览器中打开它时,您会发现它无法正常工作。
; ~( z5 n5 }% y, W$ m( A8 M1 x7 R" B& T2 r# `1 E0 N5 j
这是因为在本地计算机上打开的HTML文件(以 file:// url开头的文件)不允许从互联网上获取所需的Javascript文件(以 http:// 和 https:// 开头的URL)。因此,Ansible-cmdb 会在您安装 ansible-cmdb 时安装这些必需的文件。当然,另一台 PC 不会在本地提供这些文件。
+ ~3 ]' _! G+ g2 q0 S8 b  J1 U/ Y( r- X. P
解决方案是使用 -p local_js=0 参数生成输出,并将生成的 HTML 文件托管在某个地方的 Web 服务器上。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

返回首页|Archiver|手机版|小黑屋|易陆发现技术论坛 ( 蜀ICP备2026014127号-1 )

GMT+8, 2026-6-12 01:46 , Processed in 0.022876 second(s), 22 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表