
外部精选
软件工程
#开发者实时流pyinfra-cli-api 发布:支持主机限制与标签的部署辅助工具
来源标题:How HN: Pyinfra-CLI helper to support deploy with host restriction and tags
pyinfra-cli-api 是一个基于 pyinfra-cli 的辅助工具,在原有命令行能力上增加了主机限制(limit)和标签(tags)支持。它允许在 Python 部署脚本中直接传入 inventory、limit、connect 等配置,并在执行操作时按标签筛选或指定主机。原有的 --limit 参数仍可用,可进一步收窄目标主机范围。
Hacker News 最新Timshel2026/12/1 · 今天3 分钟阅读
以下正文由 AI 翻译自 Hacker News 最新(原文版权归原站所有),可能存在偏差,重要内容请以原文为准。
一个用于 pyinfra-cli 的辅助工具,增加了额外功能。
思路是在 deploy 中初始化 CLI,从而可以直接传入一些配置。
from pyinfra.operations import files
from pyinfra_cli_api import cli
state = cli(
inventory="inventory.py"
)
state.add_op(
files.directory,
tags=["setup"],
name=f"Create basedir {basedir}",
path=basedir,
mode="755",
present=True,
)
state.run()
然后可以这样运行:
from pyinfra_cli_api import cli
state = cli(
limit=["dedi-nginx"]
)
CLI 仍然支持 --limit 参数,以便在需要时进一步限制范围。
可以单独指定一个 connect 参数来获取主机 facts(主机需要在 inventory 中定义)。
添加操作时,可以指定 host,而不是使用由 limit 定义的默认 host。
from pyinfra.operations import files
from pyinfra_cli_api import cli
state = cli(
limit=["nginx"],
connect=["@local"],
)
local = state.get_host("@local")
state.add_op(
files.download,
tags=["setup"],
name="Download Ketesa",
src=f"https://github.com/etkecc/ketesa/releases/download/{ketesa_version}/ketesa.tar.gz",
dest=f"{local_tmp}/ketesa-{ketesa_version}.tar.gz",
host=[local],
)
通过 tags 过滤操作
添加操作时可以定义 tags。如果使用了 --tags CLI 参数,那么只有带有匹配 tag 的操作才会被解析执行。
正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。
