- 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:
- 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'
- 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'] }}
- 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
- 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 %}
- 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
- 少兒人工智能趣味入門:Scratch 3.0動畫與游戲編程
- Boost程序庫完全開發(fā)指南:深入C++”準(zhǔn)”標(biāo)準(zhǔn)庫(第5版)
- Learn TypeScript 3 by Building Web Applications
- Spring 5.0 By Example
- SQL學(xué)習(xí)指南(第3版)
- Java應(yīng)用開發(fā)與實踐
- 游戲程序設(shè)計教程
- 數(shù)據(jù)結(jié)構(gòu)(C語言)
- Learn React with TypeScript 3
- Unity 2D Game Development Cookbook
- Instant Lucene.NET
- 零基礎(chǔ)學(xué)Kotlin之Android項目開發(fā)實戰(zhàn)
- Visual Studio Code 權(quán)威指南
- ASP.NET求職寶典
- jQuery從入門到精通(微課精編版)