Configuring the Salt environment and pillar paths
In this recipe, we are going to configure the environment and pillar paths for Salt to use when we mention environments while performing Salt operations. Ideally, any infrastructure should have environments for development, testing, QA, production, and so on, for isolation of respective code and a seamless workflow. Salt enables us to implement this in a few simple steps by providing options to define environments for states and pillars.
How to do it...
In the primary configuration /etc/salt/master
file, we will configure the section called file_roots
. Under this parameter option, we will name each environment we want to configure and the path of the directory that will contain the configuration files for that environment.
- Let's create the directories we want to use as the environment paths and then configure four environments named
base
,production
,staging
, anddevelopment
as follows:[root@salt-master ~]# mkdir –p \ /opt/salt-cookbook/{base,production,staging,development}
- Edit
/etc/salt/master
to have following content:file_roots: base: - /opt/salt-cookbook/base production: - /opt/salt-cookbook/production staging: - /opt/salt-cookbook/staging development: - /opt/salt-cookbook/development
- Next, we will create the directories we want to use as pillar paths and configure them following the same environment conventions as follows:
[root@salt-master ~]# mkdir –p \ /opt/salt- cookbook/pillar/{base,production,staging,development}
- Edit
/etc/salt/master
to have following content:pillar_roots: base: - /opt/salt-cookbook/pillar/base production: - /opt/salt-cookbook/pillar/production staging: - /opt/salt-cookbook/pillar/staging development: - /opt/salt-cookbook/pillar/development
- We now restart the
salt-master
daemon for these changes to take effect:[root@salt-master ~]# service salt-master restart
How it works...
Environments in Salt are designed to keep the configuration files for each environment isolated into respective containers for different stages of deployment such as development, QA, staging, and production. Ideally, the configurations can be written in either of the environments such as staging, and after a proper test phases, they can be migrated to the production environment.
Here, we opted for the /opt/salt-cookbook
path to be the base directory of all our configurations. Next, we have chosen four environments for our infrastructure, namely, base
, staging
, production
, and development
. The base
environment is the default in Salt and the path /srv/salt
is taken as the base directory for all configurations if not configured explicitly as we have done earlier.
We then created the directory paths we want to use as the environment paths and mentioned them under the file_roots
parameter, specifying the environment name and the path that points the configuration files for that environment:
file_roots: <environment_name>: - <environment_directory_path>
Pillars are one of the best features of Salt, which enable us to isolate data that needs to be kept secure such as keys and passwords. Next, we have chosen the /opt/salt-cookbook/pillar
path to be our base directory for all pillar data and created directories for each environment that will contain pillar data, which is usable in that environment only.
We then edited the Salt master configuration file to include the environment names and their paths under the pillar_roots
section for Salt to identify them as the pillar configuration paths:
pillar_roots: <environment_name>: - <pillar_directory_path>
Finally, we restarted the Salt master daemon for the changes to take effect.
It is also to be noted that when executing the salt
command, the environment has to be mentioned for the minion(s). This is generally done with the saltenv
option on the command line. However, this parameter can also be specified in the minion configuration /etc/salt/minion
file, as follows:
environment: production
If this parameter is not set, the base environment is taken as the default environment.
See also
- The Using pillar data in states and Writing and retrieving pillar data recipes in Chapter 2, Writing Advanced Salt Configurations, to learn more about how to use pillars in states
- The Understanding and configuring Salt pillars recipe, for detailed examples of pillar configurations