Python Ansible Runner

Posted on  by 

Minimal code to run an Ansible Playbook from within python and get stats back on success or fail - minimalansibleplaybook.py. Apr 06, 2015 # Get pip sudo apt-get install -y python-pip # Get virtualenv and create one sudo pip install virtualenv cd /path/to/runner/script virtualenv./.env source./env/bin/activate # Install Ansible into the virtualenv pip install ansible. Then, with the environment active, we call upon Ansible from our Python scripts.

That is it your hello world module finished. Pass variables in and out of a Python Module. I was working on an upgrade script and I needed to bump the Semantic Versioning i.e. Bumping the minor version of 1.1.1, Ansible handles this poorly as it is not a float or a int.

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.

  1. Using Runner as a Python Module Interface to Ansible. Ansible Runner is intended to provide a directly importable and usable API for interfacing with Ansible itself and exposes a few helper interfaces. The modules center around the Runner object. The helper methods will either return an instance of this object which provides an interface to.
  2. Hello, I would like to use ansiblerunner instead of the python api to interface with ansible for a very specific task. But I want to script to be self contained so i hardcode the inventory and the playbook directly on the script. The code below is a simplified version. Usually the hosts are passed as arguments.

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

Ansible 2.5 and above work with Python 3. Previous to 2.5, using Python 3 wasconsidered a tech preview. This topic discusses how to set up your controller and managed machinesto use Python 3.

Note

On the controller we support Python 3.5 or greater and Python 2.7 or greater. Module-side, we support Python 3.5 or greater and Python 2.6 or greater.

On the controller side

Python Ansible Runner

Run Ansible Playbook From Python

The easiest way to run /usr/bin/ansible under Python 3 is to install it with the Python3version of pip. This will make the default /usr/bin/ansible run with Python3:

If you are running Ansible Running the devel branch from a clone and want to use Python 3 with your source checkout, run yourcommand via python3. For example:

Note

Runner

Individual Linux distribution packages may be packaged for Python2 or Python3. When running fromdistro packages you’ll only be able to use Ansible with the Python version for which it wasinstalled. Sometimes distros will provide a means of installing for several Python versions(via a separate package or via some commands that are run after install). You’ll need to checkwith your distro to see if that applies in your case.

Ansible

Using Python 3 on the managed machines with commands and playbooks

  • Ansible will automatically detect and use Python 3 on many platforms that ship with it. To explicitly configure aPython 3 interpreter, set the ansible_python_interpreter inventory variable at a group or host level to thelocation of a Python 3 interpreter, such as /usr/bin/python3. The default interpreter path may also beset in ansible.cfg.

See also

Interpreter Discovery for more information.

See also

How to build your inventory for more information.

  • Run your command or playbook:

Note that you can also use the -e command line option to manuallyset the python interpreter when you run a command. This can be useful if you want to test whethera specific module or playbook has any bugs under Python 3. For example:

What to do if an incompatibility is found

We have spent several releases squashing bugs and adding new tests so that Ansible’s core featureset runs under both Python 2 and Python 3. However, bugs may still exist in edge cases and many ofthe modules shipped with Ansible are maintained by the community and not all of those may be portedyet.

Python Ansible Runners

If you find a bug running under Python 3 you can submit a bug report on Ansible’s GitHub project. Be sure to mention Python3 in the bug report sothat the right people look at it.

Ansible Python Api Example

If you would like to fix the code and submit a pull request on github, you canrefer to Ansible and Python 3 for information on how we fixcommon Python3 compatibility issues in the Ansible codebase.

Coments are closed