ISalt: 交互式Salt编程
项目描述
ISalt是一个类似于IPython风格的控制台,用于简化Salt代码的调试甚至开发。
Salt代码通常使用许多双下划线(即_d_ouble _under_score)变量,例如__salt__
、__opts__
、__grains__
、__proxy__
或__pillar__
等,这些变量让您可以快速访问各种资源和功能。它们在不同的上下文中具有不同的含义 - 例如,在从属节点上的__opts__
与在主节点上的__opts__
是不同的对象;在从属节点上的__salt__
提供访问执行模块列表的权限,而在主节点上的__salt__
提供运行器,等等。
在处理这些变量时,主要困难在于它们只有在运行 Salt、有主服务器以及最终一个或多个节点运行的情况下才有意义。通常情况下,在编写新函数(使用这些特殊方法)时,您可能并不希望这些服务正在运行,或者只想快速调试某些内容而不推送代码到生产环境。
使用 ISalt,您可以轻松访问这些变量,只需执行 isalt 即可,例如:
$ isalt
>>> __salt__['test.ping']()
True
>>>
>>> __grains__['osfinger']
'Ubuntu-18.04'
换句话说,ISalt 是一个增强的 IPython 控制台,它为您提供了访问 Salt 代码中通常使用的 Salt 全局变量的权限。
安装
ISalt 通过 PyPI 分发,您可以通过执行以下命令进行安装:
$ pip install isalt
依赖关系
这些包没有特定的版本,因此不会干扰您的环境。它通常可以与任何版本很好地工作。
使用方法
您可以使用以下选项之一或多个配置数据或条件,优先级顺序如下:ISalt 配置文件、环境变量和 CLI 参数。
需要牢记的一个重要细节是,在节点端运行代码与在主服务器端运行代码之间的区别(我们可以进一步区分作为 Runner 执行的代码与为任意节点执行的执行模块——对于前者,您可能需要使用 --minion-id CLI 参数提供节点 ID)。
通常,当您在运行 Salt 节点的计算机上安装 ISalt 时,只需执行 $ isalt 即可。
当您想在主服务器端使用 ISalt 但要测试执行模块时,可以运行 $ isalt --on-master。
当您正在查看评估 Runner 代码时,您只能在主服务器端这样做,因此,您需要将控制台作为 $ isalt --master 启动。
您可以通过以下方式查看完整的 CLI 可选参数列表:
$ isalt -h
usage: isalt [-h] [--saltenv SALTENV] [--pillarenv PILLARENV] [-c CFG_FILE]
[-e CFG_FILE_ENV_VAR] [--minion-cfg MINION_CFG_FILE]
[--proxy-cfg PROXY_CFG_FILE] [--master-cfg MASTER_CFG_FILE]
[--minion] [--proxytype PROXYTYPE] [--proxy] [--sproxy]
[--master] [--local] [--minion-id MINION_ID] [--on-master]
ISalt console
optional arguments:
-h, --help show this help message and exit
--saltenv SALTENV Salt environment name.
--pillarenv PILLARENV
The Salt environment name to compile the Pillar from.
-c CFG_FILE, --cfg-file CFG_FILE
The absolute path to the ISalt config file.
-e CFG_FILE_ENV_VAR, --env-var CFG_FILE_ENV_VAR
The name of the environment variable pointing to the
ISalt config file.
--minion-cfg MINION_CFG_FILE
The absolute path to the Minion config file.
--proxy-cfg PROXY_CFG_FILE
The absolute path to the Proxy Minion config file.
--master-cfg MASTER_CFG_FILE
The absolute path to the Master config file.
--minion Prepare the Salt dunders for the Minion.
--proxy Prepare the Salt dunders for the Proxy Minion.
--sproxy Prepare the Salt dunders for the salt-sproxy (Master
side).
--master Prepare the Salt dunders for the Master.
--local Override the Minion config and use the local client.
This option loads the file roots config from the
Master file.
--minion-id MINION_ID
The Minion ID to compile the Salt dunders for. This
argument is optional, however it may fail when ISalt
is not able to determine the Minion ID, or take it
from the environment variable, etc.
--on-minion Whether should compile the dunders for the Minion
side, starting the ISalt console on the Minion
machine. The main difference is that the Pillar and
Grains are compiled locally, while when using --on-
master, it's using the local cached data.
--on-master Whether should compile the dunders for the Minion
side, starting the ISalt console on the Master
machine. This option is ignored when used in
conjunction with --master.
使用示例
在主服务器上使用 ISalt
以 isalt --master 开始。请记住,当前的 __salt__ 特殊方法映射到 Runner 函数,而不是执行模块。
$ isalt --master
In [1]: # execute the ``test.sleep`` Runner:
In [2]: # https://docs.saltstack.com/en/latest/ref/runners/all/salt.runners.test.html#module-salt.runners.test
In [3]: __salt__['test.sleep'](1)
1
Out[3]: True
在主服务器上使用 ISalt,加载(代理)节点特殊方法
在此模式下,您需要指定要使用和收集数据的节点 ID(否则它将使用本地计算机的名称)
$ isalt --on-master --minion-id jerry
对于代理节点,您必须传递 --proxy CLI 参数,例如:
$ isalt --on-master --minion-id edge-router --proxy
对于代理节点,为了正确加载 __salt__ 模块,您可能必须在代理配置文件中提供 proxytype(默认在 /etc/salt/proxy,或使用 --proxy-cfg 参数设置的不同路径)——或者使用 --proxytype CLI 参数,例如:
/etc/salt/proxy
proxy:
proxytype: napalm
然后作为 isalt --on-master --proxy --minion-id jerry 执行。
或者直接作为 isalt --on-master --proxytype napalm --minion-id jerry。
在(代理)节点上使用 ISalt
这是默认的ISalt模式,您不再需要提供Minion ID,因为它从本地机器收集,除非您想使用特定的一个。一如既往,您可以在代理/Minion配置文件、ISALT_MINION_ID环境变量或ISalt配置文件(作为minion_id选项)中设置Minion ID。
示例
$ echo $ISALT_MINION_ID
jerry
$ isalt
In [1]: __opts__['id']
Out[1]: 'jerry'
与Salt Super Proxy(Master端)结合使用ISalt
使用示例
$ isalt --sproxy
在这个交互式控制台中,您可以访问常用的Salt Master内建函数,以及salt-sproxy功能。作为快捷方式,您可以通过sproxy全局变量访问salt-sproxy核心功能。
>>> sproxy
<function execute at 0x7fd394075510>
>>> sproxy('*', preview_target=True)
['router1',
'router2']
以类似的方式,这简化了通过salt-sproxy执行任何Salt函数的过程,例如
>>> sproxy('router1', function='test.ping', static=True)
{'router1': True}
>>>
您还可以通过将ISalt配置文件中的值设置为role: sproxy来默认进入sproxy模式(有关更多信息,请参阅下一段)。
ISalt配置文件
上述所有选项均通过ISalt配置文件提供,默认为/etc/salt/isalt。要从特定路径读取文件,请使用-c / --cfg-file参数,例如
$ isalt -c /path/to/isalt/config/file
或者,作为替代,使用ISALT_CFG_FILE环境变量,例如
$ echo $ISALT_CFG_FILE
/path/to/isalt/config/file
$ isalt
更进一步,如果您想从不同的环境变量中读取配置文件的路径,请使用-e / --env-var参数
$ echo $ALTERNATIVE_ISALT_CFG_FILE
/path/to/another/isalt/config/file
$ isalt -e ALTERNATIVE_ISALT_CFG_FILE
ISalt配置文件示例
on_master: true
proxytype: dummy
proxy_cfg: /path/to/proxy/config
minion_cfg: /path/to/minion/config
master_cfg: /path/to/master/config
使用上述配置文件,您可以简化CLI的使用,例如,从isalt --on-master --proxy-cfg /path/to/proxy/config --proxytype dummy --minion-id jerry简化到只是isalt --minion-id jerry等。
环境变量
- ISALT_CFG_FILE
ISalt配置文件的绝对路径。
- ISALT_ROLE
Salt系统角色。选择:master、minion或proxy。
- ISALT_ON_MASTER
如果您在Master上运行ISalt。
- ISALT_MINION_ID
要使用的 Minion ID。
- ISALT_PROXYTYPE
要使用的代理 Minion 模块名称。
- ISALT_MASTER_CONFIG
主配置文件的绝对路径。
- ISALT_MINION_CONFIG
Minion 配置文件的绝对路径。
- ISALT_PROXY_MINION_CONFIG
代理 Minion 配置文件的绝对路径。
- ISALT_USE_CACHED_PILLAR
在代理/Minion 模式下启动时,在主节点上:是否使用可能已经为指定 Minion 可用的缓存 Pillar,或编译新鲜数据。
项目详情
下载文件
下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源分布
构建分布
isalt-2021.2.2.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 62130a0d61de65033a77fd231d32e95c12bdaec9503b8eba536ec3c0e7d56cd9 |
|
MD5 | 79e71907bfcaa46f1eea5aa300e4d33d |
|
BLAKE2b-256 | ee90698cab2bd866eaa1dc3af83fa4098d4b9689ac5f6a42dac1a91113a96fe7 |
isalt-2021.2.2-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 9606402cde4970d192ca29e0c7f857d1644f03f78d32608e6f55935c1e48d915 |
|
MD5 | a32c296fbdebc61bcd93d2506ed5b96e |
|
BLAKE2b-256 | 649cd3db4032d918a985cd5c7d74197bc09b5affb7d0623e57aa50c5010149bf |