ansible-runner
A tool and python library that helps when interfacing with Ansible directly or as part of another system whether that be through a container image interface, as a standalone tool, or as a Python module that can be imported. The goal is to provide a stable and consistent interface abstraction to Ansible.
Top Related Projects
AWX provides a web-based user interface, REST API, and task engine built on top of Ansible. It is one of the upstream projects for Red Hat Ansible Automation Platform.
Ansible is a radically simple IT automation platform that makes your applications and systems easier to deploy and maintain. Automate everything from code deployment to network configuration to cloud management, in a language that approaches plain English, using SSH, with no agents to install on remote systems. https://docs.ansible.com.
Molecule aids in the development and testing of Ansible content: collections, playbooks and roles
ansible-lint checks playbooks for practices and behavior that could potentially be improved and can fix some of the most common ones for you
Quick Overview
Ansible Runner is a tool and Python library that helps to run Ansible playbooks, roles, and commands in a controlled and portable way. It provides a consistent interface for executing Ansible content, making it easier to integrate Ansible automation into other systems and workflows.
Pros
- Provides a consistent interface for executing Ansible content
- Supports containerized execution environments
- Offers detailed logging and artifact collection
- Easily integrable with other systems and CI/CD pipelines
Cons
- Adds complexity compared to running Ansible directly
- Requires additional setup and configuration
- May have a learning curve for users new to Ansible ecosystem
- Limited documentation for advanced use cases
Code Examples
- Running a playbook using Ansible Runner:
import ansible_runner
r = ansible_runner.run(playbook='playbook.yml', inventory='inventory.ini')
print(f"Status: {r.status}")
print(f"RC: {r.rc}")
- Running an ad-hoc command:
import ansible_runner
r = ansible_runner.run(
module='ping',
host_pattern='all',
inventory='inventory.ini'
)
print(r.stdout.read())
- Using Ansible Runner with an execution environment:
import ansible_runner
r = ansible_runner.run(
playbook='playbook.yml',
inventory='inventory.ini',
container_image='quay.io/ansible/ansible-runner:latest'
)
print(f"Status: {r.status}")
Getting Started
To get started with Ansible Runner, follow these steps:
- Install Ansible Runner:
pip install ansible-runner
- Create a simple playbook (e.g.,
playbook.yml
):
---
- name: Test Playbook
hosts: localhost
tasks:
- name: Echo a message
debug:
msg: "Hello, Ansible Runner!"
- Run the playbook using Ansible Runner:
import ansible_runner
r = ansible_runner.run(playbook='playbook.yml')
print(f"Status: {r.status}")
print(f"Output:\n{r.stdout.read()}")
This will execute the playbook and display the status and output.
Competitor Comparisons
AWX provides a web-based user interface, REST API, and task engine built on top of Ansible. It is one of the upstream projects for Red Hat Ansible Automation Platform.
Pros of AWX
- Provides a web-based UI for managing Ansible playbooks and inventories
- Offers role-based access control and job scheduling capabilities
- Integrates with various cloud providers and version control systems
Cons of AWX
- More complex setup and maintenance compared to Ansible Runner
- Requires additional resources to run (database, web server, etc.)
- Steeper learning curve for users new to Ansible
Code Comparison
AWX (Python):
from awx.main.models import Job
job = Job.objects.create(
name="My Job",
project=project,
playbook="playbook.yml",
inventory=inventory
)
job.launch()
Ansible Runner (Python):
import ansible_runner
r = ansible_runner.run(
private_data_dir='/tmp/demo',
playbook='playbook.yml'
)
print(r.status)
Summary
AWX is a comprehensive web-based solution for managing Ansible at scale, offering advanced features like scheduling and access control. Ansible Runner, on the other hand, is a lightweight Python library for directly executing Ansible playbooks and commands. AWX is better suited for large teams and complex environments, while Ansible Runner is ideal for simpler setups or integration into existing Python applications.
Ansible is a radically simple IT automation platform that makes your applications and systems easier to deploy and maintain. Automate everything from code deployment to network configuration to cloud management, in a language that approaches plain English, using SSH, with no agents to install on remote systems. https://docs.ansible.com.
Pros of Ansible
- More comprehensive and feature-rich, offering a complete automation platform
- Larger community and ecosystem, with extensive documentation and support
- Provides a wide range of built-in modules for various tasks and integrations
Cons of Ansible
- Steeper learning curve due to its extensive features and capabilities
- Heavier resource usage, especially for large-scale deployments
- More complex setup and configuration process
Code Comparison
Ansible:
- name: Install Apache
hosts: webservers
tasks:
- name: Install Apache package
yum:
name: httpd
state: present
Ansible Runner:
import ansible_runner
r = ansible_runner.run(private_data_dir='/tmp/demo', playbook='install_apache.yml')
print(r.status)
Key Differences
- Ansible is a full-featured automation platform, while Ansible Runner is a tool for programmatically invoking Ansible
- Ansible Runner provides a more streamlined interface for embedding Ansible in other applications or workflows
- Ansible offers a declarative approach using YAML playbooks, whereas Ansible Runner allows for programmatic execution of Ansible tasks
Use Cases
- Ansible: Ideal for complex infrastructure management, configuration management, and application deployment
- Ansible Runner: Better suited for integrating Ansible functionality into custom applications, CI/CD pipelines, or other automation tools
Molecule aids in the development and testing of Ansible content: collections, playbooks and roles
Pros of Molecule
- Focused on testing Ansible roles and playbooks
- Supports multiple test scenarios and platforms
- Integrates well with CI/CD pipelines
Cons of Molecule
- Steeper learning curve for beginners
- More complex setup compared to Ansible Runner
- Limited to testing Ansible content
Code Comparison
Molecule configuration example:
---
dependency:
name: galaxy
driver:
name: docker
platforms:
- name: instance
image: docker.io/pycontribs/centos:7
pre_build_image: true
Ansible Runner execution example:
import ansible_runner
r = ansible_runner.run(private_data_dir='/tmp/demo', playbook='test.yml')
print("{}: {}".format(r.status, r.rc))
for each_host_event in r.events:
print(each_host_event['event'])
Summary
Molecule is specifically designed for testing Ansible roles and playbooks, offering multiple test scenarios and platform support. It integrates well with CI/CD pipelines but has a steeper learning curve and more complex setup.
Ansible Runner, on the other hand, is a more general-purpose tool for running Ansible from Python code or the command line. It's simpler to set up and use but lacks the specialized testing features of Molecule.
Choose Molecule for comprehensive Ansible content testing, especially in CI/CD environments. Opt for Ansible Runner when you need to integrate Ansible execution into Python applications or scripts.
ansible-lint checks playbooks for practices and behavior that could potentially be improved and can fix some of the most common ones for you
Pros of Ansible-lint
- Focuses on linting and best practices, improving code quality and consistency
- Provides detailed feedback on potential issues and style violations
- Can be integrated into CI/CD pipelines for automated checks
Cons of Ansible-lint
- Limited to static analysis and doesn't execute playbooks
- May produce false positives or miss context-specific issues
- Requires additional setup and maintenance compared to direct execution
Code Comparison
Ansible-lint example:
- name: Install packages
apt:
name: "{{ item }}"
state: present
loop:
- nginx
- postgresql
Ansible-runner example:
import ansible_runner
r = ansible_runner.run(private_data_dir='/tmp/demo', playbook='test.yml')
print("{}: {}".format(r.status, r.rc))
for each_host_event in r.events:
print(each_host_event['event'])
Ansible-lint focuses on analyzing YAML files for potential issues, while Ansible-runner provides a programmatic interface for executing Ansible playbooks and managing their output. Ansible-lint is more suited for code quality checks, whereas Ansible-runner is designed for integrating Ansible execution into other applications or workflows.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Ansible Runner
Ansible Runner is a tool and Python library that helps when interfacing with Ansible directly or as part of another system. Ansible Runner works as a standalone tool, a container image interface, or a Python module that can be imported. The goal is to provide a stable and consistent interface abstraction to Ansible.
See the latest documentation for usage details.
Get Involved
- GitHub issues to track bug report and feature ideas
- GitHub Milestones to track what's for the next release
- Want to contribute? Please check out our contributing guide
- Visit the Community section of the docs.
- See the Ansible communication guide for complete information about getting in touch.
Top Related Projects
AWX provides a web-based user interface, REST API, and task engine built on top of Ansible. It is one of the upstream projects for Red Hat Ansible Automation Platform.
Ansible is a radically simple IT automation platform that makes your applications and systems easier to deploy and maintain. Automate everything from code deployment to network configuration to cloud management, in a language that approaches plain English, using SSH, with no agents to install on remote systems. https://docs.ansible.com.
Molecule aids in the development and testing of Ansible content: collections, playbooks and roles
ansible-lint checks playbooks for practices and behavior that could potentially be improved and can fix some of the most common ones for you
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot