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

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

[复制链接]

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
发表于 2023-3-27 11:00:00 | 显示全部楼层 |阅读模式
一、概述+ X1 b- H7 W! N/ u( E% S
Ansible-cmdb 获取 Ansible 事实收集的输出并将其转换为包含系统配置信息的静态 HTML 概览页面(和其他内容)。3 s9 s$ C  {. V

# f. G3 S$ i6 {它支持多种类型的输出(html、csv、sql 等)并使用自定义数据扩展 Ansible 收集的信息。对于每个主机,它还显示组、主机变量、自定义变量和机器本地事实。# A8 @8 t/ {( l. z3 D5 c

, [/ K* |1 ~/ z  L- o安装要求:
1 P: \0 J! ~0 i/ D

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:

, S# O# ?8 p( p/ B) T# h
sudo pip install --upgrade ansible-cmdb
- g0 K  m: ]( T7 G6 V, D

1、pip安装

pip install ansible-cmdb

或者

pip install --upgrade ansible-cmdb

. d% [4 a4 {3 l" f7 o2 X0 p1 F
2 P" C/ _$ G" q# r" G: {( M, _& y; h7 X
brew install ansible-cmdb


4 i( G5 ]! j! p- D% Y

其他操作系统安装:

tar -vxzf ansible-cmdb*.tar.gz
6 s1 B  H3 N  H) t+ q9 Lcd ansible-cmdb*/ ^7 K: z) e, v
4 w0 b/ x7 Q) Q* d# N3 T4 t
sudo make install

/ |( t) D3 J; e4 j) }

Installation from Git repository:

使用git方式安装:

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

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

* u, e" c- J+ E. ~( E) i" ~


. i0 g" H8 q, P9 n. `: M

4 U- V# `7 _! K# j8 m) @! o

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

' D$ V' i) r$ z

下载安装包:

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


3 c# z& ]% X2 t# S4 ^; L7 B


7 m1 x* Y6 J, |, |

cd ansible-cmdb

' t5 s  X1 @* S4 g, r$ z

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

4 Z/ A, r) G% R


% u* w; c3 b+ p& L3 j, T. ?! S[root@jumpserver3 ansible-cmdb]# sudo bash -c ". build.sla && install "2 }" o" O, b2 U8 e, S% ?$ R
Installed in /usr/local/# L0 r* g5 X5 e8 k7 t; V! v# ]5 k
  V# D% B2 ^: ^0 D
; _) Y5 N  b0 w; O
/ N+ e1 k4 y" {

