@@ -196,7 +196,7 @@ The `sql` template generates an .sql file that can be loaded into an SQLite or
196
196
MySQL database.
197
197
198
198
$ ansible-cmdb -t sql -i hosts out > cmdb.sql
199
- $ echo "CREATE DATABASE ansiblecmdb" | mysql
199
+ $ echo "CREATE DATABASE ansiblecmdb" | mysql
200
200
$ mysql ansiblecmdb < cmdb.sql
201
201
202
202
## Fact caching
@@ -432,54 +432,98 @@ The software items will be listed under the "*Custom facts*" heading.
432
432
433
433
You can add custom columns to the host overview with the ` -C ` (` --cust-cols ` )
434
434
option. This allows you to specify
435
- [ jsonxs] ( https://github.com/fboender/jsonxs ) expressions to extract and
436
- display custom host facts. Such columns are fairly limited in what they can
437
- display. If you need a more powerful method of adding custom data to your
438
- CMDB, please refer to the [ Custom templates] ( #custom-templates ) section.
435
+ [ jsonxs] ( https://github.com/fboender/jsonxs ) expressions or [ Mako
436
+ template] ( https://www.makotemplates.org/ ) fragments to extract and
437
+ display custom host facts.
439
438
440
439
Custom columns are currently only supported by the ` html_fancy ` and
441
- ` html_fancy_split ` templates!
440
+ ` html_fancy_split ` templates.
442
441
443
- The ` -C ` option takes a parameter which is the path to a JSON file containing
444
- your custom column definitions. An example can be found in the
445
- ` examples/cust_cols.json ` file in the repo:
442
+ The ` -C ` option takes a parameter which is the path to a file containing
443
+ your custom column definitions. The file's syntax is Python (even though it
444
+ looks like JSON). An example can be found in the
445
+ ` examples/cust_cols.conf ` file in the repo:
446
446
447
447
[
448
+ # Show whether AppArmor is enabled
448
449
{
449
450
"title": "AppArmor",
450
451
"id": "apparmor",
451
452
"sType": "string",
452
- "visible": true ,
453
+ "visible": False ,
453
454
"jsonxs": "ansible_facts.ansible_apparmor.status"
454
455
},
456
+ # Show the nameservers configured on the host
455
457
{
456
- "title": "Proc type ",
457
- "id": "proctype ",
458
+ "title": "Nameservers ",
459
+ "id": "nameservers ",
458
460
"sType": "string",
459
- "visible": true,
460
- "jsonxs": "ansible_facts.ansible_processor[2]"
461
+ "visible": True,
462
+ "tpl": """
463
+ <ul>
464
+ <%
465
+ # Get ansible_facts.ansible_dns.nameservers
466
+ facts = host.get('ansible_facts', {})
467
+ dns = facts.get('ansible_dns', {})
468
+ nameservers = dns.get('nameservers', [])
469
+ %>
470
+ % for nameserver in nameservers:
471
+ <li>${nameserver}</li>
472
+ % endfor
473
+ </ul>
474
+ """
475
+ },
476
+ # Show the nameservers configured on the host, but use jsonxs.
477
+ {
478
+ "title": "Nameservers2",
479
+ "id": "nameservers2",
480
+ "sType": "string",
481
+ "visible": True,
482
+ "tpl": """
483
+ <ul>
484
+ <%
485
+ # Get ansible_facts.ansible_dns.nameservers using jsonxs
486
+ nameservers = jsonxs(host, 'ansible_facts.ansible_dns.nameservers', default=[])
487
+ %>
488
+ % for nameserver in nameservers:
489
+ <li>${nameserver}</li>
490
+ % endfor
491
+ </ul>
492
+ """
461
493
}
462
494
]
463
495
464
- This defines two new columns: 'AppArmor' and 'Proc type '. All keys are
465
- required.
496
+ This defines two new columns: 'AppArmor' and 'Nameservers '. Each column
497
+ consist of the following key/values:
466
498
467
- * ` title ` is what is displayed as the columns user-friendly title.
468
- * The ` id ` key must have a unique value, to differentiate between
469
- columns.
499
+ * ` title ` is what is displayed as the columns user-friendly title. ** Required ** .
500
+ * The ` id ` key must have a unique value, to differentiate between columns.
501
+ ** Required **
470
502
* The ` sType ` value determines how the values will be sorted in the host
471
- overview. Possible values include ` string ` and ` num ` .
503
+ overview. Possible values include ` string ` and ` num ` . ** Required **
472
504
* ` visible ` determines whether the column will be active (shown) by default.
473
- * The ` jsonxs ` expression points to an entry in the facts files for each host,
474
- and determines what will be shown for the column's value for each host.
475
- The easiest way to figure out a jsonxs expression is by opening one of the
476
- gathered facts files in a json editor. Please see
505
+ ** Required**
506
+ * The ` jsonxs ` expression, if specified points to an entry in the facts files
507
+ for each host, and determines what will be shown for the column's value for
508
+ each host. The easiest way to figure out a jsonxs expression is by opening
509
+ one of the gathered facts files in a json editor. Please see
477
510
[ jsonxs] ( https://github.com/fboender/jsonxs ) for info on how to write jsonxs
478
- expressions.
511
+ expressions. ** Optional**
512
+ * The ` tpl ` expression, if specified, is a [ Mako] ( http://www.makotemplates.org/ )
513
+ template fragment. A single variable ` host ` is made available in this
514
+ template. Care must be taken when accessing host information. If one of the
515
+ hosts is missing the information you're trying to access, the template will
516
+ not render and ansible-cmdb will crash (usually with a 'KeyError' message).
517
+ You should always use the ` get() ` method and specify a default value. E.g.
518
+ `host.get('ansible_facts', {}).get('ansible_dns', {}).get('nameservers',
519
+ [ ] )` . Alternatively (and recommended) is that you use ` jsonxs` to access
520
+ your info (and specify ` default=... ` ). See the example above. ** Optional**
521
+
522
+ Either ` jsonxs ` or ` tpl ` is required.
479
523
480
524
To use it:
481
525
482
- ../ ansible-cmdb/src/ansible-cmdb -C example/cust_cols.json -i example/hosts example/out/ > cmdb.html
526
+ ansible-cmdb -C example/cust_cols.conf -i example/hosts example/out/ > cmdb.html
483
527
484
528
When opening the ` cmdb.html ` file in your browser, you may have to hit the
485
529
'Clear settings' button in the top-right before the new columns show up or
@@ -488,14 +532,13 @@ when you get strange behaviour.
488
532
489
533
## Custom templates
490
534
491
- Custom columns can be added with the ` -C ` param. See the [ Custom
492
- columns] ( #custom-columns ) section for more info. Custom columns are somewhat
493
- limited in the type of information they can display (basically only strings
494
- and numbers). If you want to add more elaborate custom columns or other data
495
- to the output, you can create a custom template. Ansible-cmdb uses the [ Mako
496
- templating engine] ( http://www.makotemplates.org/ ) to render output.
535
+ It's possible to create custom templates to build completely different CMDBs
536
+ or to enhance the existing ones. Ansible-cmdb uses the [ Mako templating
537
+ engine] ( http://www.makotemplates.org/ ) to render output.
497
538
498
- For example, if you want to add a custom column to the ` html_fancy ` template:
539
+ For example, if you want to add a custom column to the ` html_fancy ` template
540
+ (note that it's easier to just use the ` --cust-cols ` option. For more info see
541
+ above):
499
542
500
543
1 . Make a copy of the default ` html_fancy ` template in a new dir. Here, we'll
501
544
use files from the ansible-cmdb git repository.
0 commit comments