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

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
主站蜘蛛池模板: 马关县| 称多县| 镇安县| 邹城市| 乡宁县| 清苑县| 辉县市| 洛宁县| 邮箱| 喀喇沁旗| 巴林右旗| 呼和浩特市| 灵石县| 高碑店市| 上饶县| 永春县| 龙口市| 宣威市| 旺苍县| 巴彦淖尔市| 来安县| 明光市| 离岛区| 奉化市| 古交市| 安乡县| 阳东县| 海门市| 甘洛县| 开鲁县| 华宁县| 古交市| 沭阳县| 道孚县| 财经| 藁城市| 碌曲县| 鹰潭市| 梅河口市| 张家界市| 沙雅县|