extend.rst 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. ===========================
  2. Extending External SLS Data
  3. ===========================
  4. Sometimes a state defined in one SLS file will need to be modified from a
  5. separate SLS file. A good example of this is when an argument needs to be
  6. overwritten or when a service needs to watch an additional state.
  7. The Extend Declaration
  8. ----------------------
  9. The standard way to extend is via the extend declaration. The extend
  10. declaration is a top level declaration like ``include`` and encapsulates ID
  11. declaration data included from other SLS files. A standard extend looks like
  12. this:
  13. .. code-block:: yaml
  14. include:
  15. - http
  16. - ssh
  17. extend:
  18. apache:
  19. file:
  20. - name: /etc/httpd/conf/httpd.conf
  21. - source: salt://http/httpd2.conf
  22. ssh-server:
  23. service:
  24. - watch:
  25. - file: /etc/ssh/banner
  26. /etc/ssh/banner:
  27. file.managed:
  28. - source: salt://ssh/banner
  29. A few critical things happened here, first off the SLS files that are going to
  30. be extended are included, then the extend dec is defined. Under the extend dec
  31. 2 IDs are extended, the apache ID's file state is overwritten with a new name
  32. and source. Then the ssh server is extended to watch the banner file in
  33. addition to anything it is already watching.
  34. Extend is a Top Level Declaration
  35. ---------------------------------
  36. This means that ``extend`` can only be called once in an sls, if it is used
  37. twice then only one of the extend blocks will be read. So this is WRONG:
  38. .. code-block:: yaml
  39. include:
  40. - http
  41. - ssh
  42. extend:
  43. apache:
  44. file:
  45. - name: /etc/httpd/conf/httpd.conf
  46. - source: salt://http/httpd2.conf
  47. # Second extend will overwrite the first!! Only make one
  48. extend:
  49. ssh-server:
  50. service:
  51. - watch:
  52. - file: /etc/ssh/banner
  53. The Requisite "in" Statement
  54. ----------------------------
  55. Since one of the most common things to do when extending another SLS is to add
  56. states for a service to watch, or anything for a watcher to watch, the
  57. requisite in statement was added to 0.9.8 to make extending the watch and
  58. require lists easier. The ssh-server extend statement above could be more
  59. cleanly defined like so:
  60. .. code-block:: yaml
  61. include:
  62. - ssh
  63. /etc/ssh/banner:
  64. file.managed:
  65. - source: salt://ssh/banner
  66. - watch_in:
  67. - service: ssh-server
  68. Rules to Extend By
  69. ------------------
  70. There are a few rules to remember when extending states:
  71. 1. Always include the SLS being extended with an include declaration
  72. 2. Requisites (watch and require) are appended to, everything else is
  73. overwritten
  74. 3. extend is a top level declaration, like an ID declaration, cannot be
  75. declared twice in a single SLS
  76. 4. Many IDs can be extended under the extend declaration