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

  • Salt Cookbook
  • Anirban Saha
  • 585字
  • 2021-07-16 13:21:57

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
主站蜘蛛池模板: 墨脱县| 昔阳县| 遂溪县| 资中县| 于都县| 修水县| 闸北区| 五常市| 镇宁| 永平县| 尼木县| 浦江县| 托里县| 来凤县| 诸暨市| 开阳县| 防城港市| 曲麻莱县| 华蓥市| 如皋市| 琼中| 潜山县| 延安市| 长丰县| 重庆市| 罗城| 宁夏| 孝昌县| 布尔津县| 淅川县| 望都县| 建平县| 二连浩特市| 平南县| 渝北区| 永德县| 宾川县| 阳春市| 克拉玛依市| 伽师县| 乌鲁木齐市|