pyinfra 简单试用

测试下基于python 代码的集成

参考示例

  • s.py
from pyinfra.api import Config, Inventory, State
from pyinfra.api.connect import connect_all
from pyinfra.api.operation import add_op
from pyinfra.api.operations import run_ops
from pyinfra.api.facts import get_facts
from pyinfra.facts.server import Os
from pyinfra.operations import server

inventory = Inventory((["me"], {}))

config = Config(SUDO=True)

state = State(inventory=inventory, config=config)

connect_all(state)

result = add_op(
    state,
    server.service,
    name="Ensure uptime service is running",
    service="openresty",
    running=True
)

run_ops(state)


print(result)

print(get_facts(state, Os))

说明

pyinfra 运行速度以及灵活性还是挺不错的,基于python 代码相比ansible 的基于配置灵活性很高,值得尝试下,我是通过python api的,实际pyinfra 包含了cli 可以对于Inventory 以及Deploy (操作)区分,灵活性很不错

参考资料

https://github.com/pyinfra-dev/pyinfra

https://docs.pyinfra.com/en/3.x/api/index.html

posted on 2026-09-28 08:00  荣锋亮  阅读(7)  评论(0)    收藏  举报

导航