易陆发现互联网技术论坛

 找回密码
 开始注册
查看: 546|回复: 7
收起左侧

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

[复制链接]
发表于 2023-3-27 11:00:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?开始注册

x
一、概述
: x) T+ x4 }6 v1 D' m% H( G7 QAnsible-cmdb 获取 Ansible 事实收集的输出并将其转换为包含系统配置信息的静态 HTML 概览页面(和其他内容)。6 a* T8 J% |3 `# w' c

: f% q$ c* v' }) J: ^' ~它支持多种类型的输出(html、csv、sql 等)并使用自定义数据扩展 Ansible 收集的信息。对于每个主机,它还显示组、主机变量、自定义变量和机器本地事实。$ q4 z0 X0 M( V% q. v/ V; J* B

1 V; r1 v9 @9 ~: j% Z' Y, s  h安装要求:/ O* O4 B4 B  b) w) Y% b( a

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

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

安装

[backcolor=rgba(1, 233, 175, 0.5)]Ansible-cmdb可以使用python包管理器pip进行安装。还有适用于各种 Linux 发行版的独立软件包。或者,您可以使用brew或普通的make install.

通过点

通过 Pip 安装:

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

通过 Pip 安装 Ansible-cmdb:

sudo pip install ansible-cmdb

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

" n! t) j% V$ e1 B( k7 W0 S
sudo pip install --upgrade ansible-cmdb
! |" S, i7 s$ p$ e; ^

1、pip安装

pip install ansible-cmdb

或者

pip install --upgrade ansible-cmdb

- n! [0 t: i# N  u2 R  i
0 o4 W2 a% D' ~* {  f1 j
brew install ansible-cmdb

[backcolor=rgba(1, 233, 175, 0.5)]


# W# s6 {& b2 z0 e. f6 h1 X$ T2 y8 _% b

其他操作系统安装:

tar -vxzf ansible-cmdb*.tar.gz
( o# w" ^% V. ycd ansible-cmdb*
" f+ S4 {1 e6 E0 y5 g; M
0 j! x3 z4 b! O+ {sudo make install


9 Z1 P0 Q9 i  \6 I' `. D

Installation from Git repository:

使用git方式安装:

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

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

* `3 X, _6 Y5 T1 {

; G. k8 D; t6 ^; T' B: h8 j

. r7 f: z* A+ h1 i: G* L! k

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


! o3 G7 e/ |, j$ }

下载安装包:

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

6 o- ]9 w% j2 ?2 H1 f3 H

: _1 `1 P, {* t) Y7 I+ i

cd ansible-cmdb

. u& h' ~% J; Y' `) [/ h

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

; X  T$ H0 o. b2 Y% L7 U

, n; p) O$ b' J& k
[root@jumpserver3 ansible-cmdb]# sudo bash -c ". build.sla && install "- [. ]5 V' F( w% O7 f# o
Installed in /usr/local/, x$ v* _. p# a$ l$ |

: |2 L* c, a. K0 h5 O$ c, V! y( Y& O' Y) b( ~
/ k; i7 \3 ^5 o. x9 v
- {% c9 ?5 E. d) |
 楼主| 发表于 2023-3-27 11:00:01 | 显示全部楼层
1、pip安装6 S/ b( _; A- a  l
pip install ansible-cmdb* f+ K9 H- F4 ?4 L+ H! Q/ g9 r
或者! Q& ]5 o4 u! v% w
pip install --upgrade ansible-cmdb' Y* R/ V1 |' g5 O+ {* D

/ W" F9 F- R) \. l2、下载安装包7 ]% p# _  y. i6 G
https://github.com/fboender/ansible-cmdb/' L0 L; y- l. H+ [/ u% C
wget https://github.com/fboender/ansi ... le-cmdb-1.17.tar.gz
% x' K, Y3 F& z0 }9 ttar -vxzf ansible-cmdb*.tar.gz
# N4 b& }$ h7 A8 K; a3 r# Dcd ansible-cmdb*- f$ m/ e1 P7 A
sudo make install
! I1 ~0 y2 l6 L- p2 F0 U, L0 Y$ v- U* e( D8 I8 E1 N8 k
3、git直接拉取' w: j: @' z8 h
git clone https://github.com/fboender/ansible-cmdb.git
/ X  V. q* G. W( g2 ocd ansible-cmdb# h3 i: i8 U' ~0 n2 j
sudo bash -c ". build.sla && install"4 r6 ^+ {1 W; h' u/ [, X& r9 M. r
 楼主| 发表于 2023-3-27 11:00:02 | 显示全部楼层
命令参数
7 A" K  e, `; y+ K登录后复制
( }4 q; P9 q; c0 R' R# Y$ GUsage: ansible-cmdb [option] <dir> > output.html( K9 N& N8 e+ F- M; W( p
Options:+ _* Y: m: W  c; \. B
  --version             show program's version number and exit+ b& G; Z+ t) g1 y
  -h, --help            show this help message and exit7 H- Z5 |8 g- T! B
  -t TEMPLATE, --template=TEMPLATE1 @8 p/ G9 y  b, s
                        Template to use. Default is 'html_fancy'8 F. G& ^' ?4 b9 b
  -i INVENTORY, --inventory=INVENTORY5 C4 g1 G8 h% W) g
                        Inventory to read extra info from
2 ?# B6 R2 p% o4 |; ~  -f, --fact-cache      <dir> contains fact-cache files) T9 M9 J- D) S) `
  -p PARAMS, --params=PARAMS
. {# e8 H" c( G( W) Z                        Params to send to template
5 G7 P5 }7 T: h- Z  -d, --debug           Show debug output" P/ a' Y7 l; M6 I; B
  -q, --quiet           Don't report warnings" X- z  r2 x. O+ T% _$ Y. B3 C. {: U
  -c COLUMNS, --columns=COLUMNS: G  ]% f# B$ |( M3 b, ]
                        Show only given columns7 N" |% w4 Z: W
  -C CUST_COLS, --cust-cols=CUST_COLS
8 O9 ^& ?% N% w8 y8 R& Q                        Path to a custom columns definition file4 X3 \% l5 ]8 U# \- O! X
  -l LIMIT, --limit=LIMIT
4 e+ b# I# D  y6 z4 p3 A+ M0 Q                        Limit hosts to pattern
, h7 |! ?9 o, a9 y$ L/ u  --exclude-cols=EXCLUDE_COLUMNS! p6 ?5 l- t- g0 |% ^" s
                        Exclude cols from output
/ ^1 P0 Z2 H+ f* |5 G) T5 x- N- U3 N

