- Salt Cookbook
- Anirban Saha
- 599字
- 2021-07-16 13:21:57
Setting and using variables in states
Variables are one of the most important features of any language or tool used to set user-defined data and manipulate it as and when required. In this recipe, you will learn how to set variables in Salt states and use the variables to do some advanced task.
How to do it...
- Configure a minion in the staging environment and create a pillar and state with the name
user
. In the minion, configure the following grain:server_type: db
- Edit the
/opt/salt-cookbook/pillar/staging/user/init.sls
file to have the following contents:app_user_list: optimus: uid: 7001 passwd: $1$Cf1V2QaF$.qyeAQ34CLqyvEnes7/VH1 bumblebee: uid: 7002 passwd: $1$KvbpASt.$L97XRqLVc0OaspatEE/n4/ db_user_list: megatron: uid: 7001 passwd: $1$8J9bAeG6$HMMV.EoMycJyLL.pb6kHj0 cyclonus: uid: 7002 passwd: $1$2HqtGifG$MF3WHFSOmKG4gksHOVvA30
- Edit the
/opt/salt-cookbook/staging/user/init.sls
file to have the following contents:{% if grains['server_type'] == 'app' %} {% set user_list = 'app_user_list' %} {% elif grains['server_type'] == 'db' %} {% set user_list = 'db_user_list' %} {% endif %} {% for user, details in pillar[user_list].iteritems() %} {{ user }}: user.present: - home: /home/{{ user }} - uid: {{ details['uid'] }} - password: {{ details['passwd'] }} - shell: /bin/bash {% endfor %}
- Apply the state to the minion:
[root@salt-master ~]# salt 'stgdc1app02' state.sls user \ saltenv=staging --state-output=terse stgdc1app02: Name: cyclonus - Function: user.present - Result: Changed Name: megatron - Function: user.present - Result: Changed Summary ------------ Succeeded: 2 Failed: 0 ------------ Total: 2
How it works...
In this recipe, we introduced the concept of variables in Salt. The objective of the recipe is to have two different lists of users in the pillar definition, one for application users and one for database users. In the state, depending on the type of the server, a variable is set with the name of the user list and the variable is used to run iteration in the state to add the users in the list.
First, we configured the minion to have a grain called server_type
with the value of db
, which tells Salt that this is a database server.
Next, we populated the pillar file with a list of users similar to the recipe Using iterations in states. However, there are two such lists here, one with the name app_user_list
for application servers and the other is db_user_list
for database servers.
Next, we configured the state with an initial block of conditionals:
{% if grains['server_type'] == 'app' %} {% set user_list = 'app_user_list' %} {% elif grains['server_type'] == 'db' %} {% set user_list = 'db_user_list' %} {% endif %}
Here, we set a variable with the statement:
{% set user_list = 'app_user_list' %}
The set
keyword is used to set the variable with user_list
as the variable name and the value is app_user_list
or db_user_list
depending on the type of the server.
Next, we use this variable in iterating through the user list from the pillar data:
{% for user, details in pillar[user_list].iteritems() %}
Since the server is a database host, the value of db_user_list
is stored in the variable user_list
. So, instead of using pillar['db_user_list'].iteritems()
, we are using pillar[user_list].iteritems()
. Do note that there are no quotes when the variable name is mentioned instead of the generic name of the list. This is a much cleaner approach to doing similar tasks than to use conditionals with iterations. Variables can be used for numerous other tasks in Salt.
Finally, we applied the state to the minion, and the database users from the list have been created on the host.
See also
- The Using iterations in states and Using conditionals in states and pillars recipes, to learn about conditionals and iterations
- The Testing a state run before applying to minions recipe, to learn about how to test state run before applying to minions
- 數據科學實戰手冊(R+Python)
- JBoss Weld CDI for Java Platform
- Node.js Design Patterns
- C# 2012程序設計實踐教程 (清華電腦學堂)
- OpenCV 3和Qt5計算機視覺應用開發
- Production Ready OpenStack:Recipes for Successful Environments
- 基于Swift語言的iOS App 商業實戰教程
- PHP編程基礎與實踐教程
- Python Interviews
- SciPy Recipes
- Julia數據科學應用
- Learning Image Processing with OpenCV
- Implementing Microsoft Dynamics NAV(Third Edition)
- Web開發新體驗
- Java Web 從入門到項目實踐(超值版)