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

  • Salt Cookbook
  • Anirban Saha
  • 668字
  • 2021-07-16 13:21:56

Using pillar data in states

How to do it...

  1. Create a new state user by creating a directory called user in the base directory of the qa environment and create an init.sls file in the user directory.
  2. 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'] }}
  3. 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
    
  4. Edit the /opt/salt-cookbook/pillar/qa/user/init.sls file and remove the following line:
    qa_user: qa-app
  5. 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'] }}
  6. 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
主站蜘蛛池模板: 庄浪县| 古田县| 南陵县| 诏安县| 车致| 泾源县| 于都县| 焦作市| 永仁县| 大悟县| 新兴县| 淮阳县| 武义县| 岳池县| 德令哈市| 青冈县| 新密市| 四川省| 孟连| 达拉特旗| 渑池县| 秭归县| 福清市| 三原县| 西宁市| 米泉市| 奉化市| 仙游县| 稷山县| 大化| 西充县| 平湖市| 柏乡县| 大悟县| 临漳县| 洪江市| 仪征市| 玛曲县| 安远县| 阳朔县| 怀柔区|