1
0

salt.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. ================================
  2. Using the Salt Modules for Cloud
  3. ================================
  4. In addition to the ``salt-cloud`` command, Salt Cloud can be called from Salt,
  5. in a variety of different ways. Most users will be interested in either the
  6. execution module or the state module, but it is also possible to call Salt Cloud
  7. as a runner.
  8. Because the actual work will be performed on a remote minion, the normal Salt
  9. Cloud configuration must exist on any target minion that needs to execute a Salt
  10. Cloud command. Because Salt Cloud now supports breaking out configuration into
  11. individual files, the configuration is easily managed using Salt's own
  12. ``file.managed`` state function. For example, the following directories allow
  13. this configuration to be managed easily:
  14. .. code-block:: yaml
  15. /etc/salt/cloud.providers.d/
  16. /etc/salt/cloud.profiles.d/
  17. Minion Keys
  18. -----------
  19. Keep in mind that when creating minions, Salt Cloud will create public and
  20. private minion keys, upload them to the minion, and place the public key on the
  21. machine that created the minion. It will *not* attempt to place any public
  22. minion keys on the master, unless the minion which was used to create the
  23. instance is also the Salt Master. This is because granting arbitrary minions
  24. access to modify keys on the master is a serious security risk, and must be
  25. avoided.
  26. Execution Module
  27. ----------------
  28. The ``cloud`` module is available to use from the command line. At the moment,
  29. almost every standard Salt Cloud feature is available to use. The following
  30. commands are available:
  31. list_images
  32. ~~~~~~~~~~~
  33. This command is designed to show images that are available to be used to create
  34. an instance using Salt Cloud. In general they are used in the creation of
  35. profiles, but may also be used to create an instance directly (see below).
  36. Listing images requires a provider to be configured, and specified:
  37. .. code-block:: bash
  38. salt myminion cloud.list_images my-cloud-provider
  39. list_sizes
  40. ~~~~~~~~~~
  41. This command is designed to show sizes that are available to be used to create
  42. an instance using Salt Cloud. In general they are used in the creation of
  43. profiles, but may also be used to create an instance directly (see below). This
  44. command is not available for all cloud providers; see the provider-specific
  45. documentation for details. Listing sizes requires a provider to be configured,
  46. and specified:
  47. .. code-block:: bash
  48. salt myminion cloud.list_sizes my-cloud-provider
  49. list_locations
  50. ~~~~~~~~~~~~~~
  51. This command is designed to show locations that are available to be used to
  52. create an instance using Salt Cloud. In general they are used in the creation of
  53. profiles, but may also be used to create an instance directly (see below). This
  54. command is not available for all cloud providers; see the provider-specific
  55. documentation for details. Listing locations requires a provider to be
  56. configured, and specified:
  57. .. code-block:: bash
  58. salt myminion cloud.list_locations my-cloud-provider
  59. query
  60. ~~~~~
  61. This command is used to query all configured cloud providers, and display all
  62. instances associated with those accounts. By default, it will run a standard
  63. query, returning the following fields:
  64. ``id``
  65. The name or ID of the instance, as used by the cloud provider.
  66. ``image``
  67. The disk image that was used to create this instance.
  68. ``private_ips``
  69. Any public IP addresses currently assigned to this instance.
  70. ``public_ips``
  71. Any private IP addresses currently assigned to this instance.
  72. ``size``
  73. The size of the instance; can refer to RAM, CPU(s), disk space, etc.,
  74. depending on the cloud provider.
  75. ``state``
  76. The running state of the instance; for example, ``running``, ``stopped``,
  77. ``pending``, etc. This state is dependent upon the provider.
  78. This command may also be used to perform a full query or a select query, as
  79. described below. The following usages are available:
  80. .. code-block:: bash
  81. salt myminion cloud.query
  82. salt myminion cloud.query list_nodes
  83. salt myminion cloud.query list_nodes_full
  84. full_query
  85. ~~~~~~~~~~
  86. This command behaves like the ``query`` command, but lists all information
  87. concerning each instance as provided by the cloud provider, in addition to the
  88. fields returned by the ``query`` command.
  89. .. code-block:: bash
  90. salt myminion cloud.full_query
  91. select_query
  92. ~~~~~~~~~~~~
  93. This command behaves like the ``query`` command, but only returned select
  94. fields as defined in the ``/etc/salt/cloud`` configuration file. A sample
  95. configuration for this section of the file might look like:
  96. .. code-block:: yaml
  97. query.selection:
  98. - id
  99. - key_name
  100. This configuration would only return the ``id`` and ``key_name`` fields, for
  101. those cloud providers that support those two fields. This would be called using
  102. the following command:
  103. .. code-block:: bash
  104. salt myminion cloud.select_query
  105. profile
  106. ~~~~~~~
  107. This command is used to create an instance using a profile that is configured
  108. on the target minion. Please note that the profile must be configured before
  109. this command can be used with it.
  110. .. code-block:: bash
  111. salt myminion cloud.profile ec2-centos64-x64 my-new-instance
  112. Please note that the execution module does *not* run in parallel mode. Using
  113. multiple minions to create instances can effectively perform parallel instance
  114. creation.
  115. create
  116. ~~~~~~
  117. This command is similar to the ``profile`` command, in that it is used to create
  118. a new instance. However, it does not require a profile to be pre-configured.
  119. Instead, all of the options that are normally configured in a profile are passed
  120. directly to Salt Cloud to create the instance:
  121. .. code-block:: bash
  122. salt myminion cloud.create my-ec2-config my-new-instance \
  123. image=ami-1624987f size='t1.micro' ssh_username=ec2-user \
  124. securitygroup=default delvol_on_destroy=True
  125. Please note that the execution module does *not* run in parallel mode. Using
  126. multiple minions to create instances can effectively perform parallel instance
  127. creation.
  128. destroy
  129. ~~~~~~~
  130. This command is used to destroy an instance or instances. This command will
  131. search all configured providers and remove any instance(s) which matches the
  132. name(s) passed in here. The results of this command are *non-reversable* and
  133. should be used with caution.
  134. .. code-block:: bash
  135. salt myminion cloud.destroy myinstance
  136. salt myminion cloud.destroy myinstance1,myinstance2
  137. action
  138. ~~~~~~
  139. This command implements both the ``action`` and the ``function`` commands
  140. used in the standard ``salt-cloud`` command. If one of the standard ``action``
  141. commands is used, an instance name must be provided. If one of the standard
  142. ``function`` commands is used, a provider configuration must be named.
  143. .. code-block:: bash
  144. salt myminion cloud.action start instance=myinstance
  145. salt myminion cloud.action show_image provider=my-ec2-config \
  146. image=ami-1624987f
  147. The actions available are largely dependent upon the module for the specific
  148. cloud provider. The following actions are available for all cloud providers:
  149. ``list_nodes``
  150. This is a direct call to the ``query`` function as described above, but is
  151. only performed against a single cloud provider. A provider configuration
  152. must be included.
  153. ``list_nodes_select``
  154. This is a direct call to the ``full_query`` function as described above, but
  155. is only performed against a single cloud provider. A provider configuration
  156. must be included.
  157. ``list_nodes_select``
  158. This is a direct call to the ``select_query`` function as described above,
  159. but is only performed against a single cloud provider. A provider
  160. configuration must be included.
  161. ``show_instance``
  162. This is a thin wrapper around ``list_nodes``, which returns the full
  163. information about a single instance. An instance name must be provided.
  164. State Module
  165. ------------
  166. A subset of the execution module is available through the ``cloud`` state
  167. module. Not all functions are currently included, because there is currently
  168. insufficient code for them to perform statefully. For example, a command to
  169. create an instance may be issued with a series of options, but those options
  170. cannot currently be statefully managed. Additional states to manage these
  171. options will be released at a later time.
  172. cloud.present
  173. ~~~~~~~~~~~~~
  174. This state will ensure that an instance is present inside a particular cloud
  175. provider. Any option that is normally specified in the ``cloud.create``
  176. execution module and function may be declared here, but only the actual
  177. presence of the instance will be managed statefully.
  178. .. code-block:: yaml
  179. my-instance-name:
  180. cloud.present:
  181. - cloud_provider: my-ec2-config
  182. - image: ami-1624987f
  183. - size: 't1.micro'
  184. - ssh_username: ec2-user
  185. - securitygroup: default
  186. - delvol_on_destroy: True
  187. cloud.profile
  188. ~~~~~~~~~~~~~
  189. This state will ensure that an instance is present inside a particular cloud
  190. provider. This function calls the ``cloud.profile`` execution module and
  191. function, but as with ``cloud.present``, only the actual presence of the
  192. instance will be managed statefully.
  193. .. code-block:: yaml
  194. my-instance-name:
  195. cloud.profile:
  196. - profile: ec2-centos64-x64
  197. cloud.absent
  198. ~~~~~~~~~~~~
  199. This state will ensure that an instance (identified by name) does not exist in
  200. any of the cloud providers configured on the target minion. Please note that
  201. this state is *non-reversable* and may be considered especially destructive when
  202. issued as a cloud state.
  203. .. code-block:: yaml
  204. my-instance-name:
  205. cloud.absent
  206. Runner Module
  207. -------------
  208. The ``cloud`` runner module is executed on the master, and performs actions
  209. using the configuration and Salt modules on the master itself. This means that
  210. any public minion keys will also be properly accepted by the master.
  211. Using the functions in the runner module is no different than using those in
  212. the execution module, outside of the behavior described in the above paragraph.
  213. The following functions are available inside the runner:
  214. - list_images
  215. - list_sizes
  216. - list_locations
  217. - query
  218. - full_query
  219. - select_query
  220. - profile
  221. - destroy
  222. - action
  223. Outside of the standard usage of ``salt-run`` itself, commands are executed as
  224. usual:
  225. .. code-block:: bash
  226. salt-run cloud.profile ec2-centos64-x86_64 my-instance-name
  227. CloudClient
  228. -----------
  229. The execution, state, and runner modules ultimately all use the CloudClient
  230. library that ships with Salt. To use the CloudClient library locally (either on
  231. the master or a minion), create a client object and issue a command against it:
  232. .. code-block:: python
  233. import salt.cloud
  234. import pprint
  235. client = salt.cloud.CloudClient("/etc/salt/cloud")
  236. nodes = client.query()
  237. pprint.pprint(nodes)
  238. Reactor
  239. -------
  240. Examples of using the reactor with Salt Cloud are available in the
  241. :formula_url:`ec2-autoscale-reactor <ec2-autoscale-reactor>` and
  242. :formula_url:`salt-cloud-reactor <salt-cloud-reactor>` formulas.