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

Testing a state run before applying to minions

Sometimes, even the smallest of typos or mistakes in configurations can create the biggest problems in infrastructure if not tested and checked before applying them. In this recipe, you will learn how to test state configurations before applying them to minions.

How to do it...

We will make use of the minion and states configured in the previous recipe Setting and using variables in states. We will also assume that the minion is a fresh install with no previous configurations applied to it:

  1. Run the following command:
    [root@salt-master ~]# salt 'stgdc1app02' state.sls user \ saltenv=staging test=True
    stgdc1app02:
    ----------
     ID: cyclonus
     Function: user.present
     Result: None
     Comment: User cyclonus set to be added
     Changes:
    ----------
     ID: megatron
     Function: user.present
     Result: None
     Comment: User megatron set to be added
     Changes:
    
    Summary
    ------------
    Succeeded: 0
    Failed: 0
    Not Run: 2
    ------------
    Total: 2
    
  2. On the minion, edit the /etc/salt/minion file, uncomment the following line, and restart the minion daemon:
    test: True
  3. On the master, run the following command:
    [root@salt-master ~]# salt 'stgdc1app02' state.sls user \ saltenv=staging
    stgdc1app02:
    ----------
     ID: cyclonus
     Function: user.present
     Result: None
     Comment: User cyclonus set to be added
     Changes:
    ----------
     ID: megatron
     Function: user.present
     Result: None
     Comment: User megatron set to be added
     Changes:
    
    Summary
    ------------
    Succeeded: 0
    Failed: 0
    Not Run: 2
    ------------
    Total: 2
    
  4. On the master, run the following command:
    [root@salt-master ~]# salt 'stgdc1app02' state.sls user \ saltenv=staging --state-output=terse test=False
    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 demonstrated how to test our configuration before applying them to minions.

First, we run the command to apply the user state to the minion, but with the following parameter:

test=True

The output of the command shows us the changes that will be applied to the minion on running the states but does not actually apply them. This is clear by the following lines:

stgdc1app02:
----------
 Result: None
 Comment: User cyclonus set to be added
----------
 Result: None
 Comment: User megatron set to be added

Summary
------------
Succeeded: 0
Failed: 0
Not Run: 2
------------
Total: 2

It clearly tells us that the users are set to be added, but has not been added yet. The Results key has the value of None showing that there was no result because nothing was actually done. Also, the following two lines show that the states were not applied:

Succeeded: 0
Not Run:   2

Next, we see how to set the test parameter on the minion. We edited the main minion configuration file /etc/salt/minion and uncommented the line:

test: True

This tells the minion that all states applied on it from the master will be in test mode, that is, no configuration will be actually applied. We then run the command to apply the state to the minion without the test parameter and see that the state runs in test mode because the minion is set to test mode.

Next, we see how to override the minion's test parameter by setting the test parameter with the salt command. We set the test parameter to be False, and when the state is applied to the minion, the salt command's test parameter overrides the test parameter of the minion and the state gets applied successfully.

See also

  • The Applying Salt states to minions recipe, in Chapter 1, Salt Architecture and Components, to learn more about applying states to minions
  • The Configuring nodegroups recipe, to learn how to configure nodegroups to group similar nodes for targeting minions
主站蜘蛛池模板: 三穗县| 漳浦县| 达州市| 大余县| 兴业县| 武威市| 莎车县| 时尚| 中超| 顺平县| 孝义市| 建平县| 翁源县| 岳西县| 开化县| 额尔古纳市| 全椒县| 宝清县| 张北县| 福贡县| 汉川市| 富宁县| 吉林市| 临沧市| 玉溪市| 岱山县| 侯马市| 盐边县| 乐业县| 大城县| 边坝县| 金华市| 额敏县| 禹城市| 太仆寺旗| 白水县| 武宁县| 九龙坡区| 进贤县| 会东县| 临猗县|