简体   繁体   English

以编程方式访问Saltstack Minion

[英]Accessing saltstack minion programmatically

I have been wrestling with this problem for the past week and I fear my solution is not conventional according to the SaltStack documentation. 在过去的一周中,我一直在努力解决这个问题,根据SaltStack文档,我担心我的解决方案不是传统的。 We have about 20 minions running on various servers throughout the country and need to be able to not only monitor them, but also issue commands and mysql queries from time to time. 我们在全国各地的各种服务器上运行着大约20个小兵,他们不仅需要能够监视它们,而且还需要不时发出命令和mysql查询。 This is very easy to do from the CLI via something like: 通过CLI可以很容易地通过以下方式完成此操作:

salt '[minion name here]' cmd.run "tail -4 /usr/local/bin/file.txt"

That would effectively return the last four line in file.txt on the server running that minion. 这将有效地返回运行该奴才的服务器上file.txt中的最后四行。 However, what we want to do next is have a script that periodically pulls this file down and caches it on salt master. 但是,我们接下来要做的是有一个脚本,该脚本会定期拉该文件并将其缓存在salt master上。 Since SaltStack is written in python it was a no-brainer to use the same language for our daemons/cron jobs. 由于SaltStack是用python编写的,因此毫无疑问地将相同的语言用于我们的守护程序/ cron作业。 However, the problem we are running into is that we would very much like a way of interfacing with SaltStack without having to resort to running a process from within our python script. 但是,我们遇到的问题是,我们非常希望与SaltStack进行接口连接,而不必诉诸于在python脚本中运行进程。 Currently we have the following line of code that does almost the same thing: 当前,我们有以下几行代码执行几乎相同的操作:

subprocess.Popen(['salt', minion, 'cmd.run', '"tail -4 /usr/local/bin/file.txt"', '--out', 'json'], stdout=subprocess.PIPE)

After reading into the documentation it has become apparent that there is a way to do this provided by SaltStack. 阅读文档后,很明显,SaltStack提供了一种方法来执行此操作。 The issue we're having is that we cannot figure out the code that is needed to actually run such a command without using the subprocess module. 我们面临的问题是,如果不使用subprocess模块​​,就无法找出实际运行这样的命令所需的代码。 Furthermore, we wish to also execute remote mysql queries on some of these minions, but we're so inexperienced (or so stupid) that we cannot decipher what the relevant code should be. 此外,我们还希望对其中的一些奴才执行远程mysql查询,但是我们经验不足(或者如此愚蠢),以至于我们无法理解相关代码应该是什么。

For the purpose of an example, we would like to list all databases located on one of our nodes. 出于示例目的,我们希望列出位于我们节点之一上的所有数据库。 We found the following two articles that explain how to do this, but we are confused as to what actually must be executed to get our final result. 我们找到了以下两篇文章来解释如何执行此操作,但是对于要获得最终结果实际上必须执行什么却感到困惑。

https://docs.saltstack.com/en/2015.8/ref/clients/index.html
https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.mysql.html

From the mysql salt modules we would expect to be able to use salt.modules.mysql.db_list , but according to the documentation that function does not accept any parameters. 我们期望从mysql salt模块可以使用salt.modules.mysql.db_list ,但是根据文档,该函数不接受任何参数。 How would we specify which minion we want to run the query on? 我们如何指定要在哪个小兵上运行查询? I thought there would be some way of instantiating a new instance of salt.modules.mysql that held a reference to the minion in question, but no such functionality seems to exist. 我以为有一种实例化salt.modules.mysql的新实例的方法,该实例持有对相关奴才的引用,但似乎不存在这样的功能。 Can anyone help us with this issue? 谁能帮助我们解决这个问题?

If you execute modules from cli and your minion ids start with something specific, like db-00 and db-01 , you would do something like that: 如果您从cli执行模块,并且您的minion ID以特定的内容开头,例如db-00db-01 ,那么您将执行以下操作:

salt 'db*' mysql.db_list

There are other approaches than relying on the minion id. 除了依赖minion id,还有其他方法。 Read more about targeting minions for further information. 详细了解有关定位小兵的更多信息。

From within python you can do the same as described within your linked docs . 在python中,您可以执行与链接文档中所述相同的操作。 A slightly adjusted example: 稍作调整的示例:

import salt.client

local = salt.client.LocalClient()
local.cmd('db-*', 'mysql.db_list')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM