官术网_书友最值得收藏!

  • Salt Cookbook
  • Anirban Saha
  • 602字
  • 2021-07-16 13:21:57

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'
  4. Edit /opt/salt-cookbook/staging/user/init.sls to have the following content:
    generic_user:
      user.present:
        - name: {{ pillar['users']['stg_user'] }}
        - shell: /bin/bash
        - home: /home/{{ pillar['users']['stg_user'] }}
        - password: {{ pillar['users']['stg_user_passwd'] }}
  5. Edit /opt/salt-cookbook/pillar/staging/group/init.sls to have the following content:
    groups:
      stg_app_group: stg-app-grp
      stg_db_group: stg-db-grp
  6. Edit /opt/salt-cookbook/staging/group/init.sls to have the following content:
    generic_group:
      group.present:
        {% if grains['server_type'] == 'app' %}
        - name: {{ pillar['groups']['stg_app_group'] }}
        {% elif grains['server_type'] == 'db' %}
        - name: {{ pillar['groups']['stg_db_group'] }}
        {% endif %}
  7. Apply the states to the minion:
    [root@salt-master ~]# salt 'stgdc1app01' state.highstate \ saltenv=staging --state-output=terse
    stgdc1app01:
     Name: stg-app-grp - Function: group.present - Result: Changed
     Name: stg-app - Function: user.present - Result: Changed
    
    Summary
    ------------
    Succeeded: 2
    Failed: 0
    ------------
    Total: 2
    

How it works...

In this recipe, we introduced conditionals in Salt. Being based on Python, the conditionals used in Salt are exactly the same as used in Python except for the opening and closing braces and percentage symbols used here, that is, {% and %}.

First, we added a custom grain called server_type with a value of app in the minion. We used this grain to make all decisions about our configuration in the rest of the recipe.

The condition block starts with:

{% if <condition> %}

Any other decision after that is made with:

{% elif <condition> %}

Finally, the block ends with:

{% endif %}

It is important to note that the if statements must be aligned with the Salt statement after it, failing this, the states will not execute.

This is correct:

{% if grains['server_type'] == 'app' %}
- name: {{ pillar['groups']['stg_app_group'] }}

However, this is incorrect:

{% if grains['server_type'] == 'app' %}
  - name: {{ pillar['groups']['stg_app_group'] }}

The purpose of this recipe is to demonstrate that conditionals can be applied to both pillars and states in Salt. For the group configuration, the pillar has normal data and the conditional is used in the state to make the decision based on the grain. In the user configuration, the pillar has the conditional and the state has normal data. Either of the combinations can be used and solely depends on the user's requirement.

For the condition being used in the example, we have taken the value of the grain and compared it with a string. However, there are numerous ways by which conditions can be used, for example, using all Python operators on various data types and using Python functions to manipulate the grain data, to name only a few.

Finally, we applied the states to the minion using the state.highstate module, which reads the states applicable to a minion from the top.sls file and applies them.

See also

  • Chapter 4, General Administration Tasks, to learn more about the application of conditionals
  • The Using Python functions in conditionals recipe, to learn how to use Python functions in conditionals
主站蜘蛛池模板: 西盟| 五指山市| 蒙山县| 禹城市| 哈尔滨市| 乌鲁木齐市| 宣武区| 望谟县| 化德县| 宾川县| 清河县| 平舆县| 乌鲁木齐市| 建昌县| 惠来县| 尉犁县| 津南区| 营山县| 墨竹工卡县| 武强县| 启东市| 永胜县| 确山县| 广德县| 诸暨市| 崇明县| 土默特右旗| 始兴县| 杭锦旗| 鄂温| 贡嘎县| 镇沅| 澎湖县| 丰都县| 如皋市| 邯郸市| 射洪县| 定州市| 庆元县| 丹凤县| 太仆寺旗|