$ }3 |. ?! T% d- _+ ^- O1 v[root@jumpserver3 ansible-cmdb]# ansible-cmdb --help
: f# C8 _. m6 M  [Usage: ansible-cmdb.py [option] <dir> > output.html3 e. S! \! j  O3 S

7 O3 h" N) c4 V- ?8 [  K  |Options:
# L7 R  k# P, B6 c+ Y, e  --version             show program's version number and exit
( [7 ~5 E7 N4 c  b" Y0 v% `) @  -h, --help            show this help message and exit
" [) M% R) r, O' U# [& Y  -t TEMPLATE, --template=TEMPLATE
4 J, L! [. |; w1 ^2 h                        Template to use. Default is 'html_fancy'
* ?1 ~8 S" x& {6 t, q( c4 p# r  -i INVENTORY, --inventory=INVENTORY
9 F  _" R* t9 B0 ^* ?                        Inventory to read extra info from$ ^! D+ r, y8 |0 K# w
  -f, --fact-cache      <dir> contains fact-cache files
+ q) p' Q0 B7 Y6 `  -p PARAMS, --params=PARAMS- H  O7 \0 j0 O+ ^
                        Params to send to template
6 P1 w1 A7 b$ M# j: s5 b6 U% r  -d, --debug           Show debug output
, u0 @* Q- {2 y9 E6 F$ ?  -q, --quiet           Don't report warnings" W* \9 l( Y8 y" j  S9 O
  -c COLUMNS, --columns=COLUMNS# M% X" D* ^) I7 ]) R# r( V
                        Show only given columns, @" y: j& x. G. n6 q
  -C CUST_COLS, --cust-cols=CUST_COLS
7 r: {1 |. c  ^. ?% D2 g" H6 b: K2 q                        Path to a custom columns definition file" D# X  B+ d  c, a& m
  -l LIMIT, --limit=LIMIT0 ?- Y; S$ T: x# M& `* v' [
                        Limit hosts to pattern
" y5 G# I! h2 x' ?* V- d6 q6 V. W6 Y  --exclude-cols=EXCLUDE_COLUMNS
+ W4 i9 T9 y1 p& E# v, y                        Exclude cols from output9 R  T4 x& d) c) q/ X
[root@jumpserver3 ~]# pip install -I "ansible<2.9"
5 u) J0 G- C; p5 a: [6 Z1 mWARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.4 y2 }% s: F# l& A- r
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
# ~2 W/ m# M& n  J8 N$ ?7 S* STo avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
2 E- a4 m) d! q; u6 X( J7 w8 jCollecting ansible<2.97 _- H0 Z2 L6 o2 T: Z0 j
  Downloading ansible-2.8.20.tar.gz (12.7 MB)
5 [. {) S! H# |4 w0 l$ ]     |████████████████████████████████| 12.7 MB 4.5 kB/s , M( C& B: c4 Y6 P
Collecting jinja2
3 E  @5 l- P" Z1 t3 O# {: E( k: S  Downloading Jinja2-3.0.3-py3-none-any.whl (133 kB)1 p/ o: F; A+ m# y( H5 R* ~
     |████████████████████████████████| 133 kB 1.8 MB/s ) |  d7 I4 M0 h. Q9 A# v# b( U
Collecting PyYAML
% c* V- q  N3 b8 {, K7 T; P6 V  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)
% n" m4 D* V9 ?  u6 aCollecting cryptography# V( k& t$ v. L& w' O9 T
  Downloading cryptography-40.0.0-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.7 MB)1 ?' a1 d7 B) a! D/ A
     |████████████████████████████████| 3.7 MB 54 kB/s
4 u; J5 v+ Q8 z1 oCollecting cffi>=1.126 {6 K: u3 E& M" a. }) o
  Downloading cffi-1.15.1-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (402 kB)
/ t- M8 `3 z  ~  C2 y     |████████████████████████████████| 402 kB 1.8 MB/s
; N$ p' U) L( W9 P8 l1 vCollecting pycparser+ f/ l( w" N9 o0 U5 `
  Downloading pycparser-2.21-py2.py3-none-any.whl (118 kB)
* Q; @; I" u0 G! {1 e" c     |████████████████████████████████| 118 kB 1.9 MB/s
( O3 s- \# D! zCollecting MarkupSafe>=2.0# A5 z" O* U3 O1 i
  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)
% p+ s) ?* G, F- l3 z' _( h6 IUsing legacy 'setup.py install' for ansible, since package 'wheel' is not installed.0 F$ l. N# b9 w" r
Installing collected packages: pycparser, MarkupSafe, cffi, PyYAML, jinja2, cryptography, ansible
6 g- j- y: C0 x    Running setup.py install for ansible ... done
( C8 W, ?+ n8 G; Q, x4 y8 P: jSuccessfully 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" O; ~0 M& e# `
WARNING: You are using pip version 20.3.4; however, version 21.3.1 is available.& v$ v+ k5 s% f
You should consider upgrading via the '/usr/bin/python3 -m pip install --upgrade pip' command.' h9 d0 x/ k) o8 p* L" ?
/ g( v$ B* X$ m1 h

/ G9 n9 P  N# T0 T6 x/ L/ K[root@jumpserver3 ~]# pip list
7 Y7 w) Q1 |9 E3 o, \WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.7 G4 _. G$ s1 D7 A! j
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
& o0 z7 t5 c8 }) h" OTo avoid this problem you can invoke Python with '-m pip' instead of running pip directly.$ ~  |  L( F  S: I, E3 T) Z
Package            Version8 q$ x2 _- \6 A  V$ @9 v1 v# B
------------------ --------
" J# l( d% m" c7 Zansible            2.8.209 L  h- E% X! g7 K! I
asgiref            3.4.1
* j3 B2 O# R( kasync-timeout      4.0.2* x( [; o8 V2 D7 S& G
cffi               1.15.1
* y7 u+ d" J9 F' C$ tcryptography       40.0.0, a2 ]0 ^* D: i# k& T
Django             3.2.18
' n4 O: [% ]+ V. \6 X9 a! _gmssl              3.2.2
; ^! L$ A- c: O2 U4 E9 Z3 timportlib-metadata 4.8.3" U( q  ~2 B$ s( M8 {9 g7 H5 h
Jinja2             3.0.3- \* j8 M6 R: p" t: e
MarkupSafe         2.0.1
( k' f4 T4 U( Ipackaging          21.3
2 F4 a9 A: }8 l; B* Z: gpip                20.3.4
8 m( l% n5 H) [& c( Xpycparser          2.21
5 ]- @" g+ \7 J( s( i3 Fpycryptodomex      3.17+ j, n+ C. Y3 f& @
pyparsing          3.0.9
' k' ]* |+ X* `  ^  vpytz               2022.7.1
/ v( n$ v  Q; k5 [. C7 kPyYAML             6.07 R- k( k5 H# T7 A5 H; R0 e
redis              4.3.56 k; i; j  A! k, o% _! D1 v
setuptools         39.2.08 K; o: m( G. z# W
sqlparse           0.4.3
: k0 H* C9 q: f/ V  _, jtyping-extensions  4.1.12 k4 z. o" w8 m. a) ~+ B1 t( l
zipp               3.6.0
! _7 T$ [5 j$ e- j2 OWARNING: You are using pip version 20.3.4; however, version 21.3.1 is available.7 b+ T$ U$ F% @1 F' ^( r2 ?
You should consider upgrading via the '/usr/bin/python3 -m pip install --upgrade pip' command.
3 z! ^& O( g" Q1 s[root@jumpserver3 ~]# ansible
5 e% Z3 J! X, P6 e/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.
- N9 _0 ?" [# s: `0 _  from cryptography.exceptions import InvalidSignature
% p/ ], h6 G4 K$ nUsage: ansible <host-pattern> [options]# c. [2 D. `$ ~
  K# j, ]( ^- Y