. ^5 O) y1 b$ v  p( p

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2023-3-27 11:00:01 | 显示全部楼层
1、pip安装
4 _+ J. p- p3 l6 c; apip install ansible-cmdb- @& I9 W# p- y  C+ c- I, Y
或者: B. d( W; y! s1 C/ g
pip install --upgrade ansible-cmdb' J6 {1 w+ N! L! R, x/ S
2 E  p9 u8 y* x0 K) L8 H0 w
2、下载安装包0 {+ L0 i) V( ^* X' F  J7 X
https://github.com/fboender/ansible-cmdb/
! M" t9 ?3 J6 awget https://github.com/fboender/ansi ... le-cmdb-1.17.tar.gz' o3 Y6 y, ]. m
tar -vxzf ansible-cmdb*.tar.gz
5 e* ]1 ]* w/ A6 ~% E3 \cd ansible-cmdb*1 \/ K- p1 C; k6 V% ~, F& c& y( s' {
sudo make install
) q& J* a- K! H1 b, G! Z: `' b) P. p1 [. m' d/ _
3、git直接拉取8 J) G& o2 f4 \
git clone https://github.com/fboender/ansible-cmdb.git
9 t& Q% y( R& j) \3 y: `cd ansible-cmdb
# ?& [' j$ ]  U: i1 m' [sudo bash -c ". build.sla && install"
* J9 e- c, W. u. O- ^2 M+ ?

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2023-3-27 11:00:02 | 显示全部楼层
命令参数/ x$ e) s. G/ Y' G
登录后复制
  h* {! w( T$ V4 mUsage: ansible-cmdb [option] <dir> > output.html
/ J" E* _- ~6 P3 g( aOptions:% _. Q5 c/ }% x6 n
  --version             show program's version number and exit
: X4 T6 r8 {6 f; G8 {. r  -h, --help            show this help message and exit
- V8 z+ @8 j/ S  -t TEMPLATE, --template=TEMPLATE
, n# Y8 D! k% ?+ e% t8 Z' ]3 x7 e9 N3 l                        Template to use. Default is 'html_fancy'
" m8 F+ |# J, V$ j4 t- l  -i INVENTORY, --inventory=INVENTORY
* j. y: q3 n% t- _                        Inventory to read extra info from
6 C, M8 A$ J  |& e5 K7 W  -f, --fact-cache      <dir> contains fact-cache files
: X7 ^$ Y& ?; ?5 M6 b6 l) w4 E  -p PARAMS, --params=PARAMS' @# d5 e0 ~4 Q4 Z9 g! i# X/ k
                        Params to send to template' d4 x; H, `6 y8 E: Z5 r9 ~1 [5 |
  -d, --debug           Show debug output
+ [8 C" x- ^2 b+ l: W; k  -q, --quiet           Don't report warnings
# i, n4 f' s8 l0 `' W  -c COLUMNS, --columns=COLUMNS& e7 \8 z' H8 _8 L2 y9 [
                        Show only given columns% B" k5 P  B  r6 |
  -C CUST_COLS, --cust-cols=CUST_COLS
! b# v) d/ \% B3 P; j: T                        Path to a custom columns definition file6 ~# a3 f& [1 h0 g# r4 _% p* E
  -l LIMIT, --limit=LIMIT& p0 a( f! r/ T# n) J/ D: R$ D
                        Limit hosts to pattern' T' r( B5 d9 ]" A9 U
  --exclude-cols=EXCLUDE_COLUMNS
. }# C* j- J( F% b) W( r& J                        Exclude cols from output
; ?% _* `. ^6 T0 K$ R+ A3 J5 A& d0 @
: [4 `8 q' a8 A
" `% W8 w0 Q: P  k" B[root@jumpserver3 ansible-cmdb]# ansible-cmdb --help
" n- N) Q6 t: q5 k- B, dUsage: ansible-cmdb.py [option] <dir> > output.html
/ ]2 C9 L* C% s" s8 N" {% |: Q9 _- {" s* S% P/ k
Options:
% k8 c% ~. J0 ~  --version             show program's version number and exit
/ {; a6 P+ K3 I7 S  -h, --help            show this help message and exit
/ S$ `6 q9 z' g- u$ A# u1 i  -t TEMPLATE, --template=TEMPLATE
$ `' y; q# B  H+ S" s                        Template to use. Default is 'html_fancy'2 Z+ @# J6 |* e" g, F
  -i INVENTORY, --inventory=INVENTORY
8 {5 X/ @' s: S                        Inventory to read extra info from
1 {, P/ n6 X0 T: [* G* H3 O  -f, --fact-cache      <dir> contains fact-cache files
. {1 u2 n7 ^2 I& \! U  c  -p PARAMS, --params=PARAMS
" {; v8 G# |! t( N/ @                        Params to send to template# l; [$ P' B- T) V/ s% i0 o7 J
  -d, --debug           Show debug output
3 J( o3 y: T! D( c' ?0 h6 e9 e7 u0 w  -q, --quiet           Don't report warnings
6 a6 e" f/ N! X+ g; o- |# d  -c COLUMNS, --columns=COLUMNS
3 y2 N. Q& P* C' e8 y- C" \! Z* K                        Show only given columns( ~, t3 a1 Z4 o& k1 ^$ U* R( D
  -C CUST_COLS, --cust-cols=CUST_COLS
- _& x, W& y7 r# z' Q, \                        Path to a custom columns definition file; f+ S; L# W/ P$ {
  -l LIMIT, --limit=LIMIT
! [8 F+ x+ ?8 X0 J8 L/ W                        Limit hosts to pattern8 U% G3 i+ T$ l
  --exclude-cols=EXCLUDE_COLUMNS3 d; B2 f  I2 k: ]  N
                        Exclude cols from output
) V3 q; `0 v; \1 I3 s  F' ^. T# q[root@jumpserver3 ~]# pip install -I "ansible<2.9"
/ u  Z& k3 B" l) c9 F7 ^$ EWARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
- d3 h1 c, ]4 b4 O" s: }Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.% q' x9 D/ H( ?) g
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
: }- u5 s9 V. D( e, aCollecting ansible<2.9
* |9 w: R8 f1 G) F1 s: g  a9 M  Downloading ansible-2.8.20.tar.gz (12.7 MB)
+ ]0 S- u$ e- _- t. u8 e4 g* ^% [# O     |████████████████████████████████| 12.7 MB 4.5 kB/s : y! |+ S; k4 @: @
Collecting jinja2
9 g2 ?9 `- S. a  Downloading Jinja2-3.0.3-py3-none-any.whl (133 kB)
: R: U( E2 Y$ T* {1 Y+ J- R& r     |████████████████████████████████| 133 kB 1.8 MB/s
5 K, m' }: l- G4 J3 GCollecting PyYAML
' ]5 M. A  a9 ]- a, t  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), x9 b7 U2 q+ I  C1 J
Collecting cryptography
. H6 h) U+ G9 e: o( A  Downloading cryptography-40.0.0-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.7 MB)9 H) P4 u6 _% F! u6 M$ x
     |████████████████████████████████| 3.7 MB 54 kB/s % z4 x+ N. y( X
Collecting cffi>=1.12! |. i8 ?- H: C- d
  Downloading cffi-1.15.1-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (402 kB)% h- q) B. Z! ~% h
     |████████████████████████████████| 402 kB 1.8 MB/s
  r4 k7 M5 B" M+ H- N7 kCollecting pycparser6 n1 d' ^: ^0 Q! _& C4 G; L
  Downloading pycparser-2.21-py2.py3-none-any.whl (118 kB)
1 q$ J+ d1 g4 a7 T+ d1 r$ x     |████████████████████████████████| 118 kB 1.9 MB/s
2 w* X2 C- x& q4 [Collecting MarkupSafe>=2.0
+ u: t/ ?- f3 e9 a" b1 w2 ~6 T1 @7 Q  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)2 x; _! S9 s5 g; e" C/ ~! e7 X! t% U# o
Using legacy 'setup.py install' for ansible, since package 'wheel' is not installed.
& g; g2 ^7 k& H, gInstalling collected packages: pycparser, MarkupSafe, cffi, PyYAML, jinja2, cryptography, ansible* O3 @) m  R% m! P& i4 s: {
    Running setup.py install for ansible ... done" g8 s- K6 y& N" a. G
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.210 P& d# Y! Q1 I+ x8 t
WARNING: You are using pip version 20.3.4; however, version 21.3.1 is available./ l9 l' v! [; w( _) M- D6 D  \
You should consider upgrading via the '/usr/bin/python3 -m pip install --upgrade pip' command.6 z% w+ Y8 }/ A  L' b

4 \4 q  L7 x1 B6 w; t5 P0 q# q4 P( t" z  v' F  C$ c6 @  Q5 p1 s  M
[root@jumpserver3 ~]# pip list
0 ]1 [2 {5 d/ L6 r* zWARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
# v! b* N( y! s" oPlease see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
/ k5 w" x2 v3 S" d  j0 O6 ETo avoid this problem you can invoke Python with '-m pip' instead of running pip directly.$ i5 Q5 I3 B6 n% Q) a
Package            Version+ u. L2 t  |- C
------------------ --------
% Q- w0 h: G- f8 S- Z& eansible            2.8.202 w: N2 W" O' f; _( b4 {3 N
asgiref            3.4.10 }# w$ k* O  R% p) K9 I1 K
async-timeout      4.0.27 o: Y% b/ t3 O) y
cffi               1.15.1$ s& o: W. x" d7 M" Q3 ]5 I
cryptography       40.0.0+ D/ C4 g* o% G" t: [' N! y# i2 O3 E' O
Django             3.2.18
4 U2 n7 _' _9 ]* Hgmssl              3.2.2. H% v) I* t2 ^1 w; i6 F
importlib-metadata 4.8.34 v; p9 x' K" r$ [
Jinja2             3.0.3. D( I; B5 q$ z: f+ Z
MarkupSafe         2.0.1
0 Z. H  z; z: L1 n# L% N+ c2 O8 D  lpackaging          21.3
* d: q' l0 C6 o( }pip                20.3.4
7 x- w2 h0 ~) z# b! u2 R* Fpycparser          2.21
4 x' t" s. C1 ^6 h& hpycryptodomex      3.17# ?9 R+ m. V" y" ^3 K
pyparsing          3.0.9
7 Q. s' \) F& d2 D" I# J+ p! dpytz               2022.7.11 {0 e9 J) d  ~
PyYAML             6.0
. z  r9 F0 X" [. g: y* r, kredis              4.3.5* x6 R4 W, x' x; }4 d. c4 V
setuptools         39.2.0
0 M5 r4 F2 W$ vsqlparse           0.4.3  Z# V! P9 f5 Q' x9 W8 {; r
typing-extensions  4.1.1+ c+ ~1 O, a  p4 ?( p( }
zipp               3.6.0! {6 z9 v5 J5 K0 R/ i- e
WARNING: You are using pip version 20.3.4; however, version 21.3.1 is available.7 F, g- g. Q' e$ E, I
You should consider upgrading via the '/usr/bin/python3 -m pip install --upgrade pip' command.
$ j/ r9 b+ |8 k8 F[root@jumpserver3 ~]# ansible 4 K9 P/ G2 N% R) Q0 ^  ]' Z
/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.
3 d% P' M9 e$ Y/ W. c  @% C  from cryptography.exceptions import InvalidSignature' z, X6 J, n3 R( m
Usage: ansible <host-pattern> [options]
" H. R1 D( Y; A. j8 j( i- N: H
/ l& k+ ]  o5 ?7 zDefine and run a single task 'playbook' against a set of hosts1 S% L3 D* p! C! _

) \7 t0 O2 j' ^2 L7 p& I( tOptions:
0 ^9 J7 L( }% v  -a MODULE_ARGS, --args=MODULE_ARGS
- F& b6 L; I: ?0 r/ p- z' {                        module arguments
+ ~1 `2 A  [( \3 U) c% `  --ask-vault-pass      ask for vault password# a! I3 ^- m4 }
  -B SECONDS, --background=SECONDS
2 T  Q2 ]$ J5 h, }/ o9 e                        run asynchronously, failing after X seconds8 d6 v6 I/ q8 d- l& k
                        (default=N/A); J+ r5 `& U( u4 y
  -C, --check           don't make any changes; instead, try to predict some& g6 g) u9 ?. F0 i1 q( ]
                        of the changes that may occur' y9 V2 |; t2 J
  -D, --diff            when changing (small) files and templates, show the
2 v  r! ^6 |4 w  I. p                        differences in those files; works great with --check
# S4 U, D) ^4 r- w0 v+ {$ a* b9 z  -e EXTRA_VARS, --extra-vars=EXTRA_VARS3 b7 R, X) Q4 z* q' m: `0 N5 U
                        set additional variables as key=value or YAML/JSON, if
$ t$ V( }* D+ @/ n6 g                        filename prepend with @2 X. t' j& K9 R% _
  -f FORKS, --forks=FORKS
1 i' k; i4 p$ h( v! R                        specify number of parallel processes to use  G% k6 t' _1 y, i- Y# P
                        (default=5)
% C$ f* |; n$ z! C, w  -h, --help            show this help message and exit' h- A& w  D3 v$ v3 ^+ t! u* |4 f
  -i INVENTORY, --inventory=INVENTORY, --inventory-file=INVENTORY2 M1 G2 A# n! G& D& T3 g8 x; H
                        specify inventory host path or comma separated host
( z9 z9 Y3 {, a) n                        list. --inventory-file is deprecated
( }  H/ r! N8 m7 U8 p  -l SUBSET, --limit=SUBSET$ Q9 j' H9 i7 e2 f
                        further limit selected hosts to an additional pattern8 S" i5 Y5 k$ i
  --list-hosts          outputs a list of matching hosts; does not execute
; \  ]; X( d  e( F& z                        anything else: Y, d7 o1 [8 G( A6 b
  -m MODULE_NAME, --module-name=MODULE_NAME
2 N' Q1 G  d/ |5 T: g. v                        module name to execute (default=command)' I1 [. ]: {& E8 y4 N  i6 }; {
  -M MODULE_PATH, --module-path=MODULE_PATH
; ]% X6 S/ |$ b1 m. `- g+ V3 M                        prepend colon-separated path(s) to module library (def; a7 P' F. T4 z! }* D2 X
                        ault=~/.ansible/plugins/modules:/usr/share/ansible/plu; G5 |# r$ n; k; @6 r
                        gins/modules)4 F0 H- d& x, ^& E) Y; x$ @, {
  -o, --one-line        condense output1 {% Y/ D; ~) a) {% C
  --playbook-dir=BASEDIR
# x( ?/ R. b8 C- N7 k& m8 ]2 C                        Since this tool does not use playbooks, use this as a4 S$ u' d9 e7 n7 Q) d
                        substitute playbook directory.This sets the relative
7 P% W  {! W3 [) }                        path for many features including roles/ group_vars/
% Q9 ^% u4 n3 p( E/ Y8 V                        etc.
" r% J- I, l, V5 A5 G: y6 b- C) e, H  -P POLL_INTERVAL, --poll=POLL_INTERVAL4 V/ I2 D* o, j7 _8 }. n
                        set the poll interval if using -B (default=15)0 J: h- M- F8 s% ^- Z. Z3 j. n
  --syntax-check        perform a syntax check on the playbook, but do not5 f! Y0 o9 p& K! A* v5 h, d
                        execute it+ E- V+ G% c/ W* }0 \' |: G  G
  -t TREE, --tree=TREE  log output to this directory) i# w& M+ c" t+ D* K( J; e4 T
  --vault-id=VAULT_IDS  the vault identity to use  Q( [, x- O7 t. B, Q7 K
  --vault-password-file=VAULT_PASSWORD_FILES1 f% \' U7 z& U: u
                        vault password file& ~" e1 ?) u0 b3 S2 n) R0 ?
  -v, --verbose         verbose mode (-vvv for more, -vvvv to enable
( M( U* }. g  a9 D+ n) x" J! x                        connection debugging)+ y$ d  h5 W9 v; z- c/ ]
  --version             show program's version number, config file location,
# e* d- m7 |, K                        configured module search path, module location,. a/ N% b4 {+ i( e; g" G
                        executable location and exit1 j0 z/ c" C* s
! R/ s8 @' ?. S6 O: Y! l4 G" y* d9 z
  Privilege Escalation Options:
8 s( v% e; o, e" z    control how and which user you become as on target hosts
, r: `4 @9 z: W+ `- d) T
5 V! J' |, q' Q    -b, --become        run operations with become (does not imply password, ]  D. @3 Z4 Z% R  d
                        prompting). d" z" w) Y/ X$ j
    --become-method=BECOME_METHOD0 f5 H5 @, q. i5 T4 G8 Q( A- n
                        privilege escalation method to use (default=sudo), use% x& G/ l; S8 V3 w1 {& T
                        `ansible-doc -t become -l` to list valid choices.; [; S/ ]4 K! Y9 E+ K) j; O
    --become-user=BECOME_USER
& y2 o  s9 {" J8 t6 s/ z; u, V2 s                        run operations as this user (default=root)
, B; q5 Q* [; m" D( J! [    -K, --ask-become-pass0 `1 a) g/ u( W. y" y4 A
                        ask for privilege escalation password
4 w1 ?4 H" j2 \3 y' D0 o4 S3 b3 p" A. ~/ [# ]8 P) a4 h2 z
  Connection Options:. i5 r1 Q+ U" u4 ~! Z# l5 F- {) v" d
    control as whom and how to connect to hosts
  V6 Z% k/ h5 v+ b- L, l2 [6 l! B: o$ U' Q
    -k, --ask-pass      ask for connection password6 G- V( Z2 F' J7 {) s0 S
    --private-key=PRIVATE_KEY_FILE, --key-file=PRIVATE_KEY_FILE
2 ]+ ^0 p& Q$ M2 j( c5 W) O                        use this file to authenticate the connection& H8 F. ~8 r2 [0 V
    -u REMOTE_USER, --user=REMOTE_USER- [* c: c. h' ^  P' T& n' D
                        connect as this user (default=None)
, d- E4 v) L3 m- z  S    -c CONNECTION, --connection=CONNECTION1 Q9 o5 W  s  E3 _/ ]! t
                        connection type to use (default=smart)
. ~; W! z) K+ D# H    -T TIMEOUT, --timeout=TIMEOUT# D! M% D, h& R
                        override the connection timeout in seconds
! ^1 U7 ]% u* }                        (default=10)! J5 f+ p9 U; t. I8 q
    --ssh-common-args=SSH_COMMON_ARGS
% Y0 R, d' Y2 {- C9 ~$ Y2 [4 |+ v' d                        specify common arguments to pass to sftp/scp/ssh (e.g.3 M4 J* w+ U" U' x
                        ProxyCommand)
" F5 f/ y0 `& w# w5 r2 ~    --sftp-extra-args=SFTP_EXTRA_ARGS
$ i- q/ C$ d# i/ ?) `6 z* s                        specify extra arguments to pass to sftp only (e.g. -f,0 \! Z2 a' O5 b3 i
                        -l)2 ^9 h* b7 X  e- Y
    --scp-extra-args=SCP_EXTRA_ARGS4 v3 ?$ C" [2 c
                        specify extra arguments to pass to scp only (e.g. -l); [/ n  C) @" X% b. s& R
    --ssh-extra-args=SSH_EXTRA_ARGS8 i: Y9 v: T/ d0 s' {. ?  C, e
                        specify extra arguments to pass to ssh only (e.g. -R)
/ D6 {/ U. u2 c
  Z+ m% }/ u7 q: A3 D4 x, TSome modules do not make sense in Ad-Hoc (include, meta, etc)7 ?/ {: U& O  `% y8 U! A
ERROR! Missing target hosts" ]0 w+ X! |2 H& i% {' n: ^( ?8 P% _3 E
[root@jumpserver3 ~]#
' `6 ]3 N: W" }$ ^
$ E! Y3 i- h9 h! @7 O
5 p3 G% t; z1 d/ V1 ?
; D1 p7 `5 s( @5 f
5 Y) r% \  c1 @& }5 c

1

主题

0

回帖

12

积分

管理员

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

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

mkdir out
4 d" D; }1 v. E0 y- ]ansible -m setup --tree out/ all
- g: }# ~8 k& P: C) B7 K. z5 r# t0 J# c8 Z: k' r

接下来,在生成的 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

7 s. d. F$ h) r' z/ }, _3 @

默认情况下,使用 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 x6 N/ z! t; e  k0 }

读取清单是使用 -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:主机的外部唯一标识符。& [7 _# q0 s! Y2 c/ y1 u* m; w

例如,假设我们有以下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文件。/ j1 B# V+ b* L' p

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).1 h1 w! l7 P1 @' S. W8 ]# [; ~3 Z  _

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=...)。请参阅上面的示例。自选. q0 C4 W* O# s& x" v) J

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 s+ e1 _7 s; |2 v

如果要修改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/
( ]" r3 h- h6 R& ?' d9 \; a# u
, x9 l. H7 H: x  }

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2023-3-27 11:00:04 | 显示全部楼层
输出的 HTML 文件在其他计算机上不起作用。, B6 V& W0 L/ V* i2 i. M  t. U9 S. {
当您传输 ansible-cmdb 的输出 HTML 文件并尝试在另一台计算机上的浏览器中打开它时,您会发现它无法正常工作。1 O. x8 D6 G, {5 U

* F! \7 H- @0 B% X" l8 a( d$ G这是因为在本地计算机上打开的HTML文件(以 file:// url开头的文件)不允许从互联网上获取所需的Javascript文件(以 http:// 和 https:// 开头的URL)。因此,Ansible-cmdb 会在您安装 ansible-cmdb 时安装这些必需的文件。当然,另一台 PC 不会在本地提供这些文件。9 v. b( z7 Q4 Q! v" ^' K( R% U" N

& J  O: F, I1 w) `* ~( ~7 _解决方案是使用 -p local_js=0 参数生成输出,并将生成的 HTML 文件托管在某个地方的 Web 服务器上。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-6-12 00:45 , Processed in 0.017734 second(s), 22 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

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