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

Using execution modules

Modules are the basic foundations of configuration management in Salt. Using Salt modules, we can configure systems from Salt state files, as well as from the command line. In this chapter, you will learn about how to use Salt modules from the command line.

How to do it...

Configure a minion in the staging environment. We will call it stgdc1log01.

  1. Run the following command to list all the cron entries for the root user on the minion:
    [root@salt-master ~]# salt 'stgdc1log01' cron.list_tab root
    stgdc1log01:
     ----------
     crons:
     env:
     pre:
     special:
    
  2. Run the following command to add a new cron entry for the root user:
    [root@salt-master ~]# salt 'stgdc1log01' cron.set_job root '00' '12' '*' \ '*' '*' 'find /var/log/ -mtime +30 -exec rm - rf {} \;'
    stgdc1log01:
     new
    
  3. Run the following command to verify that the new cron was added properly:
    [root@salt-master ~]# salt 'stgdc1log01' cron.list_tab root
    stgdc1log01:
     ----------
     crons:
     |_
     ----------
     cmd:
     find /var/log/ -mtime +30 -exec rm -rf {} \;
     comment:
     None
     daymonth:
     *
     dayweek:
     *
     hour:
     12
     identifier:
     None
     minute:
     0
     month:
     *
     env:
     pre:
     special:
    
  4. Run the following command to remove the cron entry:
    [root@salt-master ~]# salt 'stgdc1log01' cron.rm_job root 'find /var/log/ -mtime +30 -exec rm -rf {} \;'
    stgdc1log01:
     removed
    
  5. Run the following command to verify that it was removed:
    [root@salt-master ~]# salt 'stgdc1log01' cron.list_tab root
    stgdc1log01:
     ----------
     crons:
     env:
     pre:
     special:
    

How it works...

Salt modules are the entity that contains the parameters and information required to perform necessary configurations on minions. In Salt, modules are of two types: state and execution modules. In this recipe, you will learn about the details of execution modules and methods by which to use them.

Execution modules are the ones that can be used from the command line of the Salt master using the salt-binary command. A complete list of all the available Salt execution modules can be found at http://docs.saltstack.com/en/latest/ref/modules/all. The number of execution modules available in Salt is generally more than the number of state modules. Both the types of modules are extremely helpful in configuring minions.

The general structure of running an execution module consists of the salt command, the target minion using the various targeting methods, the module name along with the function, and the data being passed to the minion.

In this recipe, we made use of the cron execution module to perform some configuration related to crontab scheduling on the minion.

First, we listed the available crontab configurations for the root user:

[root@salt-master ~]# salt 'stgdc1log01' cron.list_tab root

The first word in the command is salt, which is the salt command. Next, we specified the minion to target for this action; this targeting can be performed by using the various methods demonstrated in Chapter 2, Writing Advanced Salt Configurations. The next entity in the command is what we are interested in, that is, cron.list_tab, which is our execution module implementation. Here, cron is the name of the module and list_tab is a function under the module. There are multiple functions available for all modules that can be looked up from the list of modules in the preceding link. We have then mentioned the user for which we want to retrieve the crontab list.

In the next few commands, we added a new cron entry for the root user, verified that it got added, and then removed the entry and again verified it. To carry out these tasks, we used a few more functions of the cron module, such as set_job and rm_job, with the proper number of data fields, which need to be passed for each of them. The number of fields of data that needed to be passed differ as per the function being used and are very well documented in the preceding link.

The list of execution modules in Salt is quite large and supports a wide range of tasks that can be performed on the minions. We will look at some of the most important of them in later chapters.

See also

  • The Targeting minions recipe in Chapter 2, Writing Advanced Salt Configurations, to learn about how to target minions
  • The Using state modules recipe, to learn how to use state modules
主站蜘蛛池模板: 清镇市| 利辛县| 麻城市| 竹北市| 廉江市| 涪陵区| 青海省| 祁连县| 独山县| 高邮市| 射阳县| 云龙县| 陇川县| 桂平市| 外汇| 军事| 清镇市| 洮南市| 桐乡市| 万山特区| 浙江省| 英德市| 平顶山市| 婺源县| 交城县| 阜新市| 牙克石市| 鲜城| 昌邑市| 佛冈县| 唐河县| 镇平县| 新晃| 留坝县| 图们市| 晋城| 梨树县| 莱阳市| 射阳县| 黑山县| 宁远县|