Book Image

Salt Cookbook

By : Anirban Saha
Book Image

Salt Cookbook

By: Anirban Saha

Overview of this book

Table of Contents (18 chapters)
Salt Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

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:

  1. Add a custom grain to the minion with the server_type key and value as app:

    server_type: app
  2. In the staging environment, create two pillars named groups and users. Also, create two states named group and user. Add them to top.sls for both states and pillars to be available to all nodes.

  3. 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...