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

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...

  1. 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
  2. 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
  3. 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 %}
  4. 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
主站蜘蛛池模板: 磐石市| 长宁县| 定安县| 宁化县| 增城市| 内丘县| 和平县| 手游| 潮安县| 灵川县| 凤冈县| 万山特区| 汾阳市| 榆社县| 固安县| 佛学| 虹口区| 阿尔山市| 翼城县| 昔阳县| 城口县| 虞城县| 广宁县| 英山县| 柳林县| 凤庆县| 如东县| 安塞县| 平凉市| 梅州市| 宁晋县| 玛曲县| 依兰县| 孟村| 桃源县| 呼伦贝尔市| 朝阳市| 漯河市| 密云县| 札达县| 永嘉县|