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

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
主站蜘蛛池模板: 托克逊县| 开鲁县| 玉龙| 大同市| 淄博市| 洪湖市| 文昌市| 永修县| 三门县| 隆尧县| 正宁县| 乐业县| 安阳市| 灌南县| 大城县| 彝良县| 渝北区| 鄂托克前旗| 丹寨县| 张家港市| 都匀市| 姚安县| 大同市| 乐清市| 阳春市| 景洪市| 县级市| 永清县| 衡水市| 盖州市| 天门市| 溆浦县| 那坡县| 阜新| 枣庄市| 西丰县| 行唐县| 汾阳市| 北宁市| 拉萨市| 兴安县|