requisites.rst 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  1. .. _requisites:
  2. ===========================================
  3. Requisites and Other Global State Arguments
  4. ===========================================
  5. Requisites
  6. ==========
  7. The Salt requisite system is used to create relationships between states. The
  8. core idea being that, when one state is dependent somehow on another, that
  9. inter-dependency can be easily defined. These dependencies are expressed by
  10. declaring the relationships using state names and ID's or names. The
  11. generalized form of a requisite target is ``<state name> : <ID or name>``.
  12. The specific form is defined as a :ref:`Requisite Reference
  13. <requisite-reference>`
  14. Requisites come in two types: Direct requisites (such as ``require``),
  15. and requisite_ins (such as ``require_in``). The relationships are
  16. directional: a direct requisite requires something from another state.
  17. However, a requisite_in inserts a requisite into the targeted state pointing to
  18. the targeting state. The following example demonstrates a direct requisite:
  19. .. code-block:: yaml
  20. vim:
  21. pkg.installed
  22. /etc/vimrc:
  23. file.managed:
  24. - source: salt://edit/vimrc
  25. - require:
  26. - pkg: vim
  27. In the example above, the file ``/etc/vimrc`` depends on the vim package.
  28. Requisite_in statements are the opposite. Instead of saying "I depend on
  29. something", requisite_ins say "Someone depends on me":
  30. .. code-block:: yaml
  31. vim:
  32. pkg.installed:
  33. - require_in:
  34. - file: /etc/vimrc
  35. /etc/vimrc:
  36. file.managed:
  37. - source: salt://edit/vimrc
  38. So here, with a requisite_in, the same thing is accomplished as in the first
  39. example, but the other way around. The vim package is saying "/etc/vimrc depends
  40. on me". This will result in a ``require`` being inserted into the
  41. ``/etc/vimrc`` state which targets the ``vim`` state.
  42. In the end, a single dependency map is created and everything is executed in a
  43. finite and predictable order.
  44. Requisite matching
  45. ------------------
  46. Requisites need two pieces of information for matching: The state module name –
  47. e.g. ``pkg`` –, and the identifier – e.g. vim –, which can be either the ID (the
  48. first line in the stanza) or the ``- name`` parameter.
  49. .. code-block:: yaml
  50. - require:
  51. - pkg: vim
  52. Omitting state module in requisites
  53. -----------------------------------
  54. .. versionadded:: 2016.3.0
  55. In version 2016.3.0, the state module name was made optional. If the state module
  56. is omitted, all states matching the ID will be required, regardless of which
  57. module they are using.
  58. .. code-block:: yaml
  59. - require:
  60. - vim
  61. State target matching
  62. ~~~~~~~~~~~~~~~~~~~~~
  63. In order to understand how state targets are matched, it is helpful to know
  64. :ref:`how the state compiler is working <compiler-ordering>`. Consider the following
  65. example:
  66. .. code-block:: yaml
  67. Deploy server package:
  68. file.managed:
  69. - name: /usr/local/share/myapp.tar.xz
  70. - source: salt://myapp.tar.xz
  71. Extract server package:
  72. archive.extracted:
  73. - name: /usr/local/share/myapp
  74. - source: /usr/local/share/myapp.tar.xz
  75. - archive_format: tar
  76. - onchanges:
  77. - file: Deploy server package
  78. The first formula is converted to a dictionary which looks as follows (represented
  79. as YAML, some properties omitted for simplicity) as `High Data`:
  80. .. code-block:: yaml
  81. Deploy server package:
  82. file:
  83. - managed
  84. - name: /usr/local/share/myapp.tar.xz
  85. - source: salt://myapp.tar.xz
  86. The ``file.managed`` format used in the formula is essentially syntactic sugar:
  87. at the end, the target is ``file``, which is used in the ``Extract server package``
  88. state above.
  89. Identifier matching
  90. ~~~~~~~~~~~~~~~~~~~
  91. Requisites match on both the ID Declaration and the ``name`` parameter.
  92. This means that, in the "Deploy server package" example above, a ``require``
  93. requisite would match with ``Deploy server package`` *or* ``/usr/local/share/myapp.tar.xz``,
  94. so either of the following versions for "Extract server package" works:
  95. .. code-block:: yaml
  96. # (Archive arguments omitted for simplicity)
  97. # Match by ID declaration
  98. Extract server package:
  99. archive.extracted:
  100. - onchanges:
  101. - file: Deploy server package
  102. # Match by name parameter
  103. Extract server package:
  104. archive.extracted:
  105. - onchanges:
  106. - file: /usr/local/share/myapp.tar.xz
  107. Requisite overview
  108. ~~~~~~~~~~~~~~~~~~
  109. +------------+-------------------+---------------+------------+--------------------+
  110. | name | state is only | state is only | order | comment |
  111. | of | executed if | executed if | | or |
  112. | | target execution | target has | 1.target | |
  113. | | | | 2.state | |
  114. | requisite | result is | changes | (default) | description |
  115. +============+===================+===============+============+====================+
  116. | require | success | | default | state will always |
  117. | | | | | execute unless |
  118. | | | | | target fails |
  119. +------------+-------------------+---------------+------------+--------------------+
  120. | watch | success | | default | like require, |
  121. | | | | | but adds additional|
  122. | | | | | behaviour |
  123. | | | | | (mod_watch) |
  124. +------------+-------------------+---------------+------------+--------------------+
  125. | prereq | success | has changes | switched | like onchanges, |
  126. | | | (run | | except order |
  127. | | | individually | | |
  128. | | | as dry-run) | | |
  129. +------------+-------------------+---------------+------------+--------------------+
  130. | onchanges | success | has changes | default | execute state if |
  131. | | | | | target execution |
  132. | | | | | result is success |
  133. | | | | | and target has |
  134. | | | | | changes |
  135. +------------+-------------------+---------------+------------+--------------------+
  136. | onfail | failed | | default | Only requisite |
  137. | | | | | where state exec. |
  138. | | | | | if target fails |
  139. +------------+-------------------+---------------+------------+--------------------+
  140. In this table, the following short form of terms is used:
  141. * **state** (= dependent state): state containing requisite
  142. * **target** (= state target) : state referenced by requisite
  143. Direct Requisite and Requisite_in types
  144. ---------------------------------------
  145. There are several direct requisite statements that can be used in Salt:
  146. * ``require``
  147. * ``watch``
  148. * ``prereq``
  149. * ``use``
  150. * ``onchanges``
  151. * ``onfail``
  152. Each direct requisite also has a corresponding requisite_in:
  153. * ``require_in``
  154. * ``watch_in``
  155. * ``prereq_in``
  156. * ``use_in``
  157. * ``onchanges_in``
  158. * ``onfail_in``
  159. There are several corresponding requisite_any statements:
  160. * ``require_any``
  161. * ``watch_any``
  162. * ``onchanges_any``
  163. * ``onfail_any``
  164. All of the requisites define specific relationships and always work with the
  165. dependency logic defined above.
  166. .. _requisites-require:
  167. require
  168. ~~~~~~~
  169. The use of ``require`` demands that the required state executes before the
  170. dependent state. The state containing the ``require`` requisite is defined as the
  171. dependent state. The state specified in the ``require`` statement is defined as the
  172. required state. If the required state's execution succeeds, the dependent state
  173. will then execute. If the required state's execution fails, the dependent state
  174. will not execute. In the first example above, the file ``/etc/vimrc`` will only
  175. execute after the vim package is installed successfully.
  176. Require an Entire SLS File
  177. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  178. As of Salt 0.16.0, it is possible to require an entire sls file. Do this first by
  179. including the sls file and then setting a state to ``require`` the included sls file:
  180. .. code-block:: yaml
  181. include:
  182. - foo
  183. bar:
  184. pkg.installed:
  185. - require:
  186. - sls: foo
  187. This will add all of the state declarations found in the given sls file. This means
  188. that every state in sls `foo` will be required. This makes it very easy to batch
  189. large groups of states easily in any requisite statement.
  190. .. _requisites-require_any:
  191. require_any
  192. ~~~~~~~~~~~
  193. .. versionadded:: 2018.3.0
  194. The use of ``require_any`` demands that one of the required states executes before the
  195. dependent state. The state containing the ``require_any`` requisite is defined as the
  196. dependent state. The states specified in the ``require_any`` statement are defined as the
  197. required states. If at least one of the required state's execution succeeds, the dependent state
  198. will then execute. If all of the executions by the required states fail, the dependent state
  199. will not execute.
  200. .. code-block:: yaml
  201. A:
  202. cmd.run:
  203. - name: echo A
  204. - require_any:
  205. - cmd: B
  206. - cmd: C
  207. - cmd: D
  208. B:
  209. cmd.run:
  210. - name: echo B
  211. C:
  212. cmd.run:
  213. - name: /bin/false
  214. D:
  215. cmd.run:
  216. - name: echo D
  217. In this example `A` will run because at least one of the requirements specified,
  218. `B`, `C`, or `D` will succeed.
  219. .. _requisites-watch:
  220. watch
  221. ~~~~~
  222. ``watch`` statements are used to add additional behavior when there are changes
  223. in other states.
  224. .. note::
  225. If a state should only execute when another state has changes, and
  226. otherwise do nothing, the new ``onchanges`` requisite should be used
  227. instead of ``watch``. ``watch`` is designed to add *additional* behavior
  228. when there are changes, but otherwise the state executes normally.
  229. The state containing the ``watch`` requisite is defined as the watching
  230. state. The state specified in the ``watch`` statement is defined as the watched
  231. state. When the watched state executes, it will return a dictionary containing
  232. a key named "changes". Here are two examples of state return dictionaries,
  233. shown in json for clarity:
  234. .. code-block:: json
  235. {
  236. "local": {
  237. "file_|-/tmp/foo_|-/tmp/foo_|-directory": {
  238. "comment": "Directory /tmp/foo updated",
  239. "__run_num__": 0,
  240. "changes": {
  241. "user": "bar"
  242. },
  243. "name": "/tmp/foo",
  244. "result": true
  245. }
  246. }
  247. }
  248. {
  249. "local": {
  250. "pkgrepo_|-salt-minion_|-salt-minion_|-managed": {
  251. "comment": "Package repo 'salt-minion' already configured",
  252. "__run_num__": 0,
  253. "changes": {},
  254. "name": "salt-minion",
  255. "result": true
  256. }
  257. }
  258. }
  259. If the "result" of the watched state is ``True``, the watching state *will
  260. execute normally*, and if it is ``False``, the watching state will never run.
  261. This part of ``watch`` mirrors the functionality of the ``require`` requisite.
  262. If the "result" of the watched state is ``True`` *and* the "changes"
  263. key contains a populated dictionary (changes occurred in the watched state),
  264. then the ``watch`` requisite can add additional behavior. This additional
  265. behavior is defined by the ``mod_watch`` function within the watching state
  266. module. If the ``mod_watch`` function exists in the watching state module, it
  267. will be called *in addition to* the normal watching state. The return data
  268. from the ``mod_watch`` function is what will be returned to the master in this
  269. case; the return data from the main watching function is discarded.
  270. If the "changes" key contains an empty dictionary, the ``watch`` requisite acts
  271. exactly like the ``require`` requisite (the watching state will execute if
  272. "result" is ``True``, and fail if "result" is ``False`` in the watched state).
  273. .. note::
  274. Not all state modules contain ``mod_watch``. If ``mod_watch`` is absent
  275. from the watching state module, the ``watch`` requisite behaves exactly
  276. like a ``require`` requisite.
  277. A good example of using ``watch`` is with a :mod:`service.running
  278. <salt.states.service.running>` state. When a service watches a state, then
  279. the service is reloaded/restarted when the watched state changes, in addition
  280. to Salt ensuring that the service is running.
  281. .. code-block:: yaml
  282. ntpd:
  283. service.running:
  284. - watch:
  285. - file: /etc/ntp.conf
  286. file.managed:
  287. - name: /etc/ntp.conf
  288. - source: salt://ntp/files/ntp.conf
  289. watch_any
  290. ~~~~~~~~~
  291. .. versionadded:: 2018.3.0
  292. The state containing the ``watch_any`` requisite is defined as the watching
  293. state. The states specified in the ``watch_any`` statement are defined as the watched
  294. states. When the watched states execute, they will return a dictionary containing
  295. a key named "changes".
  296. If the "result" of any of the watched states is ``True``, the watching state *will
  297. execute normally*, and if all of them are ``False``, the watching state will never run.
  298. This part of ``watch`` mirrors the functionality of the ``require`` requisite.
  299. If the "result" of any of the watched states is ``True`` *and* the "changes"
  300. key contains a populated dictionary (changes occurred in the watched state),
  301. then the ``watch`` requisite can add additional behavior. This additional
  302. behavior is defined by the ``mod_watch`` function within the watching state
  303. module. If the ``mod_watch`` function exists in the watching state module, it
  304. will be called *in addition to* the normal watching state. The return data
  305. from the ``mod_watch`` function is what will be returned to the master in this
  306. case; the return data from the main watching function is discarded.
  307. If the "changes" key contains an empty dictionary, the ``watch`` requisite acts
  308. exactly like the ``require`` requisite (the watching state will execute if
  309. "result" is ``True``, and fail if "result" is ``False`` in the watched state).
  310. .. code-block:: yaml
  311. apache2:
  312. service.running:
  313. - watch_any:
  314. - file: /etc/apache2/sites-available/site1.conf
  315. - file: apache2-site2
  316. file.managed:
  317. - name: /etc/apache2/sites-available/site1.conf
  318. - source: salt://apache2/files/site1.conf
  319. apache2-site2:
  320. file.managed:
  321. - name: /etc/apache2/sites-available/site2.conf
  322. - source: salt://apache2/files/site2.conf
  323. In this example, the service will be reloaded/restarted if either of the
  324. file.managed states has a result of True and has changes.
  325. .. _requisites-prereq:
  326. prereq
  327. ~~~~~~
  328. .. versionadded:: 0.16.0
  329. ``prereq`` allows for actions to be taken based on the expected results of
  330. a state that has not yet been executed. The state containing the ``prereq``
  331. requisite is defined as the pre-requiring state. The state specified in the
  332. ``prereq`` statement is defined as the pre-required state.
  333. When a ``prereq`` requisite is evaluated, the pre-required state reports if it
  334. expects to have any changes. It does this by running the pre-required single
  335. state as a test-run by enabling ``test=True``. This test-run will return a
  336. dictionary containing a key named "changes". (See the ``watch`` section above
  337. for examples of "changes" dictionaries.)
  338. If the "changes" key contains a populated dictionary, it means that the
  339. pre-required state expects changes to occur when the state is actually
  340. executed, as opposed to the test-run. The pre-requiring state will now
  341. actually run. If the pre-requiring state executes successfully, the
  342. pre-required state will then execute. If the pre-requiring state fails, the
  343. pre-required state will not execute.
  344. If the "changes" key contains an empty dictionary, this means that changes are
  345. not expected by the pre-required state. Neither the pre-required state nor the
  346. pre-requiring state will run.
  347. The best way to define how ``prereq`` operates is displayed in the following
  348. practical example: When a service should be shut down because underlying code
  349. is going to change, the service should be off-line while the update occurs. In
  350. this example, ``graceful-down`` is the pre-requiring state and ``site-code``
  351. is the pre-required state.
  352. .. code-block:: yaml
  353. graceful-down:
  354. cmd.run:
  355. - name: service apache graceful
  356. - prereq:
  357. - file: site-code
  358. site-code:
  359. file.recurse:
  360. - name: /opt/site_code
  361. - source: salt://site/code
  362. In this case the apache server will only be shutdown if the site-code state
  363. expects to deploy fresh code via the file.recurse call. The site-code
  364. deployment will only be executed if the graceful-down run completes
  365. successfully.
  366. .. _requisites-onfail:
  367. onfail
  368. ~~~~~~
  369. .. versionadded:: 2014.7.0
  370. The ``onfail`` requisite allows for reactions to happen strictly as a response
  371. to the failure of another state. This can be used in a number of ways, such as
  372. executing a second attempt to set up a service or begin to execute a separate
  373. thread of states because of a failure.
  374. The ``onfail`` requisite is applied in the same way as ``require`` as ``watch``:
  375. .. code-block:: yaml
  376. primary_mount:
  377. mount.mounted:
  378. - name: /mnt/share
  379. - device: 10.0.0.45:/share
  380. - fstype: nfs
  381. backup_mount:
  382. mount.mounted:
  383. - name: /mnt/share
  384. - device: 192.168.40.34:/share
  385. - fstype: nfs
  386. - onfail:
  387. - mount: primary_mount
  388. .. note::
  389. Setting failhard (:ref:`globally <global-failhard>` or in
  390. :ref:`the failing state <state-level-failhard>`) to ``True`` will cause
  391. ``onfail``, ``onfail_in`` and ``onfail_any`` requisites to be ignored.
  392. If you want to combine a global failhard set to True with ``onfail``,
  393. ``onfail_in`` or ``onfail_any``, you will have to explicitly set failhard
  394. to ``False`` (overriding the global setting) in the state that could fail.
  395. .. note::
  396. Beginning in the ``2016.11.0`` release of Salt, ``onfail`` uses OR logic for
  397. multiple listed ``onfail`` requisites. Prior to the ``2016.11.0`` release,
  398. ``onfail`` used AND logic. See `Issue #22370`_ for more information.
  399. .. _Issue #22370: https://github.com/saltstack/salt/issues/22370
  400. .. _requisites-onfail_any:
  401. onfail_any
  402. ~~~~~~~~~~
  403. .. versionadded:: 2018.3.0
  404. The ``onfail_any`` requisite allows for reactions to happen strictly as a response
  405. to the failure of at least one other state. This can be used in a number of ways, such as
  406. executing a second attempt to set up a service or begin to execute a separate
  407. thread of states because of a failure.
  408. The ``onfail_any`` requisite is applied in the same way as ``require_any`` and ``watch_any``:
  409. .. code-block:: yaml
  410. primary_mount:
  411. mount.mounted:
  412. - name: /mnt/share
  413. - device: 10.0.0.45:/share
  414. - fstype: nfs
  415. secondary_mount:
  416. mount.mounted:
  417. - name: /mnt/code
  418. - device: 10.0.0.45:/code
  419. - fstype: nfs
  420. backup_mount:
  421. mount.mounted:
  422. - name: /mnt/share
  423. - device: 192.168.40.34:/share
  424. - fstype: nfs
  425. - onfail_any:
  426. - mount: primary_mount
  427. - mount: secondary_mount
  428. In this example, the `backup_mount` will be mounted if either of the
  429. `primary_mount` or `secondary_mount` states results in a failure.
  430. .. _requisites-onchanges:
  431. onchanges
  432. ~~~~~~~~~
  433. .. versionadded:: 2014.7.0
  434. The ``onchanges`` requisite makes a state only apply if the required states
  435. generate changes, and if the watched state's "result" is ``True``. This can be
  436. a useful way to execute a post hook after changing aspects of a system.
  437. If a state has multiple ``onchanges`` requisites then the state will trigger
  438. if any of the watched states changes.
  439. .. note::
  440. One easy-to-make mistake is to use ``onchanges_in`` when ``onchanges`` is
  441. supposed to be used. For example, the below configuration is not correct:
  442. .. code-block:: yaml
  443. myservice:
  444. pkg.installed:
  445. - name: myservice
  446. file.managed:
  447. - name: /etc/myservice/myservice.conf
  448. - source: salt://myservice/files/myservice.conf
  449. - mode: 600
  450. cmd.run:
  451. - name: /usr/libexec/myservice/post-changes-hook.sh
  452. - onchanges_in:
  453. - file: /etc/myservice/myservice.conf
  454. This will set up a requisite relationship in which the ``cmd.run`` state
  455. always executes, and the ``file.managed`` state only executes if the
  456. ``cmd.run`` state has changes (which it always will, since the ``cmd.run``
  457. state includes the command results as changes).
  458. It may semantically seem like the ``cmd.run`` state should only run
  459. when there are changes in the file state, but remember that requisite
  460. relationships involve one state watching another state, and a
  461. :ref:`requisite_in <requisites-onchanges-in>` does the opposite: it forces
  462. the specified state to watch the state with the ``requisite_in``.
  463. The correct usage would be:
  464. .. code-block:: yaml
  465. myservice:
  466. pkg.installed:
  467. - name: myservice
  468. file.managed:
  469. - name: /etc/myservice/myservice.conf
  470. - source: salt://myservice/files/myservice.conf
  471. - mode: 600
  472. cmd.run:
  473. - name: /usr/libexec/myservice/post-changes-hook.sh
  474. - onchanges:
  475. - file: /etc/myservice/myservice.conf
  476. .. _requisites-onchanges_any:
  477. onchanges_any
  478. ~~~~~~~~~~~~~
  479. .. versionadded:: 2018.3.0
  480. The ``onchanges_any`` requisite makes a state only apply one of the required states
  481. generates changes, and if one of the watched state's "result" is ``True``. This can be
  482. a useful way to execute a post hook after changing aspects of a system.
  483. .. code-block:: yaml
  484. myservice:
  485. pkg.installed:
  486. - name: myservice
  487. - name: yourservice
  488. file.managed:
  489. - name: /etc/myservice/myservice.conf
  490. - source: salt://myservice/files/myservice.conf
  491. - mode: 600
  492. file.managed:
  493. - name: /etc/yourservice/yourservice.conf
  494. - source: salt://yourservice/files/yourservice.conf
  495. - mode: 600
  496. cmd.run:
  497. - name: /usr/libexec/myservice/post-changes-hook.sh
  498. - onchanges_any:
  499. - file: /etc/myservice/myservice.conf
  500. - file: /etc/your_service/yourservice.conf
  501. In this example, the `cmd.run` would be run only if either of the
  502. `file.managed` states generated changes and at least one of the
  503. watched state's "result" is ``True``.
  504. use
  505. ~~~
  506. The ``use`` requisite is used to inherit the arguments passed in another
  507. id declaration. This is useful when many files need to have the same defaults.
  508. .. code-block:: yaml
  509. /etc/foo.conf:
  510. file.managed:
  511. - source: salt://foo.conf
  512. - template: jinja
  513. - mkdirs: True
  514. - user: apache
  515. - group: apache
  516. - mode: 755
  517. /etc/bar.conf:
  518. file.managed:
  519. - source: salt://bar.conf
  520. - use:
  521. - file: /etc/foo.conf
  522. The ``use`` statement was developed primarily for the networking states but
  523. can be used on any states in Salt. This makes sense for the networking state
  524. because it can define a long list of options that need to be applied to
  525. multiple network interfaces.
  526. The ``use`` statement does not inherit the requisites arguments of the
  527. targeted state. This means also a chain of ``use`` requisites would not
  528. inherit inherited options.
  529. runas
  530. ~~~~~
  531. .. versionadded:: 2017.7.0
  532. The ``runas`` global option is used to set the user which will be used to run
  533. the command in the ``cmd.run`` module.
  534. .. code-block:: yaml
  535. django:
  536. pip.installed:
  537. - name: django >= 1.6, <= 1.7
  538. - runas: daniel
  539. - require:
  540. - pkg: python-pip
  541. In the above state, the pip command run by ``cmd.run`` will be run by the daniel user.
  542. runas_password
  543. ~~~~~~~~~~~~~~
  544. .. versionadded:: 2017.7.2
  545. The ``runas_password`` global option is used to set the password used by the
  546. runas global option. This is required by ``cmd.run`` on Windows when ``runas``
  547. is specified. It will be set when ``runas_password`` is defined in the state.
  548. .. code-block:: yaml
  549. run_script:
  550. cmd.run:
  551. - name: Powershell -NonInteractive -ExecutionPolicy Bypass -File C:\\Temp\\script.ps1
  552. - runas: frank
  553. - runas_password: supersecret
  554. In the above state, the Powershell script run by ``cmd.run`` will be run by the
  555. frank user with the password ``supersecret``.
  556. .. _requisites-require-in:
  557. .. _requisites-watch-in:
  558. .. _requisites-onchanges-in:
  559. The _in versions of requisites
  560. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  561. All of the requisites also have corresponding requisite_in versions, which do
  562. the reverse of their normal counterparts. The examples below all use
  563. ``require_in`` as the example, but note that all of the ``_in`` requisites work
  564. the same way: They result in a normal requisite in the targeted state, which
  565. targets the state which has defines the requisite_in. Thus, a ``require_in``
  566. causes the target state to ``require`` the targeting state. Similarly, a
  567. ``watch_in`` causes the target state to ``watch`` the targeting state. This
  568. pattern continues for the rest of the requisites.
  569. If a state declaration needs to be required by another state declaration then
  570. ``require_in`` can accommodate it. Therefore, these two sls files would be the
  571. same in the end:
  572. Using ``require``
  573. .. code-block:: yaml
  574. httpd:
  575. pkg.installed: []
  576. service.running:
  577. - require:
  578. - pkg: httpd
  579. Using ``require_in``
  580. .. code-block:: yaml
  581. httpd:
  582. pkg.installed:
  583. - require_in:
  584. - service: httpd
  585. service.running: []
  586. The ``require_in`` statement is particularly useful when assigning a require
  587. in a separate sls file. For instance it may be common for httpd to require
  588. components used to set up PHP or mod_python, but the HTTP state does not need
  589. to be aware of the additional components that require it when it is set up:
  590. http.sls
  591. .. code-block:: yaml
  592. httpd:
  593. pkg.installed: []
  594. service.running:
  595. - require:
  596. - pkg: httpd
  597. php.sls
  598. .. code-block:: yaml
  599. include:
  600. - http
  601. php:
  602. pkg.installed:
  603. - require_in:
  604. - service: httpd
  605. mod_python.sls
  606. .. code-block:: yaml
  607. include:
  608. - http
  609. mod_python:
  610. pkg.installed:
  611. - require_in:
  612. - service: httpd
  613. Now the httpd server will only start if both php and mod_python are first verified to
  614. be installed. Thus allowing for a requisite to be defined "after the fact".
  615. .. _requisites-fire-event:
  616. Fire Event Notifications
  617. ========================
  618. .. versionadded:: 2015.8.0
  619. The `fire_event` option in a state will cause the minion to send an event to
  620. the Salt Master upon completion of that individual state.
  621. The following example will cause the minion to send an event to the Salt Master
  622. with a tag of `salt/state_result/20150505121517276431/dasalt/nano` and the
  623. result of the state will be the data field of the event. Notice that the `name`
  624. of the state gets added to the tag.
  625. .. code-block:: yaml
  626. nano_stuff:
  627. pkg.installed:
  628. - name: nano
  629. - fire_event: True
  630. In the following example instead of setting `fire_event` to `True`,
  631. `fire_event` is set to an arbitrary string, which will cause the event to be
  632. sent with this tag:
  633. `salt/state_result/20150505121725642845/dasalt/custom/tag/nano/finished`
  634. .. code-block:: yaml
  635. nano_stuff:
  636. pkg.installed:
  637. - name: nano
  638. - fire_event: custom/tag/nano/finished
  639. Altering States
  640. ===============
  641. The state altering system is used to make sure that states are evaluated exactly
  642. as the user expects. It can be used to double check that a state preformed
  643. exactly how it was expected to, or to make 100% sure that a state only runs
  644. under certain conditions. The use of unless or onlyif options help make states
  645. even more stateful. The ``check_cmd`` option helps ensure that the result of a
  646. state is evaluated correctly.
  647. Reload
  648. ------
  649. ``reload_modules`` is a boolean option that forces salt to reload its modules
  650. after a state finishes. ``reload_pillar`` and ``reload_grains`` can also be set.
  651. See :ref:`Reloading Modules <reloading-modules>`.
  652. .. code-block:: yaml
  653. grains_refresh:
  654. module.run:
  655. - name: saltutil.refresh_grains
  656. - reload_grains: true
  657. grains_read:
  658. module.run:
  659. - name: grains.items
  660. .. _unless-requisite:
  661. Unless
  662. ------
  663. .. versionadded:: 2014.7.0
  664. The ``unless`` requisite specifies that a state should only run when any of
  665. the specified commands return ``False``. The ``unless`` requisite operates
  666. as NAND and is useful in giving more granular control over when a state should
  667. execute.
  668. **NOTE**: Under the hood ``unless`` calls ``cmd.retcode`` with
  669. ``python_shell=True``. This means the commands referenced by ``unless`` will be
  670. parsed by a shell, so beware of side-effects as this shell will be run with the
  671. same privileges as the salt-minion. Also be aware that the boolean value is
  672. determined by the shell's concept of ``True`` and ``False``, rather than Python's
  673. concept of ``True`` and ``False``.
  674. .. code-block:: yaml
  675. vim:
  676. pkg.installed:
  677. - unless:
  678. - rpm -q vim-enhanced
  679. - ls /usr/bin/vim
  680. In the example above, the state will only run if either the vim-enhanced
  681. package is not installed (returns ``False``) or if /usr/bin/vim does not
  682. exist (returns ``False``). The state will run if both commands return
  683. ``False``.
  684. However, the state will not run if both commands return ``True``.
  685. Unless checks are resolved for each name to which they are associated.
  686. For example:
  687. .. code-block:: yaml
  688. deploy_app:
  689. cmd.run:
  690. - names:
  691. - first_deploy_cmd
  692. - second_deploy_cmd
  693. - unless: ls /usr/bin/vim
  694. In the above case, ``some_check`` will be run prior to _each_ name -- once for
  695. ``first_deploy_cmd`` and a second time for ``second_deploy_cmd``.
  696. .. _onlyif-requisite:
  697. Onlyif
  698. ------
  699. .. versionadded:: 2014.7.0
  700. The ``onlyif`` requisite specifies that if each command listed in ``onlyif``
  701. returns ``True``, then the state is run. If any of the specified commands
  702. return ``False``, the state will not run.
  703. **NOTE**: Under the hood ``onlyif`` calls ``cmd.retcode`` with
  704. ``python_shell=True``. This means the commands referenced by ``onlyif`` will be
  705. parsed by a shell, so beware of side-effects as this shell will be run with the
  706. same privileges as the salt-minion. Also be aware that the boolean value is
  707. determined by the shell's concept of ``True`` and ``False``, rather than Python's
  708. concept of ``True`` and ``False``.
  709. .. code-block:: yaml
  710. stop-volume:
  711. module.run:
  712. - name: glusterfs.stop_volume
  713. - m_name: work
  714. - onlyif:
  715. - gluster volume status work
  716. - order: 1
  717. remove-volume:
  718. module.run:
  719. - name: glusterfs.delete
  720. - m_name: work
  721. - onlyif:
  722. - gluster volume info work
  723. - watch:
  724. - cmd: stop-volume
  725. The above example ensures that the stop_volume and delete modules only run
  726. if the gluster commands return a 0 ret value.
  727. Listen/Listen_in
  728. ----------------
  729. .. versionadded:: 2014.7.0
  730. listen and its counterpart listen_in trigger mod_wait functions for states,
  731. when those states succeed and result in changes, similar to how watch its
  732. counterpart watch_in. Unlike watch and watch_in, listen, and listen_in will
  733. not modify the order of states and can be used to ensure your states are
  734. executed in the order they are defined. All listen/listen_in actions will occur
  735. at the end of a state run, after all states have completed.
  736. .. code-block:: yaml
  737. restart-apache2:
  738. service.running:
  739. - name: apache2
  740. - listen:
  741. - file: /etc/apache2/apache2.conf
  742. configure-apache2:
  743. file.managed:
  744. - name: /etc/apache2/apache2.conf
  745. - source: salt://apache2/apache2.conf
  746. This example will cause apache2 to be restarted when the apache2.conf file is
  747. changed, but the apache2 restart will happen at the end of the state run.
  748. .. code-block:: yaml
  749. restart-apache2:
  750. service.running:
  751. - name: apache2
  752. configure-apache2:
  753. file.managed:
  754. - name: /etc/apache2/apache2.conf
  755. - source: salt://apache2/apache2.conf
  756. - listen_in:
  757. - service: apache2
  758. This example does the same as the above example, but puts the state argument
  759. on the file resource, rather than the service resource.
  760. check_cmd
  761. ---------
  762. .. versionadded:: 2014.7.0
  763. Check Command is used for determining that a state did or did not run as
  764. expected.
  765. **NOTE**: Under the hood ``check_cmd`` calls ``cmd.retcode`` with
  766. ``python_shell=True``. This means the commands referenced by unless will be
  767. parsed by a shell, so beware of side-effects as this shell will be run with the
  768. same privileges as the salt-minion.
  769. .. code-block:: yaml
  770. comment-repo:
  771. file.replace:
  772. - name: /etc/yum.repos.d/fedora.repo
  773. - pattern: '^enabled=0'
  774. - repl: enabled=1
  775. - check_cmd:
  776. - "! grep 'enabled=0' /etc/yum.repos.d/fedora.repo"
  777. This will attempt to do a replace on all ``enabled=0`` in the .repo file, and
  778. replace them with ``enabled=1``. The ``check_cmd`` is just a bash command. It
  779. will do a grep for ``enabled=0`` in the file, and if it finds any, it will
  780. return a 0, which will be inverted by the leading ``!``, causing ``check_cmd``
  781. to set the state as failed. If it returns a 1, meaning it didn't find any
  782. ``enabled=0``, it will be inverted by the leading ``!``, returning a 0, and
  783. declaring the function succeeded.
  784. **NOTE**: This requisite ``check_cmd`` functions differently than the ``check_cmd``
  785. of the ``file.managed`` state.
  786. Overriding Checks
  787. -----------------
  788. There are two commands used for the above checks.
  789. ``mod_run_check`` is used to check for ``onlyif`` and ``unless``. If the goal is to
  790. override the global check for these to variables, include a ``mod_run_check`` in the
  791. salt/states/ file.
  792. ``mod_run_check_cmd`` is used to check for the check_cmd options. To override
  793. this one, include a ``mod_run_check_cmd`` in the states file for the state.
  794. Retrying States
  795. ===============
  796. .. versionadded:: 2017.7.0
  797. The retry option in a state allows it to be executed multiple times until a desired
  798. result is obtained or the maximum number of attempts have been made.
  799. The retry option can be configured by the ``attempts``, ``until``, ``interval``, and
  800. ``splay`` parameters.
  801. The ``attempts`` parameter controls the maximum number of times the state will be
  802. run. If not specified or if an invalid value is specified, ``attempts`` will default
  803. to ``2``.
  804. The ``until`` parameter defines the result that is required to stop retrying the state.
  805. If not specified or if an invalid value is specified, ``until`` will default to ``True``
  806. The ``interval`` parameter defines the amount of time, in seconds, that the system
  807. will wait between attempts. If not specified or if an invalid value is specified,
  808. ``interval`` will default to ``30``.
  809. The ``splay`` parameter allows the ``interval`` to be additionally spread out. If not
  810. specified or if an invalid value is specified, ``splay`` defaults to ``0`` (i.e. no
  811. splaying will occur).
  812. The following example will run the pkg.installed state until it returns ``True`` or it has
  813. been run ``5`` times. Each attempt will be ``60`` seconds apart and the interval will be splayed
  814. up to an additional ``10`` seconds:
  815. .. code-block:: yaml
  816. my_retried_state:
  817. pkg.installed:
  818. - name: nano
  819. - retry:
  820. attempts: 5
  821. until: True
  822. interval: 60
  823. splay: 10
  824. The following example will run the pkg.installed state with all the defaults for ``retry``.
  825. The state will run up to ``2`` times, each attempt being ``30`` seconds apart, or until it
  826. returns ``True``.
  827. .. code-block:: yaml
  828. install_nano:
  829. pkg.installed:
  830. - name: nano
  831. - retry: True
  832. The following example will run the file.exists state every ``30`` seconds up to ``15`` times
  833. or until the file exists (i.e. the state returns ``True``).
  834. .. code-block:: yaml
  835. wait_for_file:
  836. file.exists:
  837. - name: /path/to/file
  838. - retry:
  839. attempts: 15
  840. interval: 30
  841. Return data from a retried state
  842. --------------------------------
  843. When a state is retried, the returned output is as follows:
  844. The ``result`` return value is the ``result`` from the final run. For example, imagine a state set
  845. to ``retry`` up to three times or ``until`` ``True``. If the state returns ``False`` on the first run
  846. and then ``True`` on the second, the ``result`` of the state will be ``True``.
  847. The ``started`` return value is the ``started`` from the first run.
  848. The ``duration`` return value is the total duration of all attempts plus the retry intervals.
  849. The ``comment`` return value will include the result and comment from all previous attempts.
  850. For example:
  851. .. code-block:: yaml
  852. wait_for_file:
  853. file.exists:
  854. - name: /path/to/file
  855. - retry:
  856. attempts: 10
  857. interval: 2
  858. splay: 5
  859. Would return similar to the following. The state result in this case is ``False`` (file.exist was run 10
  860. times with a 2 second interval, but the file specified did not exist on any run).
  861. .. code-block:: none
  862. ID: wait_for_file
  863. Function: file.exists
  864. Result: False
  865. Comment: Attempt 1: Returned a result of "False", with the following comment: "Specified path /path/to/file does not exist"
  866. Attempt 2: Returned a result of "False", with the following comment: "Specified path /path/to/file does not exist"
  867. Attempt 3: Returned a result of "False", with the following comment: "Specified path /path/to/file does not exist"
  868. Attempt 4: Returned a result of "False", with the following comment: "Specified path /path/to/file does not exist"
  869. Attempt 5: Returned a result of "False", with the following comment: "Specified path /path/to/file does not exist"
  870. Attempt 6: Returned a result of "False", with the following comment: "Specified path /path/to/file does not exist"
  871. Attempt 7: Returned a result of "False", with the following comment: "Specified path /path/to/file does not exist"
  872. Attempt 8: Returned a result of "False", with the following comment: "Specified path /path/to/file does not exist"
  873. Attempt 9: Returned a result of "False", with the following comment: "Specified path /path/to/file does not exist"
  874. Specified path /path/to/file does not exist
  875. Started: 09:08:12.903000
  876. Duration: 47000.0 ms
  877. Changes: