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

  • Salt Cookbook
  • Anirban Saha
  • 691字
  • 2021-07-16 13:21:58

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
主站蜘蛛池模板: 中牟县| 沙洋县| 门源| 拉萨市| 关岭| 闵行区| 佳木斯市| 南京市| 班玛县| 云龙县| 富平县| 三门峡市| 大悟县| 安宁市| 三台县| 华蓥市| 高安市| 文登市| 屯留县| 沧源| 白银市| 临武县| 慈溪市| 黔西县| 崇义县| 嵩明县| 武安市| 蕲春县| 浦北县| 安福县| 荥经县| 宝清县| 大渡口区| 山东省| 岗巴县| 中西区| 丽水市| 永胜县| 葵青区| 正蓝旗| 烟台市|