Pyinfra:使用 Python 自动化基础设施
Pyinfra: Automate Infrastructure Using Python

原始链接: https://pyinfra.com/

Pyinfra 是一个基于 Python 的工具,用于管理和自动化基础设施。 它提供快速的处理时间和可扩展性,从处理单个服务器到处理多个服务器。 其好处包括: 1. 以一致的速度快速执行众多目标。 2. 通过标准输入/输出流(-vvv)进行实时调试。 3. 幂等操作允许差异比较和练习运行。 4.利用庞大的Python库生态系统。 5. 在具有 shell 访问权限的任何系统上无代理执行。 6. 与Docker、Terraform等各种连接器无缝集成。 安装涉及通过 pip 添加 Pyinfra 并通过 SSH (my-server.net) 或针对包括 Docker 和本地计算机在内的不同连接运行命令。 通过操作,定义的状态也可以通过编写Python脚本来实现。 例如,在 Ubuntu 中使用 Apt 安装软件包: ````蟒蛇 从 pyinfra.operations 导入 apt apt.packages(name="确保安装了 iftop", packages=["iftop"], update=True, _sudo=True) ```` 随后可以使用“pyinfra @docker/ubuntu deploy.py”调用该脚本。 探索全面的入门指南和操作使用文档,以进一步了解如何实施清单、数据、全局参数和命令行界面以及可用的示例脚本。

本文讨论 PyInfra(一种无代理配置管理工具)和 Ansible(一种用 Python 编写的流行且成熟的无代理工具)之间的比较。 作者建议突出 PyInfra 的独特功能,而不是关注 Ansible 的缺点。 一个显着的区别是 PyInfra 的即时调试功能,允许在执行期间实时输出 stdin/stdout/stderr(-vvv 标志)。 尽管 Ansible 也是声明性的和幂等的,但某些操作打破了声明性,导致混乱。 其中包括 shell 任务、注册任务结果以及根据注册结果插值。 此外,Ansible 不提供直接的方法来声明某些配置的最终状态。 声明性和幂等性是相关的概念,声明性指的是描述所需的状态,而幂等性则确保系统无论先前的条件如何都保持在该状态。 用户喜欢声明式方法,因为它们可以带来更清晰、更易于理解的工作流程,从而减少出错的可能性。 然而,与其他一些工具相比,Ansible 的实现可能需要更详细的理解和调试工作。 PyInfra 旨在通过提供更简化的体验来解决这些问题,特别是对于那些熟悉 Python 的人来说。 它允许用户通过定义操作、提供目标清单并相应地执行 Python 代码来轻松管理和维护其基础设施。 该工具旨在帮助节省时间、减少复杂性并提高管理基础设施的整体效率。
相关文章

原文

pyinfra automates infrastructure using Python. It’s fast and scales from one server to thousands. Great for ad-hoc command execution, service deployment, configuration management and more. Here's why you should try pyinfra:

  • 🚀 Super fast execution over thousands of targets with predictable performance.
  • 🚨 Instant debugging with realtime stdin/stdout/stderr output (-vvv).
  • 🔄 Idempotent operations that enable diffs and dry runs before making changes.
  • 📦 Extendable with the entire Python package ecosystem.
  • 💻 Agentless execution against anything with shell access.
  • 🔌 Integrated with connectors for Docker, Terraform, Vagrant and more.

Install pyinfra with pip:

Now you can execute commands over SSH:

pyinfra my-server.net exec -- echo "hello world"

Or target Docker, the local machine, and other connectors:

pyinfra @docker/ubuntu exec -- echo "Hello world"
pyinfra @local exec -- echo "Hello world"

As well as executing commands you can define state using operations:

pyinfra @docker/ubuntu apt.packages iftop update=true _sudo=true

Which can then be saved as a Python file like deploy.py:

from pyinfra.operations import apt

apt.packages(
    name="Ensure iftop is installed",
    packages=["iftop"],
    update=True,
    _sudo=True,
)

And executed:

pyinfra @docker/ubuntu deploy.py

Now you know the building blocks of pyinfra! By combining inventory, operations and Python code you can deploy anything.

See the more detailed getting started or using operations guides. See how to use inventory & data, global arguments and the CLI or check out the documented examples.

联系我们 contact @ memedata.com