1
0

pillar.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. .. _pillar-walk-through:
  2. ==================
  3. Pillar Walkthrough
  4. ==================
  5. .. note::
  6. This walkthrough assumes that the reader has already completed the initial
  7. Salt :ref:`walkthrough <tutorial-salt-walk-through>`.
  8. Pillars are tree-like structures of data defined on the Salt Master and passed
  9. through to minions. They allow confidential, targeted data to be securely sent
  10. only to the relevant minion.
  11. .. note::
  12. Grains and Pillar are sometimes confused, just remember that Grains
  13. are data about a minion which is stored or generated from the minion.
  14. This is why information like the OS and CPU type are found in Grains.
  15. Pillar is information about a minion or many minions stored or generated
  16. on the Salt Master.
  17. Pillar data is useful for:
  18. Highly Sensitive Data:
  19. Information transferred via pillar is guaranteed to only be presented to
  20. the minions that are targeted, making Pillar suitable
  21. for managing security information, such as cryptographic keys and
  22. passwords.
  23. Minion Configuration:
  24. Minion modules such as the execution modules, states, and returners can
  25. often be configured via data stored in pillar.
  26. Variables:
  27. Variables which need to be assigned to specific minions or groups of
  28. minions can be defined in pillar and then accessed inside sls formulas
  29. and template files.
  30. Arbitrary Data:
  31. Pillar can contain any basic data structure in dictionary format,
  32. so a key/value store can be defined making it easy to iterate over a group
  33. of values in sls formulas.
  34. Pillar is therefore one of the most important systems when using Salt. This
  35. walkthrough is designed to get a simple Pillar up and running in a few minutes
  36. and then to dive into the capabilities of Pillar and where the data is
  37. available.
  38. Setting Up Pillar
  39. =================
  40. The pillar is already running in Salt by default. To see the minion's
  41. pillar data:
  42. .. code-block:: bash
  43. salt '*' pillar.items
  44. .. note::
  45. Prior to version 0.16.2, this function is named ``pillar.data``. This
  46. function name is still supported for backwards compatibility.
  47. By default, the contents of the master configuration file are not loaded into
  48. pillar for all minions. This default is stored in the ``pillar_opts`` setting,
  49. which defaults to ``False``.
  50. The contents of the master configuration file can be made available to minion
  51. pillar files. This makes global configuration of services and systems very easy,
  52. but note that this may not be desired or appropriate if sensitive data is stored
  53. in the master's configuration file. To enable the master configuration file to be
  54. available to a minion's pillar files, set ``pillar_opts`` to ``True`` in the
  55. minion configuration file.
  56. Similar to the state tree, the pillar is comprised of sls files and has a top file.
  57. The default location for the pillar is in /srv/pillar.
  58. .. note::
  59. The pillar location can be configured via the ``pillar_roots`` option inside
  60. the master configuration file. It must not be in a subdirectory of the state
  61. tree or file_roots. If the pillar is under file_roots, any pillar targeting
  62. can be bypassed by minions.
  63. To start setting up the pillar, the /srv/pillar directory needs to be present:
  64. .. code-block:: bash
  65. mkdir /srv/pillar
  66. Now create a simple top file, following the same format as the top file used for
  67. states:
  68. ``/srv/pillar/top.sls``:
  69. .. code-block:: yaml
  70. base:
  71. '*':
  72. - data
  73. This top file associates the data.sls file to all minions. Now the
  74. ``/srv/pillar/data.sls`` file needs to be populated:
  75. ``/srv/pillar/data.sls``:
  76. .. code-block:: yaml
  77. info: some data
  78. To ensure that the minions have the new pillar data, issue a command
  79. to them asking that they fetch their pillars from the master:
  80. .. code-block:: bash
  81. salt '*' saltutil.refresh_pillar
  82. Now that the minions have the new pillar, it can be retrieved:
  83. .. code-block:: bash
  84. salt '*' pillar.items
  85. The key ``info`` should now appear in the returned pillar data.
  86. More Complex Data
  87. ~~~~~~~~~~~~~~~~~
  88. Unlike states, pillar files do not need to define :strong:`formulas`.
  89. This example sets up user data with a UID:
  90. ``/srv/pillar/users/init.sls``:
  91. .. code-block:: yaml
  92. users:
  93. thatch: 1000
  94. shouse: 1001
  95. utahdave: 1002
  96. redbeard: 1003
  97. .. note::
  98. The same directory lookups that exist in states exist in pillar, so the
  99. file ``users/init.sls`` can be referenced with ``users`` in the :term:`top
  100. file <Top File>`.
  101. The top file will need to be updated to include this sls file:
  102. ``/srv/pillar/top.sls``:
  103. .. code-block:: yaml
  104. base:
  105. '*':
  106. - data
  107. - users
  108. Now the data will be available to the minions. To use the pillar data in a
  109. state, you can use Jinja:
  110. ``/srv/salt/users/init.sls``
  111. .. code-block:: jinja
  112. {% for user, uid in pillar.get('users', {}).items() %}
  113. {{user}}:
  114. user.present:
  115. - uid: {{uid}}
  116. {% endfor %}
  117. This approach allows for users to be safely defined in a pillar and then the
  118. user data is applied in an sls file.
  119. Parameterizing States With Pillar
  120. =================================
  121. Pillar data can be accessed in state files to customise behavior for each
  122. minion. All pillar (and grain) data applicable to each minion is substituted
  123. into the state files through templating before being run. Typical uses
  124. include setting directories appropriate for the minion and skipping states
  125. that don't apply.
  126. A simple example is to set up a mapping of package names in pillar for
  127. separate Linux distributions:
  128. ``/srv/pillar/pkg/init.sls``:
  129. .. code-block:: jinja
  130. pkgs:
  131. {% if grains['os_family'] == 'RedHat' %}
  132. apache: httpd
  133. vim: vim-enhanced
  134. {% elif grains['os_family'] == 'Debian' %}
  135. apache: apache2
  136. vim: vim
  137. {% elif grains['os'] == 'Arch' %}
  138. apache: apache
  139. vim: vim
  140. {% endif %}
  141. The new ``pkg`` sls needs to be added to the top file:
  142. ``/srv/pillar/top.sls``:
  143. .. code-block:: yaml
  144. base:
  145. '*':
  146. - data
  147. - users
  148. - pkg
  149. Now the minions will auto map values based on respective operating systems
  150. inside of the pillar, so sls files can be safely parameterized:
  151. ``/srv/salt/apache/init.sls``:
  152. .. code-block:: jinja
  153. apache:
  154. pkg.installed:
  155. - name: {{ pillar['pkgs']['apache'] }}
  156. Or, if no pillar is available a default can be set as well:
  157. .. note::
  158. The function ``pillar.get`` used in this example was added to Salt in
  159. version 0.14.0
  160. ``/srv/salt/apache/init.sls``:
  161. .. code-block:: jinja
  162. apache:
  163. pkg.installed:
  164. - name: {{ salt['pillar.get']('pkgs:apache', 'httpd') }}
  165. In the above example, if the pillar value ``pillar['pkgs']['apache']`` is not
  166. set in the minion's pillar, then the default of ``httpd`` will be used.
  167. .. note::
  168. Under the hood, pillar is just a Python dict, so Python dict methods such
  169. as ``get`` and ``items`` can be used.
  170. Pillar Makes Simple States Grow Easily
  171. ======================================
  172. One of the design goals of pillar is to make simple sls formulas easily grow
  173. into more flexible formulas without refactoring or complicating the states.
  174. A simple formula:
  175. ``/srv/salt/edit/vim.sls``:
  176. .. code-block:: yaml
  177. vim:
  178. pkg.installed: []
  179. /etc/vimrc:
  180. file.managed:
  181. - source: salt://edit/vimrc
  182. - mode: 644
  183. - user: root
  184. - group: root
  185. - require:
  186. - pkg: vim
  187. Can be easily transformed into a powerful, parameterized formula:
  188. ``/srv/salt/edit/vim.sls``:
  189. .. code-block:: jinja
  190. vim:
  191. pkg.installed:
  192. - name: {{ pillar['pkgs']['vim'] }}
  193. /etc/vimrc:
  194. file.managed:
  195. - source: {{ pillar['vimrc'] }}
  196. - mode: 644
  197. - user: root
  198. - group: root
  199. - require:
  200. - pkg: vim
  201. Where the vimrc source location can now be changed via pillar:
  202. ``/srv/pillar/edit/vim.sls``:
  203. .. code-block:: jinja
  204. {% if grains['id'].startswith('dev') %}
  205. vimrc: salt://edit/dev_vimrc
  206. {% elif grains['id'].startswith('qa') %}
  207. vimrc: salt://edit/qa_vimrc
  208. {% else %}
  209. vimrc: salt://edit/vimrc
  210. {% endif %}
  211. Ensuring that the right vimrc is sent out to the correct minions.
  212. The pillar top file must include a reference to the new sls pillar file:
  213. ``/srv/pillar/top.sls``:
  214. .. code-block:: yaml
  215. base:
  216. '*':
  217. - pkg
  218. - edit.vim
  219. Setting Pillar Data on the Command Line
  220. =======================================
  221. Pillar data can be set on the command line when running :py:func:`state.apply
  222. <salt.modules.state.apply_` like so:
  223. .. code-block:: bash
  224. salt '*' state.apply pillar='{"foo": "bar"}'
  225. salt '*' state.apply my_sls_file pillar='{"hello": "world"}'
  226. Nested pillar values can also be set via the command line:
  227. .. code-block:: bash
  228. salt '*' state.sls my_sls_file pillar='{"foo": {"bar": "baz"}}'
  229. Lists can be passed via command line pillar data as follows:
  230. .. code-block:: bash
  231. salt '*' state.sls my_sls_file pillar='{"some_list": ["foo", "bar", "baz"]}'
  232. .. note::
  233. If a key is passed on the command line that already exists on the minion,
  234. the key that is passed in will overwrite the entire value of that key,
  235. rather than merging only the specified value set via the command line.
  236. The example below will swap the value for vim with telnet in the previously
  237. specified list, notice the nested pillar dict:
  238. .. code-block:: bash
  239. salt '*' state.apply edit.vim pillar='{"pkgs": {"vim": "telnet"}}'
  240. This will attempt to install telnet on your minions, feel free to
  241. uninstall the package or replace telnet value with anything else.
  242. .. note::
  243. Be aware that when sending sensitive data via pillar on the command-line
  244. that the publication containing that data will be received by all minions
  245. and will not be restricted to the targeted minions. This may represent
  246. a security concern in some cases.
  247. More On Pillar
  248. ==============
  249. Pillar data is generated on the Salt master and securely distributed to
  250. minions. Salt is not restricted to the pillar sls files when defining the
  251. pillar but can retrieve data from external sources. This can be useful when
  252. information about an infrastructure is stored in a separate location.
  253. Reference information on pillar and the external pillar interface can be found
  254. in the Salt documentation:
  255. :ref:`Pillar <pillar>`
  256. Minion Config in Pillar
  257. =======================
  258. Minion configuration options can be set on pillars. Any option that you want
  259. to modify, should be in the first level of the pillars, in the same way you set
  260. the options in the config file. For example, to configure the MySQL root
  261. password to be used by MySQL Salt execution module:
  262. .. code-block:: yaml
  263. mysql.pass: hardtoguesspassword
  264. This is very convenient when you need some dynamic configuration change that
  265. you want to be applied on the fly. For example, there is a chicken and the egg
  266. problem if you do this:
  267. .. code-block:: yaml
  268. mysql-admin-passwd:
  269. mysql_user.present:
  270. - name: root
  271. - password: somepasswd
  272. mydb:
  273. mysql_db.present
  274. The second state will fail, because you changed the root password and the
  275. minion didn't notice it. Setting mysql.pass in the pillar, will help to sort
  276. out the issue. But always change the root admin password in the first place.
  277. This is very helpful for any module that needs credentials to apply state
  278. changes: mysql, keystone, etc.