Define and run a single task 'playbook' against a set of hosts
. }! _! g  V4 k5 o2 E" M
" _# m6 i& c/ Q  hOptions:
# H: V, k  G7 k# k5 k# ~9 T6 \  -a MODULE_ARGS, --args=MODULE_ARGS
1 {9 e& K# w! O$ ?6 `  J                        module arguments
( R. l: Y0 m) m( [& G( T4 o  --ask-vault-pass      ask for vault password
# V; ?: Q! j/ Q% V8 a  -B SECONDS, --background=SECONDS
" t' [# c& c' @                        run asynchronously, failing after X seconds9 z# u! ]4 n- K& n8 f1 H
                        (default=N/A), I& x7 ^: y1 S  Y" d  T
  -C, --check           don't make any changes; instead, try to predict some9 a  H7 K$ U0 g' ]
                        of the changes that may occur
+ u2 x/ z3 o$ B0 ^" q  -D, --diff            when changing (small) files and templates, show the
. E7 `5 w" y/ {) N- N. N                        differences in those files; works great with --check
3 _& e) t4 N0 ]- L  -e EXTRA_VARS, --extra-vars=EXTRA_VARS4 ^+ p% D) ~; D
                        set additional variables as key=value or YAML/JSON, if' `2 u* p! E) x& h# z) |1 X
                        filename prepend with @
5 q' I2 Z( r6 Y  -f FORKS, --forks=FORKS
) i  p$ _, O8 L3 a6 I1 f! c                        specify number of parallel processes to use
8 k" z: E# e1 g9 R9 z- l                        (default=5)
4 @7 i; M- d! P% q* z* x  -h, --help            show this help message and exit
. {- N' r8 Z: A1 F* ^( k  -i INVENTORY, --inventory=INVENTORY, --inventory-file=INVENTORY
% n/ J: B: k' U                        specify inventory host path or comma separated host
' ?; K! Z' R: y0 O/ I: u* i/ N3 d0 s( l                        list. --inventory-file is deprecated4 }1 T5 T- U5 ~
  -l SUBSET, --limit=SUBSET- `8 n8 F3 S5 M/ q; W( s
                        further limit selected hosts to an additional pattern
4 E1 e7 v0 w1 O2 u, F! o: Q9 l  --list-hosts          outputs a list of matching hosts; does not execute
! x+ Y  n( [+ h) \. I8 d                        anything else* f2 I7 w  v5 [; i1 y) C
  -m MODULE_NAME, --module-name=MODULE_NAME; L' ~8 C: l5 f7 O& {7 s; h
                        module name to execute (default=command)
* d9 Y. ~' n/ w6 k  -M MODULE_PATH, --module-path=MODULE_PATH3 ?, @% @; h  s5 F/ o  q/ i
                        prepend colon-separated path(s) to module library (def7 t) o" t+ W" q
                        ault=~/.ansible/plugins/modules:/usr/share/ansible/plu
& X* K& ~0 R% r6 Y# Z                        gins/modules)
+ E1 q9 B6 ~- ]2 ^( W! I  -o, --one-line        condense output
2 J4 P6 @: |, N; U  --playbook-dir=BASEDIR- x2 N% ?/ Y5 g( }; v
                        Since this tool does not use playbooks, use this as a6 |& \7 [  W& U" l/ r$ D1 A* h
                        substitute playbook directory.This sets the relative( y7 E$ I) N, F0 j7 u5 v
                        path for many features including roles/ group_vars/" |8 E; I& `. u- u4 \9 Y! b5 d
                        etc.9 X8 h* a" J* s3 f9 O5 B# t% {
  -P POLL_INTERVAL, --poll=POLL_INTERVAL3 {4 R3 Y- L# m; q- R
                        set the poll interval if using -B (default=15)% I- H2 h4 S6 r& x" I0 j
  --syntax-check        perform a syntax check on the playbook, but do not) r5 s- p. n/ E  K
                        execute it
! a4 w4 @' b& R  -t TREE, --tree=TREE  log output to this directory4 K' d; _/ C. Y! @
  --vault-id=VAULT_IDS  the vault identity to use
: b! D" D1 H% |0 `  --vault-password-file=VAULT_PASSWORD_FILES
, R% R! u2 j1 e1 l, J9 x8 `& [                        vault password file
1 s2 x* ^3 c- t7 F2 A6 E0 G6 {* m  -v, --verbose         verbose mode (-vvv for more, -vvvv to enable0 B1 u8 i: ?7 _1 L9 [& ]  o
                        connection debugging)0 ]: O( A$ h% H7 z: Z0 T9 p& D
  --version             show program's version number, config file location,. e* B# |! c  w+ v
                        configured module search path, module location,
; d6 C1 W, m) x$ d2 M0 }                        executable location and exit
$ g9 {% U' G3 U7 b* G" ?! ?! k2 c( N- h$ \: e& V" ?7 r
  Privilege Escalation Options:
8 n2 D9 r. _" a. I+ E1 M    control how and which user you become as on target hosts! \, k8 j. H. ~$ W+ ^7 E. ]

, R1 j) c' \8 D    -b, --become        run operations with become (does not imply password9 f7 ]  V2 T1 ^9 `# C$ r6 B+ w$ m
                        prompting)
) [, I8 ^5 @; L1 |! u2 g    --become-method=BECOME_METHOD1 m& C" O0 @! I/ x+ `
                        privilege escalation method to use (default=sudo), use
) f- a6 {2 K9 ^. |6 T! i                        `ansible-doc -t become -l` to list valid choices.9 Q' A$ X! \* k
    --become-user=BECOME_USER5 `) k! p# p$ x$ ~
                        run operations as this user (default=root)! M; k% J. {0 O9 q, l
    -K, --ask-become-pass
1 F9 C8 h' }2 \/ S8 |9 T3 X3 D                        ask for privilege escalation password/ W0 y4 \3 @3 Z
5 g4 W, d+ ]: s) \. ?. u8 u% G
  Connection Options:
' o- }5 k! }" D2 ~' f    control as whom and how to connect to hosts' z) j% D2 }% z$ x

7 _2 I3 b6 q5 t' j    -k, --ask-pass      ask for connection password! c3 u" O7 l% X9 b" r
    --private-key=PRIVATE_KEY_FILE, --key-file=PRIVATE_KEY_FILE
7 @$ V8 `" p5 b7 [/ e                        use this file to authenticate the connection" H8 h3 P- H$ O2 N5 G! i
    -u REMOTE_USER, --user=REMOTE_USER2 a" b0 D4 @2 b
                        connect as this user (default=None)' i0 Q4 L: |& p- T* U
    -c CONNECTION, --connection=CONNECTION' q8 h9 |( F  D) j7 z% E2 c0 R9 E
                        connection type to use (default=smart)
1 P6 n% g& D7 B* N: G$ f* t% g* c/ A    -T TIMEOUT, --timeout=TIMEOUT
; y: J+ Z) G0 d! O3 T* u, E                        override the connection timeout in seconds" [: R6 a) I" U1 ^/ R# t1 n
                        (default=10)
