walkthrough.rst 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. .. _tutorial-salt-walk-through:
  2. ==================
  3. Salt in 10 Minutes
  4. ==================
  5. .. note::
  6. Welcome to SaltStack! I am excited that you are interested in Salt and
  7. starting down the path to better infrastructure management. I developed
  8. (and am continuing to develop) Salt with the goal of making the best
  9. software available to manage computers of almost any kind. I hope you enjoy
  10. working with Salt and that the software can solve your real world needs!
  11. - Thomas S Hatch
  12. - Salt creator and Chief Developer
  13. - CTO of SaltStack, Inc.
  14. Getting Started
  15. ===============
  16. What is Salt?
  17. -------------
  18. Salt is a different approach to infrastructure management, founded on
  19. the idea that high-speed communication with large numbers of systems can open
  20. up new capabilities. This approach makes Salt a powerful multitasking system
  21. that can solve many specific problems in an infrastructure.
  22. The backbone of Salt is the remote execution engine, which creates a high-speed,
  23. secure and bi-directional communication net for groups of systems. On top of this
  24. communication system, Salt provides an extremely fast, flexible, and easy-to-use
  25. configuration management system called ``Salt States``.
  26. Installing Salt
  27. ---------------
  28. SaltStack has been made to be very easy to install and get started. The
  29. :ref:`installation documents <installation>` contain instructions
  30. for all supported platforms.
  31. Starting Salt
  32. -------------
  33. Salt functions on a master/minion topology. A master server acts as a
  34. central control bus for the clients, which are called ``minions``. The minions
  35. connect back to the master.
  36. Setting Up the Salt Master
  37. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  38. Turning on the Salt Master is easy -- just turn it on! The default configuration
  39. is suitable for the vast majority of installations. The Salt Master can be
  40. controlled by the local Linux/Unix service manager:
  41. On Systemd based platforms (newer Debian, openSUSE, Fedora):
  42. .. code-block:: bash
  43. systemctl start salt-master
  44. On Upstart based systems (Ubuntu, older Fedora/RHEL):
  45. .. code-block:: bash
  46. service salt-master start
  47. On SysV Init systems (Gentoo, older Debian etc.):
  48. .. code-block:: bash
  49. /etc/init.d/salt-master start
  50. Alternatively, the Master can be started directly on the command-line:
  51. .. code-block:: bash
  52. salt-master -d
  53. The Salt Master can also be started in the foreground in debug mode, thus
  54. greatly increasing the command output:
  55. .. code-block:: bash
  56. salt-master -l debug
  57. The Salt Master needs to bind to two TCP network ports on the system. These ports
  58. are ``4505`` and ``4506``. For more in depth information on firewalling these ports,
  59. the firewall tutorial is available :ref:`here <firewall>`.
  60. .. _master-dns:
  61. Finding the Salt Master
  62. ~~~~~~~~~~~~~~~~~~~~~~~
  63. When a minion starts, by default it searches for a system that resolves to the ``salt`` hostname on the network.
  64. If found, the minion initiates the handshake and key authentication process with the Salt master.
  65. This means that the easiest configuration approach is to set internal DNS to resolve the name ``salt`` back to the Salt Master IP.
  66. Otherwise, the minion configuration file will need to be edited so that the
  67. configuration option ``master`` points to the DNS name or the IP of the Salt Master:
  68. .. note::
  69. The default location of the configuration files is ``/etc/salt``. Most
  70. platforms adhere to this convention, but platforms such as FreeBSD and
  71. Microsoft Windows place this file in different locations.
  72. ``/etc/salt/minion:``
  73. .. code-block:: yaml
  74. master: saltmaster.example.com
  75. Setting up a Salt Minion
  76. ~~~~~~~~~~~~~~~~~~~~~~~~
  77. .. note::
  78. The Salt Minion can operate with or without a Salt Master. This walk-through
  79. assumes that the minion will be connected to the master, for information on
  80. how to run a master-less minion please see the master-less quick-start guide:
  81. :ref:`Masterless Minion Quickstart <masterless-quickstart>`
  82. Now that the master can be found, start the minion in the same way as the
  83. master; with the platform init system or via the command line directly:
  84. As a daemon:
  85. .. code-block:: bash
  86. salt-minion -d
  87. In the foreground in debug mode:
  88. .. code-block:: bash
  89. salt-minion -l debug
  90. .. _minion-id-generation:
  91. When the minion is started, it will generate an ``id`` value, unless it has
  92. been generated on a previous run and cached (in ``/etc/salt/minion_id`` by
  93. default). This is the name by which the minion will attempt
  94. to authenticate to the master. The following steps are attempted, in order to
  95. try to find a value that is not ``localhost``:
  96. 1. The Python function ``socket.getfqdn()`` is run
  97. 2. ``/etc/hostname`` is checked (non-Windows only)
  98. 3. ``/etc/hosts`` (``%WINDIR%\system32\drivers\etc\hosts`` on Windows hosts) is
  99. checked for hostnames that map to anything within :strong:`127.0.0.0/8`.
  100. If none of the above are able to produce an id which is not ``localhost``, then
  101. a sorted list of IP addresses on the minion (excluding any within
  102. :strong:`127.0.0.0/8`) is inspected. The first publicly-routable IP address is
  103. used, if there is one. Otherwise, the first privately-routable IP address is
  104. used.
  105. If all else fails, then ``localhost`` is used as a fallback.
  106. .. note:: Overriding the ``id``
  107. The minion id can be manually specified using the :conf_minion:`id`
  108. parameter in the minion config file. If this configuration value is
  109. specified, it will override all other sources for the ``id``.
  110. Now that the minion is started, it will generate cryptographic keys and attempt
  111. to connect to the master. The next step is to venture back to the master server
  112. and accept the new minion's public key.
  113. .. _using-salt-key:
  114. Using salt-key
  115. ~~~~~~~~~~~~~~
  116. Salt authenticates minions using public-key encryption and authentication. For
  117. a minion to start accepting commands from the master, the minion keys need to be
  118. accepted by the master.
  119. The ``salt-key`` command is used to manage all of the keys on the
  120. master. To list the keys that are on the master:
  121. .. code-block:: bash
  122. salt-key -L
  123. The keys that have been rejected, accepted, and pending acceptance are listed.
  124. The easiest way to accept the minion key is to accept all pending keys:
  125. .. code-block:: bash
  126. salt-key -A
  127. .. note::
  128. Keys should be verified! Print the master key fingerprint by running ``salt-key -F master``
  129. on the Salt master. Copy the ``master.pub`` fingerprint from the Local Keys section,
  130. and then set this value as the :conf_minion:`master_finger` in the minion configuration
  131. file. Restart the Salt minion.
  132. On the master, run ``salt-key -f minion-id`` to print the fingerprint of the
  133. minion's public key that was received by the master. On the minion, run
  134. ``salt-call key.finger --local`` to print the fingerprint of the minion key.
  135. On the master:
  136. .. code-block:: bash
  137. # salt-key -f foo.domain.com
  138. Unaccepted Keys:
  139. foo.domain.com: 39:f9:e4:8a:aa:74:8d:52:1a:ec:92:03:82:09:c8:f9
  140. On the minion:
  141. .. code-block:: bash
  142. # salt-call key.finger --local
  143. local:
  144. 39:f9:e4:8a:aa:74:8d:52:1a:ec:92:03:82:09:c8:f9
  145. If they match, approve the key with ``salt-key -a foo.domain.com``.
  146. Sending the First Commands
  147. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  148. Now that the minion is connected to the master and authenticated, the master
  149. can start to command the minion.
  150. Salt commands allow for a vast set of functions to be executed and for
  151. specific minions and groups of minions to be targeted for execution.
  152. The ``salt`` command is comprised of command options, target specification,
  153. the function to execute, and arguments to the function.
  154. A simple command to
  155. start with looks like this:
  156. .. code-block:: bash
  157. salt '*' test.version
  158. The ``*`` is the target, which specifies all minions.
  159. ``test.version`` tells the minion to run the :py:func:`test.version
  160. <salt.modules.test.version>` function.
  161. In the case of ``test.version``, ``test`` refers to a :ref:`execution module
  162. <writing-execution-modules>`. ``version`` refers to the :py:func:`version
  163. <salt.modules.test.version>` function contained in the aforementioned ``test``
  164. module.
  165. .. note::
  166. Execution modules are the workhorses of Salt. They do the work on the
  167. system to perform various tasks, such as manipulating files and restarting
  168. services.
  169. The result of running this command will be the master instructing all of the
  170. minions to execute :py:func:`test.version <salt.modules.test.version>` in parallel
  171. and return the result. Using :py:func:`test.version <salt.modules.test.version>`
  172. is a good way of confirming that a minion is connected, and reaffirm to the user
  173. the salt version(s) they have installed on the minions.
  174. .. note::
  175. Each minion registers itself with a unique minion ID. This ID defaults to
  176. the minion's hostname, but can be explicitly defined in the minion config as
  177. well by using the :conf_minion:`id` parameter.
  178. Of course, there are hundreds of other modules that can be called just as
  179. ``test.version`` can. For example, the following would return disk usage on all
  180. targeted minions:
  181. .. code-block:: bash
  182. salt '*' disk.usage
  183. Getting to Know the Functions
  184. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  185. Salt comes with a vast library of functions available for execution, and Salt
  186. functions are self-documenting. To see what functions are available on the
  187. minions execute the :py:func:`sys.doc <salt.modules.sys.doc>` function:
  188. .. code-block:: bash
  189. salt '*' sys.doc
  190. This will display a very large list of available functions and documentation on
  191. them.
  192. .. note::
  193. Module documentation is also available :ref:`on the web <all-salt.modules>`.
  194. These functions cover everything from shelling out to package management to
  195. manipulating database servers. They comprise a powerful system management API
  196. which is the backbone to Salt configuration management and many other aspects
  197. of Salt.
  198. .. note::
  199. Salt comes with many plugin systems. The functions that are available via
  200. the ``salt`` command are called :ref:`Execution Modules <all-salt.modules>`.
  201. Helpful Functions to Know
  202. ~~~~~~~~~~~~~~~~~~~~~~~~~
  203. The :mod:`cmd <salt.modules.cmdmod>` module contains
  204. functions to shell out on minions, such as :mod:`cmd.run
  205. <salt.modules.cmdmod.run>` and :mod:`cmd.run_all
  206. <salt.modules.cmdmod.run_all>`:
  207. .. code-block:: bash
  208. salt '*' cmd.run 'ls -l /etc'
  209. The ``pkg`` functions automatically map local system package managers to the
  210. same salt functions. This means that ``pkg.install`` will install packages via
  211. ``yum`` on Red Hat based systems, ``apt`` on Debian systems, etc.:
  212. .. code-block:: bash
  213. salt '*' pkg.install vim
  214. .. note::
  215. Some custom Linux spins and derivatives of other distributions are not properly
  216. detected by Salt. If the above command returns an error message saying that
  217. ``pkg.install`` is not available, then you may need to override the pkg
  218. provider. This process is explained :ref:`here <state-providers>`.
  219. The :mod:`network.interfaces <salt.modules.network.interfaces>` function will
  220. list all interfaces on a minion, along with their IP addresses, netmasks, MAC
  221. addresses, etc:
  222. .. code-block:: bash
  223. salt '*' network.interfaces
  224. Changing the Output Format
  225. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  226. The default output format used for most Salt commands is called the ``nested``
  227. outputter, but there are several other outputters that can be used to change
  228. the way the output is displayed. For instance, the ``pprint`` outputter can be
  229. used to display the return data using Python's ``pprint`` module:
  230. .. code-block:: bash
  231. root@saltmaster:~# salt myminion grains.item pythonpath --out=pprint
  232. {'myminion': {'pythonpath': ['/usr/lib64/python2.7',
  233. '/usr/lib/python2.7/plat-linux2',
  234. '/usr/lib64/python2.7/lib-tk',
  235. '/usr/lib/python2.7/lib-tk',
  236. '/usr/lib/python2.7/site-packages',
  237. '/usr/lib/python2.7/site-packages/gst-0.10',
  238. '/usr/lib/python2.7/site-packages/gtk-2.0']}}
  239. The full list of Salt outputters, as well as example output, can be found
  240. :ref:`here <all-salt.output>`.
  241. ``salt-call``
  242. ~~~~~~~~~~~~~
  243. The examples so far have described running commands from the Master using the
  244. ``salt`` command, but when troubleshooting it can be more beneficial to login
  245. to the minion directly and use ``salt-call``.
  246. Doing so allows you to see the minion log messages specific to the command you
  247. are running (which are *not* part of the return data you see when running the
  248. command from the Master using ``salt``), making it unnecessary to tail the
  249. minion log. More information on ``salt-call`` and how to use it can be found
  250. :ref:`here <using-salt-call>`.
  251. Grains
  252. ~~~~~~
  253. Salt uses a system called :ref:`Grains <targeting-grains>` to build up
  254. static data about minions. This data includes information about the operating
  255. system that is running, CPU architecture and much more. The grains system is
  256. used throughout Salt to deliver platform data to many components and to users.
  257. Grains can also be statically set, this makes it easy to assign values to
  258. minions for grouping and managing.
  259. A common practice is to assign grains to minions to specify what the role or
  260. roles a minion might be. These static grains can be set in the minion
  261. configuration file or via the :mod:`grains.setval <salt.modules.grains.setval>`
  262. function.
  263. Targeting
  264. ~~~~~~~~~
  265. Salt allows for minions to be targeted based on a wide range of criteria. The
  266. default targeting system uses globular expressions to match minions, hence if
  267. there are minions named ``larry1``, ``larry2``, ``curly1``, and ``curly2``, a
  268. glob of ``larry*`` will match ``larry1`` and ``larry2``, and a glob of ``*1``
  269. will match ``larry1`` and ``curly1``.
  270. Many other targeting systems can be used other than globs, these systems
  271. include:
  272. Regular Expressions
  273. Target using PCRE-compliant regular expressions
  274. Grains
  275. Target based on grains data:
  276. :ref:`Targeting with Grains <targeting-grains>`
  277. Pillar
  278. Target based on pillar data:
  279. :ref:`Targeting with Pillar <targeting-pillar>`
  280. IP
  281. Target based on IP address/subnet/range
  282. Compound
  283. Create logic to target based on multiple targets:
  284. :ref:`Targeting with Compound <targeting-compound>`
  285. Nodegroup
  286. Target with nodegroups:
  287. :ref:`Targeting with Nodegroup <targeting-nodegroups>`
  288. The concepts of targets are used on the command line with Salt, but also
  289. function in many other areas as well, including the state system and the
  290. systems used for ACLs and user permissions.
  291. Passing in Arguments
  292. ~~~~~~~~~~~~~~~~~~~~
  293. Many of the functions available accept arguments which can be passed in on
  294. the command line:
  295. .. code-block:: bash
  296. salt '*' pkg.install vim
  297. This example passes the argument ``vim`` to the pkg.install function. Since
  298. many functions can accept more complex input than just a string, the arguments
  299. are parsed through YAML, allowing for more complex data to be sent on the
  300. command line:
  301. .. code-block:: bash
  302. salt '*' test.echo 'foo: bar'
  303. In this case Salt translates the string 'foo: bar' into the dictionary
  304. "{'foo': 'bar'}"
  305. .. note::
  306. Any line that contains a newline will not be parsed by YAML.
  307. Salt States
  308. ===========
  309. Now that the basics are covered the time has come to evaluate ``States``. Salt
  310. ``States``, or the ``State System`` is the component of Salt made for
  311. configuration management.
  312. The state system is already available with a basic Salt setup, no additional
  313. configuration is required. States can be set up immediately.
  314. .. note::
  315. Before diving into the state system, a brief overview of how states are
  316. constructed will make many of the concepts clearer. Salt states are based
  317. on data modeling and build on a low level data structure that is used to
  318. execute each state function. Then more logical layers are built on top of
  319. each other.
  320. The high layers of the state system which this tutorial will
  321. cover consists of everything that needs to be known to use states, the two
  322. high layers covered here are the `sls` layer and the highest layer
  323. `highstate`.
  324. Understanding the layers of data management in the State System will help with
  325. understanding states, but they never need to be used. Just as understanding
  326. how a compiler functions assists when learning a programming language,
  327. understanding what is going on under the hood of a configuration management
  328. system will also prove to be a valuable asset.
  329. The First SLS Formula
  330. ---------------------
  331. The state system is built on SLS (SaLt State) formulas. These formulas are built out in
  332. files on Salt's file server. To make a very basic SLS formula open up a file
  333. under /srv/salt named vim.sls. The following state ensures that vim is installed
  334. on a system to which that state has been applied.
  335. ``/srv/salt/vim.sls:``
  336. .. code-block:: yaml
  337. vim:
  338. pkg.installed
  339. Now install vim on the minions by calling the SLS directly:
  340. .. code-block:: bash
  341. salt '*' state.apply vim
  342. This command will invoke the state system and run the ``vim`` SLS.
  343. Now, to beef up the vim SLS formula, a ``vimrc`` can be added:
  344. ``/srv/salt/vim.sls:``
  345. .. code-block:: yaml
  346. vim:
  347. pkg.installed: []
  348. /etc/vimrc:
  349. file.managed:
  350. - source: salt://vimrc
  351. - mode: 644
  352. - user: root
  353. - group: root
  354. Now the desired ``vimrc`` needs to be copied into the Salt file server to
  355. ``/srv/salt/vimrc``. In Salt, everything is a file, so no path redirection needs
  356. to be accounted for. The ``vimrc`` file is placed right next to the ``vim.sls`` file.
  357. The same command as above can be executed to all the vim SLS formulas and now
  358. include managing the file.
  359. .. note::
  360. Salt does not need to be restarted/reloaded or have the master manipulated
  361. in any way when changing SLS formulas. They are instantly available.
  362. Adding Some Depth
  363. -----------------
  364. Obviously maintaining SLS formulas right in a single directory at the root of
  365. the file server will not scale out to reasonably sized deployments. This is
  366. why more depth is required. Start by making an nginx formula a better way,
  367. make an nginx subdirectory and add an init.sls file:
  368. ``/srv/salt/nginx/init.sls:``
  369. .. code-block:: yaml
  370. nginx:
  371. pkg.installed: []
  372. service.running:
  373. - require:
  374. - pkg: nginx
  375. A few concepts are introduced in this SLS formula.
  376. First is the service statement which ensures that the ``nginx`` service is running.
  377. Of course, the nginx service can't be started unless the package is installed --
  378. hence the ``require`` statement which sets up a dependency between the two.
  379. The ``require`` statement makes sure that the required component is executed before
  380. and that it results in success.
  381. .. note::
  382. The `require` option belongs to a family of options called `requisites`.
  383. Requisites are a powerful component of Salt States, for more information
  384. on how requisites work and what is available see:
  385. :ref:`Requisites <requisites>`
  386. Also evaluation ordering is available in Salt as well:
  387. :ref:`Ordering States<ordering>`
  388. This new sls formula has a special name -- ``init.sls``. When an SLS formula is
  389. named ``init.sls`` it inherits the name of the directory path that contains it.
  390. This formula can be referenced via the following command:
  391. .. code-block:: bash
  392. salt '*' state.apply nginx
  393. .. note::
  394. :py:func:`state.apply <salt.modules.state.apply_>` is just another remote
  395. execution function, just like :py:func:`test.version <salt.modules.test.version>`
  396. or :py:func:`disk.usage <salt.modules.disk.usage>`. It simply takes the
  397. name of an SLS file as an argument.
  398. Now that subdirectories can be used, the ``vim.sls`` formula can be cleaned up.
  399. To make things more flexible, move the ``vim.sls`` and vimrc into a new subdirectory
  400. called ``edit`` and change the ``vim.sls`` file to reflect the change:
  401. ``/srv/salt/edit/vim.sls:``
  402. .. code-block:: yaml
  403. vim:
  404. pkg.installed
  405. /etc/vimrc:
  406. file.managed:
  407. - source: salt://edit/vimrc
  408. - mode: 644
  409. - user: root
  410. - group: root
  411. Only the source path to the vimrc file has changed. Now the formula is
  412. referenced as ``edit.vim`` because it resides in the edit subdirectory.
  413. Now the edit subdirectory can contain formulas for emacs, nano, joe or any other
  414. editor that may need to be deployed.
  415. Next Reading
  416. ------------
  417. Two walk-throughs are specifically recommended at this point. First, a deeper
  418. run through States, followed by an explanation of Pillar.
  419. 1. :ref:`Starting States <starting-states>`
  420. 2. :ref:`Pillar Walkthrough <pillar-walk-through>`
  421. An understanding of Pillar is extremely helpful in using States.
  422. Getting Deeper Into States
  423. --------------------------
  424. Two more in-depth States tutorials exist, which delve much more deeply into States
  425. functionality.
  426. 1. :ref:`How Do I Use Salt States? <starting-states>`, covers much
  427. more to get off the ground with States.
  428. 2. The :ref:`States Tutorial<states-tutorial>` also provides a
  429. fantastic introduction.
  430. These tutorials include much more in-depth information including templating
  431. SLS formulas etc.
  432. So Much More!
  433. =============
  434. This concludes the initial Salt walk-through, but there are many more things still
  435. to learn! These documents will cover important core aspects of Salt:
  436. - :ref:`Pillar<pillar>`
  437. - :ref:`Job Management<jobs>`
  438. A few more tutorials are also available:
  439. - :ref:`Remote Execution Tutorial<writing-execution-modules>`
  440. - :ref:`Standalone Minion<tutorial-standalone-minion>`
  441. This still is only scratching the surface, many components such as the reactor
  442. and event systems, extending Salt, modular components and more are not covered
  443. here. For an overview of all Salt features and documentation, look at the
  444. :ref:`Table of Contents<table-of-contents>`.