| 1 |
Inventory |
| 2 |
========= |
| 3 |
|
| 4 |
Introduction |
| 5 |
------------ |
| 6 |
|
| 7 |
cdist comes with simple built-in tag based inventory. It is a simple inventory |
| 8 |
with list of hosts and a host has a list of tags. |
| 9 |
Inventory functionality is still in **beta** so it can be used only if beta |
| 10 |
command line flag is specified (-b, --beta) or setting CDIST_BETA env var. |
| 11 |
|
| 12 |
Description |
| 13 |
----------- |
| 14 |
|
| 15 |
The idea is to have simple tagging inventory. There is a list of hosts and for |
| 16 |
each host there are tags. Inventory database is a set of files under inventory |
| 17 |
database base directory. Filename equals hostname. Each file contains tags for |
| 18 |
hostname with each tag on its own line. |
| 19 |
|
| 20 |
Using inventory you can now configure hosts by selecting them by tags. |
| 21 |
|
| 22 |
Tags have no values, as tags are just tags. Tag name-value would in this |
| 23 |
context mean that host has two tags and it is selected by specifying that both |
| 24 |
tags are present. |
| 25 |
|
| 26 |
This inventory is **KISS** cdist built-in inventory database. You can maintain it |
| 27 |
using cdist inventory interface or using standard UNIX tools. |
| 28 |
|
| 29 |
cdist inventory interface |
| 30 |
------------------------- |
| 31 |
|
| 32 |
With cdist inventory interface you can list host(s) and tag(s), add host(s), |
| 33 |
add tag(s), delete host(s) and delete tag(s). |
| 34 |
|
| 35 |
Configuring hosts using inventory |
| 36 |
--------------------------------- |
| 37 |
|
| 38 |
config command now has new options, **-t**, **-a** and **-A**. |
| 39 |
|
| 40 |
**-A** means that all hosts in tag db is selected. |
| 41 |
|
| 42 |
**-a** means that selected hosts must contain ALL specified tags. |
| 43 |
|
| 44 |
**-t** means that host specifies tag - all hosts that have specified tags are |
| 45 |
selected. |
| 46 |
|
| 47 |
Examples |
| 48 |
-------- |
| 49 |
|
| 50 |
.. code-block:: sh |
| 51 |
|
| 52 |
# List inventory content |
| 53 |
$ cdist inventory list -b |
| 54 |
|
| 55 |
# List inventory for specified host localhost |
| 56 |
$ cdist inventory list -b localhost |
| 57 |
|
| 58 |
# List inventory for specified tag loadbalancer |
| 59 |
$ cdist inventory list -b -t loadbalancer |
| 60 |
|
| 61 |
# Add hosts to inventory |
| 62 |
$ cdist inventory add-host -b web1 web2 web3 |
| 63 |
|
| 64 |
# Delete hosts from file old-hosts from inventory |
| 65 |
$ cdist inventory del-host -b -f old-hosts |
| 66 |
|
| 67 |
# Add tags to specified hosts |
| 68 |
$ cdist inventory add-tag -b -t europe,croatia,web,static web1 web2 |
| 69 |
|
| 70 |
# Add tag to all hosts in inventory |
| 71 |
$ cdist inventory add-tag -b -t vm |
| 72 |
|
| 73 |
# Delete all tags from specified host |
| 74 |
$ cdist inventory del-tag -b -a localhost |
| 75 |
|
| 76 |
# Delete tags read from stdin from hosts specified by file hosts |
| 77 |
$ cdist inventory del-tag -b -T - -f hosts |
| 78 |
|
| 79 |
# Configure hosts from inventory with any of specified tags |
| 80 |
$ cdist config -b -t web dynamic |
| 81 |
|
| 82 |
# Configure hosts from inventory with all specified tags |
| 83 |
$ cdist config -b -t -a web dynamic |
| 84 |
|
| 85 |
# Configure all hosts from inventory db |
| 86 |
$ cdist config -b -A |
| 87 |
|
| 88 |
Example of manipulating database |
| 89 |
-------------------------------- |
| 90 |
|
| 91 |
.. code-block:: sh |
| 92 |
|
| 93 |
$ python3 scripts/cdist inventory list -b |
| 94 |
$ python3 scripts/cdist inventory add-host -b localhost |
| 95 |
$ python3 scripts/cdist inventory add-host -b test.mycloud.net |
| 96 |
$ python3 scripts/cdist inventory list -b |
| 97 |
localhost |
| 98 |
test.mycloud.net |
| 99 |
$ python3 scripts/cdist inventory add-host -b web1.mycloud.net web2.mycloud.net shell1.mycloud.net shell2.mycloud.net |
| 100 |
$ python3 scripts/cdist inventory list -b |
| 101 |
localhost |
| 102 |
test.mycloud.net |
| 103 |
web1.mycloud.net |
| 104 |
web2.mycloud.net |
| 105 |
shell1.mycloud.net |
| 106 |
shell2.mycloud.net |
| 107 |
$ python3 scripts/cdist inventory add-tag -b -t web web1.mycloud.net web2.mycloud.net |
| 108 |
$ python3 scripts/cdist inventory add-tag -b -t shell shell1.mycloud.net shell2.mycloud.net |
| 109 |
$ python3 scripts/cdist inventory add-tag -b -t cloud |
| 110 |
$ python3 scripts/cdist inventory list -b |
| 111 |
localhost cloud |
| 112 |
test.mycloud.net cloud |
| 113 |
web1.mycloud.net cloud,web |
| 114 |
web2.mycloud.net cloud,web |
| 115 |
shell1.mycloud.net cloud,shell |
| 116 |
shell2.mycloud.net cloud,shell |
| 117 |
$ python3 scripts/cdist inventory add-tag -b -t test,web,shell test.mycloud.net |
| 118 |
$ python3 scripts/cdist inventory list -b |
| 119 |
localhost cloud |
| 120 |
test.mycloud.net cloud,shell,test,web |
| 121 |
web1.mycloud.net cloud,web |
| 122 |
web2.mycloud.net cloud,web |
| 123 |
shell1.mycloud.net cloud,shell |
| 124 |
shell2.mycloud.net cloud,shell |
| 125 |
$ python3 scripts/cdist inventory del-tag -b -t shell test.mycloud.net |
| 126 |
$ python3 scripts/cdist inventory list -b |
| 127 |
localhost cloud |
| 128 |
test.mycloud.net cloud,test,web |
| 129 |
web1.mycloud.net cloud,web |
| 130 |
web2.mycloud.net cloud,web |
| 131 |
shell1.mycloud.net cloud,shell |
| 132 |
shell2.mycloud.net cloud,shell |
| 133 |
$ python3 scripts/cdist inventory add-tag -b -t all |
| 134 |
$ python3 scripts/cdist inventory add-tag -b -t mistake |
| 135 |
$ python3 scripts/cdist inventory list -b |
| 136 |
localhost all,cloud,mistake |
| 137 |
test.mycloud.net all,cloud,mistake,test,web |
| 138 |
web1.mycloud.net all,cloud,mistake,web |
| 139 |
web2.mycloud.net all,cloud,mistake,web |
| 140 |
shell1.mycloud.net all,cloud,mistake,shell |
| 141 |
shell2.mycloud.net all,cloud,mistake,shell |
| 142 |
$ python3 scripts/cdist inventory del-tag -b -t mistake |
| 143 |
$ python3 scripts/cdist inventory list -b |
| 144 |
localhost all,cloud |
| 145 |
test.mycloud.net all,cloud,test,web |
| 146 |
web1.mycloud.net all,cloud,web |
| 147 |
web2.mycloud.net all,cloud,web |
| 148 |
shell1.mycloud.net all,cloud,shell |
| 149 |
shell2.mycloud.net all,cloud,shell |
| 150 |
$ python3 scripts/cdist inventory del-host -b localhost |
| 151 |
$ python3 scripts/cdist inventory list -b |
| 152 |
test.mycloud.net all,cloud,test,web |
| 153 |
web1.mycloud.net all,cloud,web |
| 154 |
web2.mycloud.net all,cloud,web |
| 155 |
shell1.mycloud.net all,cloud,shell |
| 156 |
shell2.mycloud.net all,cloud,shell |
| 157 |
$ python3 scripts/cdist inventory list -b -t web |
| 158 |
test.mycloud.net all,cloud,test,web |
| 159 |
web1.mycloud.net all,cloud,web |
| 160 |
web2.mycloud.net all,cloud,web |
| 161 |
$ python3 scripts/cdist inventory list -b -t -a web test |
| 162 |
test.mycloud.net all,cloud,test,web |
| 163 |
$ python3 scripts/cdist inventory list -b -t -a web all |
| 164 |
test.mycloud.net all,cloud,test,web |
| 165 |
web1.mycloud.net all,cloud,web |
| 166 |
web2.mycloud.net all,cloud,web |
| 167 |
$ python3 scripts/cdist inventory list -b -t web all |
| 168 |
test.mycloud.net all,cloud,test,web |
| 169 |
web1.mycloud.net all,cloud,web |
| 170 |
web2.mycloud.net all,cloud,web |
| 171 |
shell1.mycloud.net all,cloud,shell |
| 172 |
shell2.mycloud.net all,cloud,shell |
| 173 |
$ cd cdist/inventory |
| 174 |
$ ls -1 |
| 175 |
shell1.mycloud.net |
| 176 |
shell2.mycloud.net |
| 177 |
test.mycloud.net |
| 178 |
web1.mycloud.net |
| 179 |
web2.mycloud.net |
| 180 |
$ ls -l |
| 181 |
total 20 |
| 182 |
-rw-r--r-- 1 darko darko 16 Jun 24 12:43 shell1.mycloud.net |
| 183 |
-rw-r--r-- 1 darko darko 16 Jun 24 12:43 shell2.mycloud.net |
| 184 |
-rw-r--r-- 1 darko darko 19 Jun 24 12:43 test.mycloud.net |
| 185 |
-rw-r--r-- 1 darko darko 14 Jun 24 12:43 web1.mycloud.net |
| 186 |
-rw-r--r-- 1 darko darko 14 Jun 24 12:43 web2.mycloud.net |
| 187 |
$ cat test.mycloud.net |
| 188 |
test |
| 189 |
all |
| 190 |
web |
| 191 |
cloud |
| 192 |
$ cat web2.mycloud.net |
| 193 |
all |
| 194 |
web |
| 195 |
cloud |
| 196 |
|
| 197 |
For more info about inventory commands and options see `cdist <man1/cdist.html>`_\ (1). |
| 198 |
|
| 199 |
Using external inventory |
| 200 |
------------------------ |
| 201 |
|
| 202 |
cdist can be used with any external inventory where external inventory is |
| 203 |
some storage or database from which you can get a list of hosts to configure. |
| 204 |
cdist can then be fed with this list of hosts through stdin or file using |
| 205 |
**-f** option. For example, if your host list is stored in sqlite3 database |
| 206 |
hosts.db and you want to select hosts which purpose is **django** then you |
| 207 |
can use it with cdist like: |
| 208 |
|
| 209 |
.. code-block:: sh |
| 210 |
|
| 211 |
$ sqlite3 hosts.db "select hostname from hosts where purpose = 'django';" | cdist config |