- Salt Cookbook
- Anirban Saha
- 668字
- 2021-07-16 13:21:56
Using pillar data in states
How to do it...
- Create a new state user by creating a directory called
user
in the base directory of theqa
environment and create aninit.sls
file in theuser
directory. - Edit
/opt/salt-cookbook/qa/user/init.sls
and populate it with the following entries:qa_deploy_user: user.present: - name: {{ pillar['users']['qa_user'] }} - password: {{ pillar['users']['qa_user_password'] }}
- Run the following command to apply the state to the minion:
[root@salt-master ~]# salt 'salt-minion' state.sls user \ saltenv=qa --state-output=terse salt-minion: Name: qa-app - Function: user.present - Result: Changed Summary ------------ Succeeded: 1 Failed: 0 ------------ Total: 1
- Edit the
/opt/salt-cookbook/pillar/qa/user/init.sls
file and remove the following line:qa_user: qa-app
- Reopen the
/opt/salt-cookbook/qa/user/init.sls
file and edit it to have the following entries:qa_deploy_user: user.present: - name: {{ salt['pillar.get']('users:qa_user', 'qa- deploy-user') }} - password: {{ pillar['users']['qa_user_password'] }}
- Run the following command to apply the modified state to the minion:
[root@salt-master ~]# salt 'salt-minion' state.sls user \ saltenv=qa --state-output=terse salt-minion: Name: qa-deploy-user - Function: user.present - Result: Changed Summary ------------ Succeeded: 1 Failed: 0 ------------ Total: 1
How it works...
In this recipe, we learn about the various methods to use the pillar data in states. First, we configured a state called user
and configured a user with the generic name qa_deploy_user
. We used the basic way to use the pillar data in states as seen in Chapter 1, Salt Architecture and Components:
- name: {{ pillar['users']['qa_user'] }} - password: {{ pillar['users']['qa_user_password'] }}
We use the pillar
keyword with the YAML formatted keys from the pillar files. Note that from Chapter 1, Salt Architecture and Components that the method to access the value of the pillar is to use the YAML keys and not the pillar directory name, for example, here although the pillar directory is named user
, we use users
while trying to access the values.
We then apply the state to the minion with the –state-output=terse
parameter to shorten the output.
Next, we see a different way to use pillars. Here, we see how to specify a default value for a pillar
key if the value of the key is not set in the pillar files. To demonstrate this, we have just modified the name
key in the user
state file to look like the following:
- name: {{ salt['pillar.get']('users:qa_user', 'qa-deploy-user') }}
As seen earlier in the text, a new function called pillar.get
has been used with the keyword salt
instead of pillar
. The keys are written with the delimiter as a colon, that is, ['users'][ 'qa_user']
is written as 'users:qa_user'
, and the default value is mentioned after a comma. This definition tells Salt to use the value of qa-deploy-user
for the keys 'users:qa_user'
if the value of these keys is not set in the pillar files.
To demonstrate this, we removed the mentioned key from the pillar file and then applied the state to the minion. As a result, we see the user qa-deploy-user
being created on the minion instead of qa-user
, as the value for that key was not found in the pillar file.
There's more...
Including other pillars in the pillar file
Pillars can be included in other pillar files as if it was defined in the same file. If two pillars called group
and user
are configured, a pillar group can be included in the pillar user by inserting the following entry in to the pillar file, for example, init.sls
:
include: - group
Setting pillar data at the command line
Pillar data can also be set at the command line if required. The following command can be used to achieve the same:
[root@salt-master ~]# salt 'salt-minion' state.sls user \ pillar='{"qa_user": "qa-deploy-user"}'
Here, qa_user
is the key and qa-deploy-user
is the value. Although this procedure is not very efficient to use in production, it can be used to perform development
and qa
tasks.
See also
- The Writing and retrieving pillar data recipe, to learn about how to write and retrieve pillar data
- Chapter 4, General Administration Tasks, for demonstrations about how to use pillar data in Salt states
- The Using grains in states recipe to learn about how to use grains in states
- 自然語言處理實戰:預訓練模型應用及其產品化
- Spring Boot開發與測試實戰
- TypeScript Blueprints
- 樂學Web編程:網站制作不神秘
- Learning Python Design Patterns(Second Edition)
- HTML5+CSS3+JavaScript Web開發案例教程(在線實訓版)
- 微信小程序全棧開發技術與實戰(微課版)
- 編程與類型系統
- ElasticSearch Cookbook(Second Edition)
- Unity 2018 Augmented Reality Projects
- PrimeFaces Blueprints
- JavaScript機器人編程指南
- Python Machine Learning Blueprints:Intuitive data projects you can relate to
- 現代CPU性能分析與優化
- Android智能手機APP界面設計實戰教程