Python API pre 2.0 ¶. It’s pretty simple: import ansible.runner runner = ansible.runner.Runner ( modulename='ping', moduleargs=', pattern='web.', forks=10 ) datastructure = runner.run The run method returns results per host, grouped by whether they could be contacted or not. Return types are module specific, as expressed in the About. Software Testing (LIVE Course). Example 2: Create a directory with the mode as 775 and owner/group as Ansible. An Ansible controller host – This tutorial will be using Ansible v2.9.18 on an Ubuntu 18.04.5 LTS machine with an IP address of 10.111.4.53. Note that Windows is not supported as a control node, only a managed node. Python installed on your Ansible controller host – This tutorial will be using Python v2, but v3 should work just as well. Ansible celery does not workcelery pythonoptimizecelery ansible optimization.
Released:
Pytest fixture which runs given ansible playbook file.
Project description
This repository contains pytest
_ plugin
_ which provides an easy wayto run particular ansible playbooks
_ during setup phase of a test case.This is useful whenyou already have some playbook files you would like to reuse during test setupor plan to maintain test setup in ansible playbooks for you to be able touse it both during test run setup and directly via ansible for other purposes(automatically during deployment or manually when needed).
Compared with pytest-ansible
_ module, this module doesn't allow you toinspect ansible facts
_ or details about results of each ansible task, nordoest it allow to specify and execute an ansible task directly. So if you needany of that, go for pytest-ansible
_ instead. This plugin provides the onlymissing ansible feature which pytest-ansible
_ is not supposed to provide - torun ansible playbook file directly.
Initial structure of this repository was generated with Cookiecutter
_along with @hackebrot
's Cookiecutter-pytest-plugin
template.
Features
Notes
The plugin provides
ansible_playbook
pytest fixture
_, which allowsone to run one or more ansible playbooks during test setup or tear down of atest case.It also provides
context manager
_pytest_ansible_playbook.runner()
which can be used to build custom fixtures with anyscope
_ or to executesetup and/or teardown playbooks in a code of a test case.It's compatible with python3 (playbooks are executed viarunning
ansible-playbook
in subprocess instead of using apiof ansible python module).Doesn't allow you to configure ansible in any way, all changes of ansiblesetup needs to be done in ansible playbooks, variable or config files.This encourages you to maintain a clear separation of ansible playbooksand the tests.
Ansible Runner Python Example Pdf
Key features
An option to run arbitrary playbooks in the middle of the test:
An option to add teardown playbooks in the middle of the test:
Return values have been added to every playbook run. The return value respects playbook execution order and for every host:
A test can pass arguments to the playbooks it runs. Thus the playbook has changed from string to dictionary:
Mark setup / teardown syntax:
Now the pytest plugin uses a separate module: playbook_runner
.https://github.com/final-israel/playbook_runnerThis is because other tools may want to also run playbooks not necessarily as a part of the pytest framework.
Requirements
Ansible should be installed (so that ansible-playbook
binary isavailable in PATH). Use version provided by packaging system of your operationsystem.
Installation
You can either install stable release from PyPI
or use latest development version from master
branch.
- Installing stable release - You can install
pytest-ansible-playbook-runner
viapip
fromPyPI
:
- Installing latest development version
The suggested way to install from sources of current master branch is via python virtual enviroment
:
Note that you can use virtualenvwrapper
to simplify this workflow.
Usage
When the plugin is installed, you can use the following command-lineparameters::
Where <path_to_directory_with_playbooks>
is a directory which contains ansible playbooks and any other ansible files such as configuration or roles if needed. A ansible-playbook
process will be ableto access the files stored there, since this directory is set as cwd
(current working directory) of the ansible
process.
The <path_to_inventory_file>
is file with ansible inventory
. You can use either an absolute path or a relative path within the ansible directory specified via the 1st option. Note that the option names were chosen this way so that it doesn't conflict with pytest-ansible
plugin.
Example of simple custom fixture:
And here is an example of using the fixture inside a test case directly:
Contributing
Contributions are very welcome. Tests can be run with tox
, please ensure the coverage at least stays the same before you submit a pull request.
License
Distributed under the terms of the Apache License 2.0
license, pytest-ansible-playbook-runner
is free and open source software
Issues
If you encounter any problems, please file an issue along with a detailed description.
.. file an issue
: https://github.com/final-israel/pytest-ansible-playbook-runner/issues.. Cookiecutter
: https://github.com/audreyr/cookiecutter.. @hackebrot
: https://github.com/hackebrot.. cookiecutter-pytest-plugin
: https://github.com/pytest-dev/cookiecutter-pytest-plugin.. pytest
: http://docs.pytest.org/en/latest/.. pytest fixture
: http://doc.pytest.org/en/latest/fixture.html.. pytest markers
: http://doc.pytest.org/en/latest/example/markers.html.. plugin
: http://doc.pytest.org/en/latest/plugins.html.. tox
: https://tox.readthedocs.io/en/latest/.. pip
: https://pypi.python.org/pypi/pip/.. PyPI
: https://pypi.python.org/pypi.. stable release from PyPI
: https://pypi.org/project/pytest-ansible-playbook-runner/.. python virtual enviroment
: https://virtualenv.pypa.io/en/stable/.. virtualenvwrapper
: https://virtualenvwrapper.readthedocs.io/en/latest/.. pytest-ansible
: https://pypi.python.org/pypi/pytest-ansible.. ansible playbooks
: https://docs.ansible.com/ansible/playbooks.html.. ansible facts
: https://docs.ansible.com/ansible/playbooks_variables.html#information-discovered-from-systems-facts.. ansible inventory
: https://docs.ansible.com/ansible/intro_inventory.html.. Apache License 2.0
: http://www.apache.org/licenses/LICENSE-2.0.. context manager
: https://docs.python.org/3.6/library/stdtypes.html#context-manager-types.. scope
: https://docs.pytest.org/en/latest/fixture.html#scope-sharing-a-fixture-instance-across-tests-in-a-class-module-or-session.. pytest doesn't expect fixtures to have markers
: https://github.com/pytest-dev/pytest/issues/3664
Release historyRelease notifications | RSS feed
0.5.4
0.5.3
0.5.1
0.0.5
0.0.4
0.0.3
0.0.2
0.0.1
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Filename, size | File type | Python version | Upload date | Hashes |
---|---|---|---|---|
Filename, size pytest_ansible_playbook_runner-0.5.4-py3-none-any.whl (12.4 kB) | File type Wheel | Python version py3 | Upload date | Hashes |
Filename, size pytest-ansible-playbook-runner-0.5.4.tar.gz (12.7 kB) | File type Source | Python version None | Upload date | Hashes |
Hashes for pytest_ansible_playbook_runner-0.5.4-py3-none-any.whl
Algorithm | Hash digest |
---|---|
SHA256 | 81a187797b58077147f415eacb0f8864e0910000b389a5736b2c79856f8a5c1a |
MD5 | dfcdf248f09bd51c2dfcdd5eb80c050b |
BLAKE2-256 | 03f2316a6c602636bfb8f95cf8fa9ea0a41022c4b3ba33da9d10bc1678ca0749 |
Hashes for pytest-ansible-playbook-runner-0.5.4.tar.gz
Algorithm | Hash digest |
---|---|
SHA256 | c31c0447a9f467bc080a8d951b62ae286ddd6ef4eeade15748f81c41267659ef |
MD5 | 0cdf5a503daf10d91bee66c928df6bbc |
BLAKE2-256 | 5c0df6971a01ad83cd554a1d2a31c172d5a4c733e60c5f7f99ad18ecb644b721 |
Topics
- Python API
- Python API pre 2.0
Please note that while we make this API available it is not intended for direct consumption, it is herefor the support of the Ansible command line tools. We try not to make breaking changes but we reserve theright to do so at any time if it makes sense for the Ansible toolset.
Ansible Runner Python Example Interview
The following documentation is provided for those that still want to use the API directly, but be mindful this is not something the Ansible team supports.
There are several interesting ways to use Ansible from an API perspective. You can usethe Ansible python API to control nodes, you can extend Ansible to respond to various python events, you canwrite various plugins, and you can plug in inventory data from external data sources. This documentcovers the execution and Playbook API at a basic level.
Python Example Programs
If you are looking to use Ansible programmatically from something other than Python, trigger events asynchronously,or have access control and logging demands, take a look at Ansible Toweras it has a very nice REST API that provides all of these things at a higher level.
Ansible is written in its own API so you have a considerable amount of power across the board.This chapter discusses the Python API.
The Python API is very powerful, and is how the all the ansible CLI tools are implemented.In version 2.0 the core ansible got rewritten and the API was mostly rewritten.
注解
Ansible relies on forking processes, as such the API is not thread safe.
In 2.0 things get a bit more complicated to start, but you end up with much more discrete and readable classes:
It’s pretty simple:
The run method returns results per host, grouped by whether theycould be contacted or not. Return types are module specific, asexpressed in the About Modules documentation.:
A module can return any type of JSON data it wants, so Ansible canbe used as a framework to rapidly build powerful applications and scripts.
The following script prints out the uptime information for all hosts:
Advanced programmers may also wish to read the source to ansible itself,for it uses the API (with all available options) to implement the ansible
command line tools (lib/ansible/cli/
).
参见
- Developing Dynamic Inventory Sources
- Developing dynamic inventory integrations
- Developing Modules
- How to develop modules
- Developing Plugins
- How to develop plugins
- Development Mailing List
- Mailing list for development topics
- irc.freenode.net
- #ansible IRC chat channel