Salt part two
Thu, Jan 17, 2019 · 2 minute readlinuxsalt
Salt states
From the documentation,
The core of the Salt State system is the SLS, or SaLt State file. The SLS is a representation of the state in which a system should be in, and is set up to contain this data in a simple format. This is often called configuration management.
The default location for salt state files (sls) is defined per environment in master configuration, e.g.
file_roots:
base:
- /srv/salt/base/state/
In our environment all of the sls files for the base environment are under /srv/salt/base/state
State files can be further organised by creating directories under this base directory, e.g.
/srv/salt/base/state/
|
top.sls
+ core
|
+ network
| |
| init.sls
| |
| + resolver
| |
| init.sls
|
init.sls
motd.sls
top.sls is a special state file, it contains the state defintion for the top (or high) state.
It is usually just a call to other state files (all of which can also be run individually).
This is a portion of our top.sls
####
# 'base' environment
base:
'*':
# Apply to all minion
#
# Core states
- core
# SSH
- ssh/config
'type:lxc':
# Apply only to containers
- match: grain
- core/network/resolver
That demonstrates several aspects of the top state,
- it’s contents are targeted per environment.
- parts of the top state can be further targetted at groups of minions.
in the example above the core/network/resolver state is applied only to containers (type:lxc) as determined by a grain (match:grain).
init.sls state files are also special.
For a directory representing a state (core in the example above) salt will look for init.sls when applying the state.
e.g. when applying the core
state salt will execute init.sls
.
This is our core/init.sls
install_core_packages:
pkg.installed:
- pkgs:
# Ensure these packages are installed
- rsync
- curl
- wget
- unzip
# Include other state files
include:
# set date, time and timezone
- core.timedate
# set motd
- core.motd
# create management user accounts
- core.user.manage
Remaining state files are run by salt when requested.
Note that all of this is executed by the single core
entry in top.sls
.
Salt executes state file when the state.apply
module is called.
## Apply the top.sls to all minions
$ salt '*' state.apply
## Apply the core/motd state
$ salt '*' state.apply core.motd