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

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
主站蜘蛛池模板: 固镇县| 湖南省| 甘孜| 安福县| 泰和县| 玉屏| 日喀则市| 白银市| 沿河| 南昌县| 龙泉市| 前郭尔| 东明县| 永嘉县| 玉环县| 格尔木市| 甘谷县| 巢湖市| 黄龙县| 万安县| 建始县| 贵定县| 西乌珠穆沁旗| 新密市| 平塘县| 永寿县| 沁水县| 慈利县| 区。| 延安市| 德令哈市| 昭通市| 榆中县| 牟定县| 临漳县| 广东省| 平果县| 潼关县| 阳新县| 什邡市| 荥经县|