- K6 h  }9 O( m/ R6 U$ y1 d# p    --ssh-common-args=SSH_COMMON_ARGS3 \) y& R, l5 |# H4 K. g: M1 ]; ~
                        specify common arguments to pass to sftp/scp/ssh (e.g., l! R- ?/ S# i8 e+ f: [
                        ProxyCommand)
: ^( @2 k/ E1 b; K    --sftp-extra-args=SFTP_EXTRA_ARGS
3 m5 E4 N3 ]. {! x" V                        specify extra arguments to pass to sftp only (e.g. -f,
4 i+ ?8 Q2 a" K( S& g  p                        -l)
8 e# _% N/ J0 j- m( H  Y9 ~$ F: C- x; `    --scp-extra-args=SCP_EXTRA_ARGS
2 Z0 k; ]3 [* D1 C6 S) D" |                        specify extra arguments to pass to scp only (e.g. -l)1 r7 g% \  D# F! n, b  Q# G: f+ N
    --ssh-extra-args=SSH_EXTRA_ARGS
; z* z4 Y; y% b( U$ k5 Q& ^6 {                        specify extra arguments to pass to ssh only (e.g. -R)
# h% \* k! l' ^; X- g3 d* t+ ~
7 e( D) i! C# C1 v' e# Q- p" C- bSome modules do not make sense in Ad-Hoc (include, meta, etc)# G  ^6 X6 o# J  V& V
ERROR! Missing target hosts
/ e$ m/ u6 ^( n5 P[root@jumpserver3 ~]#
+ w9 Q2 z% B6 K9 M* o( ]/ F$ Q) L# |; v
! r# r/ P  I6 J* j9 K6 g6 D

$ `$ ~3 u3 t- L" b# F: m$ |% M. N( L1 {7 H; D( n
 楼主| 发表于 2023-3-27 11:00:03 | 显示全部楼层

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

mkdir out7 J: z* l1 n; w7 o- W
ansible -m setup --tree out/ all
2 g& U: L5 T$ E' K. l  l  b6 N4 b+ _3 i3 ~, d% f1 @3 V

接下来,在生成的 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
3 q' x" @& t/ u

默认情况下,使用 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”部分下的主机发现的事实中。. }' P! L, G$ g5 B& V/ M( |# 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:主机的外部唯一标识符。
    - n# t& G) z) E$ Y+ z

例如,假设我们有以下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文件。
    4 N4 w/ P1 U: W2 e- S

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).
    6 o/ e# d4 ~7 o% ]+ W7 T$ q

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=...)。请参阅上面的示例。自选! S$ H: d5 ^! J" B" F8 b7 E

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.html; r4 K9 R$ V+ z7 O8 b2 q

如果要修改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/

[backcolor=rgba(1, 233, 175, 0.5)]这个模板有点特别,因为它不是.tpl,而是Python脚本。您可以通过将 -t 参数指向该脚本来使用它:

    $ cd ~/mytemplate    $ ansible-cmdb -t ./html_fancy_split.py -i ~/ansible/hosts ~/ansible/out/
$ R; l9 n: T" o# M" K$ j( N
1 l# S4 b3 p% ]# S# S
 楼主| 发表于 2023-3-27 11:00:04 | 显示全部楼层
输出的 HTML 文件在其他计算机上不起作用。/ T* F$ v6 f! U) ^- |/ [- I
当您传输 ansible-cmdb 的输出 HTML 文件并尝试在另一台计算机上的浏览器中打开它时,您会发现它无法正常工作。3 A+ P) G4 I( p7 |

; Y) \: T' M9 ]3 Q; q这是因为在本地计算机上打开的HTML文件(以 file:// url开头的文件)不允许从互联网上获取所需的Javascript文件(以 http:// 和 https:// 开头的URL)。因此,Ansible-cmdb 会在您安装 ansible-cmdb 时安装这些必需的文件。当然,另一台 PC 不会在本地提供这些文件。
  A* C& l9 y( f5 r6 f* H0 w' L) D/ [3 F5 x  N0 |
解决方案是使用 -p local_js=0 参数生成输出,并将生成的 HTML 文件托管在某个地方的 Web 服务器上。
您需要登录后才可以回帖 登录 | 开始注册

本版积分规则

关闭

站长推荐上一条 /4 下一条

北京云银创陇科技有限公司以云计算运维,代码开发

QQ|返回首页|Archiver|小黑屋|易陆发现技术论坛 ( 蜀ICP备2026014127号-1 )点击这里给我发消息

GMT+8, 2026-4-8 21:39 , Processed in 0.067933 second(s), 22 queries .

Powered by Discuz! X3.4 Licensed

© 2012-2025 Discuz! Team.

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