Ansible Runner Python Example

  

Since Ansible is written in Python, it becomes the default choice for most users, just like other Python-based configuration management systems, such as Fabric and SaltStack. The name Jinja originated from the Japanese word for temple, which is similar in phonetics to the word template. If you do not have Ansible Runner already installed, you can refer to its documentation for guidance. Below is an example playbook (we’ll call it test.yml) that can be run via Ansible Runner to ensure that the Execution Environment is working:-hosts: localhost connection: local tasks: - name: Ensure the myapp Namespace exists.

2014-11-03 17:59:38 UTC
import ansible.runner

Ansible Playbook Run Python Script


import ansible.playbook
from ansible import callbacks
from ansible import utils
stats = callbacks.AggregateStats()
playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
runner_cb = callbacks.PlaybookRunnerCallbacks(stats,
verbose=utils.VERBOSITY)
pb = ansible.playbook.PlayBook(
playbook='myplaybook.yml',
stats=stats,

Ansible Runner Python Example Program

callbacks=playbook_cb,

Ansible Runner Python Example Program

runner_callbacks=runner_cb,
check=True # Change to false to run it.
)Ansible Runner Python Example
for (play_ds, play_basedir) in zip(pb.playbook, pb.play_basedirs):

Python Ansible Playbook

import ipdb

Ansible Runner Python Examples

ipdb.set_trace()
I'm new to Ansible... I would like to create a library of playbooks and
then use ansible's python api to run the playbooks and get the result from
a larger python script.
I see there is an example <http://ansible.cc/docs/api.html> of how to use
the ansible.runner.Runner() from python, but that seems to be useful for
running modules. I would like to run a playbook from a yml file
(specifying the host, and private_key parameters directly from python). I
found the playbooks.Playbook() python class, but the constructor requires
several parameters which I am not sure how to construct... callbacks...
runner callbacks... etc
Any pointers or any examples of how to use the python API?
Cheers,
Kyle
--
You received this message because you are subscribed to the Google Groups 'Ansible Project' group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/1bd90e0a-9e6d-4ab2-8308-95ef0ff1aaae%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ansible Runner is a tool and python library that helps when interfacing with Ansible directly or as part of another systemwhether that be through a container image interface, as a standalone tool, or as a Python module that can be imported. The goalis to provide a stable and consistent interface abstraction to Ansible. This allows Ansible to be embedded into other systems that don’twant to manage the complexities of the interface on their own (such as CI/CD platforms, Jenkins, or other automated tooling).

Ansible Runner represents the modularization of the part of Ansible Tower/AWX that is responsiblefor running ansible and ansible-playbook tasks and gathers the output from it. It does this by presenting a common interface that doesn’tchange, even as Ansible itself grows and evolves.

Part of what makes this tooling useful is that it can gather its inputs in a flexible way (See Introduction to Ansible Runner:). It also has a system for storing theoutput (stdout) and artifacts (host-level event data, fact data, etc) of the playbook run.

There are 3 primary ways of interacting with Runner

  • A standalone command line tool (ansible-runner) that can be started in the foreground or run in the background asynchronously
  • A reference container image that can be used as a base for your own images and will work as a standalone container or running inOpenshift or Kubernetes
  • A python module - library interface

Ansible Runner can also be configured to send status and event data to other systems using a plugin interface, see Sending Runner Status and Events to External Systems.

Examples of this could include:

  • Sending status to Ansible Tower/AWX
  • Sending events to an external logging service

Contents:

  • Introduction to Ansible Runner
  • Installing Ansible Runner
  • Sending Runner Status and Events to External Systems
  • Using Runner as a standalone command line tool
  • Using Runner as a Python Module Interface to Ansible
  • Using Runner with Execution Environmnets
  • Using Runner as a container interface to Ansible
  • Remote job execution