Using conditionals in states and pillars
One of the best advantages of any programming language or configuration tool is its ability to make decisions based on various properties, and apply configurations, or perform tasks, based on these decisions. In this recipe, you will learn how to leverage the decision-making ability of Salt and use it to your advantage.
How to do it...
We will use the same minion as the previous recipe for this task:
Add a custom grain to the minion with the
server_type
key and value asapp
:server_type: app
In the staging environment, create two pillars named
groups
andusers
. Also, create two states namedgroup
anduser
. Add them totop.sls
for bothstates
andpillars
to be available to all nodes.Edit
/opt/salt-cookbook/pillar/staging/user/init.sls
to have the following content:users: {% if grains['server_type'] == 'app' %} stg_user: stg-app {% elif grains['server_type'] == 'db' %} stg_user: stg-db {% endif %} stg_user_passwd: '$1$lhxadPoh$d5tZktsF/eI08tqiwmwBo0...