Running Ansible Tasks from Python Without YAML
Автор: vlogize
Загружено: 2025-03-21
Просмотров: 29
Discover how to run Ansible tasks directly from Python without YAML configuration. Simplify your file modification tasks using `ansible_runner`.
---
This video is based on the question https://stackoverflow.com/q/76497159/ asked by the user 'Wolkenarchitekt' ( https://stackoverflow.com/u/319905/ ) and on the answer https://stackoverflow.com/a/76497402/ provided by the user 'Freeman' ( https://stackoverflow.com/u/252518/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: How to run an Ansible task from Python without any YAML
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Running Ansible Tasks from Python Without YAML
Ansible is a powerful automation tool that simplifies configuration management, but it often requires YAML files for task definitions. What if you want to run Ansible tasks directly from Python without having to deal with YAML configuration files? That's exactly the problem we're tackling today.
In this post, we’ll explore how to utilize Ansible modules in Python for simple file modification tasks, enabling you to perform operations like adding lines to a file without the overhead of configuration. Whether you're a developer looking to streamline your tasks or someone who loves automation, read on to learn how to leverage Ansible directly in your Python scripts.
Understanding the Need
You might find yourself needing to modify files frequently, perhaps to add a line of text, but don’t want the hassle of setting up a full Ansible configuration. Using Ansible modules directly in Python can make these tasks less cumbersome and more efficient.
The Solution: Using ansible_runner
To run Ansible tasks from Python without using YAML files, we can utilize the ansible_runner library. This lightweight library enables you to execute Ansible tasks directly, using just a few lines of Python code.
Step 1: Installing ansible_runner
Before diving into the code, make sure you have the ansible_runner library installed. You can easily install it via pip:
[[See Video to Reveal this Text or Code Snippet]]
Ensure that the target file (e.g., myfile) exists in your current working directory before you run the code.
Step 2: Writing the Python Code
Let's break down the implementation based on older and newer versions of ansible_runner.
Using Older Versions
If you're working with older versions of ansible_runner, here’s the basic structure of your Python script:
[[See Video to Reveal this Text or Code Snippet]]
Explanation of Key Arguments:
private_data_dir: Specifies where Ansible should look for its configuration files; here it points to the current directory.
host_pattern: Indicates that the task is to be run on the localhost.
module_name: The Ansible module you want to execute ("lineinfile" in this case).
Using Latest Version (2.3.3 and above)
If you are using a newer version, the function structure has slightly changed. Here is how you would write your code:
[[See Video to Reveal this Text or Code Snippet]]
Updates in Key Arguments:
module: The name of the modules to run (this replaces the earlier module_name).
module_args_str: The arguments for the module, specified as a string (replacing module_args).
Conclusion
By employing the ansible_runner library, you can efficiently execute Ansible modules directly from Python without relying on YAML configurations. Whether you choose to utilize older versions or the latest updates, this approach allows for seamless automation of simple tasks. Now you can focus on automating your workflow without getting bogged down in configuration details.
Feel free to experiment with your own tasks, and enjoy the efficiency that comes with leveraging Ansible in your Python scripts!

Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: