• Stars
    star
    149
  • Rank 248,619 (Top 5 %)
  • Language
    Python
  • License
    MIT License
  • Created over 10 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A pytest plugin that enables the use of ansible in tests, enables the use of pytest as a collection unit test runner, and exposes molecule scnearios through a pytest fixture.

pytest-ansible

Build Status Version License Supported Python Versions

This repository contains a plugin for pytest which adds several fixtures for running ansible modules, or inspecting ansible_facts. While one can simply call out to ansible using the subprocess module, having to parse stdout to determine the outcome of the operation is unpleasant and prone to error. With pytest-ansible, modules return JSON data which you can inspect and act on, much like with an ansible playbook.

Installation

Install this plugin using pip

pip install pytest-ansible

Usage

Once installed, the following pytest command-line parameters are available:

pytest \
    [--inventory <path_to_inventory>] \
    [--extra-inventory <path_to_extra_inventory>] \
    [--host-pattern <host-pattern>] \
    [--connection <plugin>] \
    [--module-path <path_to_modules] \
    [--user <username>] \
    [--become] \
    [--become-user <username>] \
    [--become-method <method>] \
    [--limit <limit>] \
    [--check]

Inventory

Using ansible first starts with defining your inventory. This can be done several ways, but to start, we'll use the ansible_adhoc fixture.

def test_my_inventory(ansible_adhoc):
    hosts = ansible_adhoc()

In the example above, the hosts variable is an instance of the HostManager class and describes your ansible inventory. For this to work, you'll need to tell ansible where to find your inventory. Inventory can be anything supported by ansible, which includes an INI file or an executable script that returns properly formatted JSON. For example,

pytest --inventory my_inventory.ini --host-pattern all

or

pytest --inventory path/to/my/script.py --host-pattern webservers

or

pytest --inventory one.example.com,two.example.com --host-pattern all

In the above examples, the inventory provided at runtime will be used in all tests that use the ansible_adhoc fixture. A more realistic scenario may involve using different inventory files (or host patterns) with different tests. To accomplish this, the fixture ansible_adhoc allows you to customize the inventory parameters. Read on for more detail on using the ansible_adhoc fixture.

Extra Inventory

Using ansible first starts with defining your extra inventory. This feature was added in version 2.3.0, and is intended to allow the user to work with two different inventories. This can be done in several ways, but to start, we'll use the ansible_adhoc fixture.

For example,

pytest --inventory my_inventory.ini --extra-inventory my_second_inventory.ini --host-pattern host_in_second_inventory

Fixture ansible_adhoc

The ansible_adhoc fixture returns a function used to initialize a HostManager object. The ansible_adhoc fixture will default to parameters supplied to the pytest command-line, but also allows one to provide keyword arguments used to initialize the inventory.

The example below demonstrates basic usage with options supplied at run-time to pytest.

def test_all_the_pings(ansible_adhoc):
    ansible_adhoc().all.ping()

The following example demonstrates available keyword arguments when creating a HostManager object.

def test_uptime(ansible_adhoc):
    # take down the database
    ansible_adhoc(inventory='db1.example.com,', user='ec2-user',
        become=True, become_user='root').all.command('reboot')

The HostManager object returned by the ansible_adhoc() function provides numerous ways of calling ansible modules against some, or all, of the inventory. The following demonstates sample usage.

def test_host_manager(ansible_adhoc):
    hosts = ansible_adhoc()

    # __getitem__
    hosts['all'].ping()
    hosts['localhost'].ping()

    # __getattr__
    hosts.all.ping()
    hosts.localhost.ping()

    # Supports [ansible host patterns](http://docs.ansible.com/ansible/latest/intro_patterns.html)
    hosts['webservers:!phoenix'].ping()  # all webservers that are not in phoenix
    hosts[0].ping()
    hosts[0:2].ping()

    assert 'one.example.com' in hosts

    assert hasattr(hosts, 'two.example.com')

    for a_host in hosts:
        a_host.ping()

Fixture localhost

The localhost fixture is a convenience fixture that surfaces a ModuleDispatcher instance for ansible host running pytest. This is convenient when using ansible modules that typically run on the local machine, such as cloud modules (ec2, gce etc...).

def test_do_something_cloudy(localhost, ansible_adhoc):
    """Deploy an ec2 instance using multiple fixtures."""
    params = dict(
        key_name='somekey',
        instance_type='t2.micro',
        image='ami-123456',
        wait=True,
        group='webserver',
        count=1,
        vpc_subnet_id='subnet-29e63245',
        assign_public_ip=True,
    )

    # Deploy an ec2 instance from localhost using the `ansible_adhoc` fixture
    ansible_adhoc(inventory='localhost,', connection='local').localhost.ec2(**params)

    # Deploy an ec2 instance from localhost using the `localhost` fixture
    localhost.ec2(**params)

Fixture ansible_module

The ansible_module fixture allows tests and fixtures to call ansible modules. Unlike the ansible_adhoc fixture, this fixture only uses the options supplied to pytest at run time.

A very basic example demonstrating the ansible ping module:

def test_ping(ansible_module):
    ansible_module.ping()

A more involved example of updating the sshd configuration, and restarting the service.

def test_sshd_config(ansible_module):

    # update sshd MaxSessions
    contacted = ansible_module.lineinfile(
        dest="/etc/ssh/sshd_config",
        regexp="^#?MaxSessions .*",
        line="MaxSessions 150")
    )

    # assert desired outcome
    for (host, result) in contacted.items():
        assert 'failed' not in result, result['msg']
        assert 'changed' in result

    # restart sshd
    contacted = ansible_module.service(
        name="sshd",
        state="restarted"
    )

    # assert successful restart
    for (host, result) in contacted.items():
        assert 'changed' in result and result['changed']
        assert result['name'] == 'sshd'

    # do other stuff ...

Fixture ansible_facts

The ansible_facts fixture returns a JSON structure representing the system facts for the associated inventory. Sample fact data is available in the ansible documentation.

Note, this fixture is provided for convenience and could easily be called using ansible_module.setup().

A systems facts can be useful when deciding whether to skip a test ...

def test_something_with_amazon_ec2(ansible_facts):
    for facts in ansible_facts:
        if 'ec2.internal' != facts['ansible_domain']:
            pytest.skip("This test only applies to ec2 instances")

Additionally, since facts are just ansible modules, you could inspect the contents of the ec2_facts module for greater granularity ...

def test_terminate_us_east_1_instances(ansible_adhoc):

    for facts in ansible_adhoc().all.ec2_facts():
        if facts['ansible_ec2_placement_region'].startswith('us-east'):
            '''do some testing'''

Parameterizing with pytest.mark.ansible

Perhaps the --ansible-inventory=<inventory> includes many systems, but you only wish to interact with a subset. The pytest.mark.ansible marker can be used to modify the pytest-ansible command-line parameters for a single test. Please note, the fixture ansible_adhoc is the prefer mechanism for interacting with ansible inventory within tests.

For example, to interact with the local system, you would adjust the host_pattern and connection parameters.

@pytest.mark.ansible(host_pattern='local,', connection='local')
def test_copy_local(ansible_module):

    # create a file with random data
    contacted = ansible_module.copy(
        dest='/etc/motd',
        content='PyTest is amazing!',
        owner='root',
        group='root',
        mode='0644',
    )

    # assert only a single host was contacted
    assert len(contacted) == 1, \
        "Unexpected number of hosts contacted (%d != %d)" % \
        (1, len(contacted))

    assert 'local' in contacted

    # assert the copy module reported changes
    assert 'changed' in contacted['local']
    assert contacted['local']['changed']

Note, the parameters provided by pytest.mark.ansible will apply to all class methods.

@pytest.mark.ansible(host_pattern='local,', connection='local')
class Test_Local(object):
    def test_install(self, ansible_module):
        '''do some testing'''
    def test_template(self, ansible_module):
        '''do some testing'''
    def test_service(self, ansible_module):
        '''do some testing'''

Inspecting results

When using the ansible_adhoc, localhost or ansible_module fixtures, the object returned will be an instance of class AdHocResult. The AdHocResult class can be inspected as follows:

def test_adhoc_result(ansible_adhoc):
    contacted = ansible_adhoc(inventory=my_inventory).command("date")

    # As a dictionary
    for (host, result) in contacted.items():
        assert result.is_successful, "Failed on host %s" % host
    for result in contacted.values():
        assert result.is_successful
    for host in contacted.keys():
        assert host in ['localhost', 'one.example.com']

    assert contacted.localhost.is_successful

    # As a list
    assert len(contacted) > 0
    assert 'localhost' in contacted

    # As an iterator
    for result in contacted:
        assert result.is_successful

    # With __getattr__
    assert contacted.localhost.is_successful

    # Or __gettem__
    assert contacted['localhost'].is_successful

Using the AdHocResult object provides ways to conveniently access results for different hosts involved in the ansible adhoc command. Once the specific host result is found, you may inspect the result of the ansible adhoc command on that use by way of the ModuleResult interface. The ModuleResult class represents the dictionary returned by the ansible module for a particular host. The contents of the dictionary depend on the module called.

The ModuleResult interface provides some convenient proprerties to determine the success of the module call. Examples are included below.

def test_module_result(localhost):
    contacted = localhost.command("find /tmp")

    assert contacted.localhost.is_successful
    assert contacted.localhost.is_ok
    assert contacted.localhost.is_changed
    assert not contacted.localhost.is_failed

    contacted = localhost.shell("exit 1")
    assert contacted.localhost.is_failed
    assert not contacted.localhost.is_successful

The contents of the JSON returned by an ansible module differs from module to module. For guidance, consult the documentation and examples for the specific ansible module.

Exception handling

If ansible is unable to connect to any inventory, an exception will be raised.

@pytest.mark.ansible(inventory='unreachable.example.com,')
def test_shutdown(ansible_module):

    # attempt to ping a host that is down (or doesn't exist)
    pytest.raises(pytest_ansible.AnsibleHostUnreachable):
        ansible_module.ping()

Sometimes, only a single host is unreachable, and others will have properly returned data. The following demonstrates how to catch the exception, and inspect the results.

@pytest.mark.ansible(inventory='good:bad')
def test_inventory_unreachable(ansible_module):
    exc_info = pytest.raises(pytest_ansible.AnsibleHostUnreachable, ansible_module.ping)
    (contacted, dark) = exc_info.value.results

    # inspect the JSON result...
    for (host, result) in contacted.items():
        assert result['ping'] == 'pong'

    for (host, result) in dark.items():
        assert result['failed'] == True

More Repositories

1

ansible

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.
Python
58,550
star
2

awx

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.
Python
13,864
star
3

ansible-examples

A few starter examples of ansible playbooks, to show features and how they work together. See http://galaxy.ansible.com for example roles from the Ansible community for deploying many popular applications.
Shell
11,590
star
4

molecule

Molecule aids in the development and testing of Ansible content: collections, playbooks and roles
Python
3,849
star
5

ansible-lint

ansible-lint checks playbooks for practices and behavior that could potentially be improved and can fix some of the most common ones for you
Python
3,436
star
6

ansible-container

DEPRECATED -- Ansible Container was a tool to build Docker images and orchestrate containers using only Ansible playbooks.
Python
2,191
star
7

workshops

Training Course for Ansible Automation Platform
Jinja
1,708
star
8

ansible-modules-core

Ansible modules - these modules ship with ansible
Python
1,279
star
9

awx-operator

An Ansible AWX operator for Kubernetes built with Operator SDK and Ansible. πŸ€–
Jinja
1,234
star
10

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.
Python
949
star
11

ansible-modules-extras

Ansible extra modules - these modules ship with ansible
Python
942
star
12

galaxy

Legacy Galaxy still available as read-only on https://old-galaxy.ansible.com - looking for the new galaxy -> https://github.com/ansible/galaxy_ng
Python
850
star
13

ansible-jupyter-kernel

Jupyter Notebook Kernel for running Ansible Tasks and Playbooks
Python
518
star
14

community

This repository is being archived. See https://github.com/ansible-community/presentations and https://github.com/ansible-community/meetings for the new locations
HTML
487
star
15

lightbulb

Lightbulb has been deprecated and replaced by Ansible Workshops
HTML
480
star
16

ansible-lockdown

Archived, new content in https://github.com/ansible-lockdown
454
star
17

ansible-docker-base

Ansible base Images for easy Ansible-Playbook-based Docker builds
406
star
18

ansible-navigator

A text-based user interface (TUI) for Ansible.
Python
369
star
19

tower-cli

THIS TOOL IS NO LONGER UNDER ACTIVE DEVELOPMENT. This tool is being phased out in favor of the new official AWX CLI
Python
364
star
20

vscode-ansible

vscode/vscodium extension for providing Ansible auto-completion and integrating quality assurance tools like ansible-lint, ansible syntax check, yamllint, molecule and ansible-test.
TypeScript
352
star
21

test-playbooks

playbook-tests
Python
346
star
22

event-driven-ansible

Ansible Collection for EDA
Python
274
star
23

ansible-builder

An Ansible execution environment builder
Python
264
star
24

ansible-lint-action

❗️Replaced by https://github.com/marketplace/actions/run-ansible-lint
254
star
25

ansible-language-server

🚧 Ansible Language Server codebase is now included in vscode-ansible repository
TypeScript
248
star
26

galaxy_ng

Ansible Galaxy Server - Issues on https://forum.ansible.com Docs on https://galaxy-ng.readthedocs.io/
Python
213
star
27

ansibullbot

Bot for management of Ansible issues and PRs on GitHub.
Python
203
star
28

ansible-runner-service

Python
200
star
29

terraform-provider-ansible

community terraform provider for ansible
Go
192
star
30

ansible-rulebook

Python
190
star
31

product-demos

Jinja
184
star
32

receptor

Project Receptor is a flexible multi-service relayer with remote execution and orchestration capabilities linking controllers with executors across a mesh of nodes.
Go
160
star
33

awx-ee

An Ansible execution environment for AWX project
137
star
34

creator-ee

Ansible Execution environment targeted for content creators. It includes most development tools such ansible-lint, molecule, ...
Shell
117
star
35

mazer

Experimental Ansible Galaxy Content Manager
Python
114
star
36

ansible-for-rubyists

Ansible is written in Python, but you can write modules in any language. Here are some Ruby examples to get you started.
Ruby
108
star
37

immutablish-deploys

Python
99
star
38

proposals

Repository for sharing and tracking progress on enhancement proposals for Ansible.
91
star
39

ansible-documentation

Ansible community documentation
Python
83
star
40

ansible-container-examples

A few starter applications to demonstrate features and provide examples.
Python
76
star
41

ansible-creator

The fastest way to generate all your ansible content!
Python
75
star
42

ansible-kubernetes-modules

DEPRECATED Ansible role containing pre-release K8s modules
Python
73
star
43

instruqt

Self-paced instruqt Training material
Shell
71
star
44

ansible-ui

Ansible UI
TypeScript
67
star
45

ansible-hub-ui

Ansible Automation Hub UI
TypeScript
66
star
46

tacacs_plus

A Python-based TACACS+ client that supports authentication, authorization and accounting.
Python
64
star
47

ansible-dev-tools

Ansible automation developer tools
Python
63
star
48

ansible-container-demo

Manage the application lifecycle from development to deployment using Ansible Container
JavaScript
61
star
49

pytest-mp

multiprocessing.Process(target=pytest_runtest_protocol, args=(your_test, None))
Python
61
star
50

pylibssh

Python bindings specific to Ansible use case for libssh https://www.libssh.org/
Cython
60
star
51

galaxy_collection

Collection of modules and roles to configure Automation Hub
Jinja
58
star
52

autoscaling-blog

Companion playbooks to an article at http://www.ansible.com/blog/autoscaling-infrastructures
56
star
53

tox-ansible

The tox-ansible plugin dynamically creates a full matrix of python interpreter and ansible-core version environments for running integration, sanity, and unit for an ansible collection both locally and in a Github action. tox virtual environments are leveraged for collection building, collection installation, dependency installation, and testing.
Python
53
star
54

ansible-tower-samples

Ansible Tower Playbook Samples
46
star
55

schemas

❗️Schemas are now managed inside ansible-lint project
TypeScript
44
star
56

ansible-baseline

A baseline playbook for testing Ansible performance
Python
41
star
57

awx-resource-operator

Jinja
41
star
58

role-secure-docker-daemon

Ansible role to generate server and client certificates for your docker daemon
Shell
38
star
59

workshop-examples

This repository contains demo playbooks and roles used in our Ansible Workshops.
37
star
60

ansible.github.com

nothing to see here, this just makes ansible.github.com/io a redirect to the main project page
JavaScript
35
star
61

ansible-blog-examples

Example playbooks from posts on the Ansible blog (https://www.ansible.com/blog)
Python
34
star
62

eda-server-operator

Jinja
34
star
63

eda-server-prototype

Python
33
star
64

awx-facts-playbooks

Repository containing playbooks to support fact scanning in Ansible Tower and AWX.
Python
33
star
65

ansible-risk-insight

Ansible Risk Insight (ARI) is the tool to evaluate the quality and risk of the ansible content.
Python
32
star
66

galaxy-lint-rules

Ansible Lint rules used by Galaxy and Mazer to evaluate Ansible content
Python
29
star
67

tower-example

Ansible Tower Example Playbooks
28
star
68

ansible-lightspeed

This repository is no longer in use. The Ansible Lightspeed with IBM watsonx Code Assistant product documentation can be found at https://docs.redhat.com/en/documentation/red_hat_ansible_lightspeed_with_ibm_watsonx_code_assistant.
28
star
69

ansible-runner-http

Python
28
star
70

distro-test-containers

Distribution specific containers for Ansible integration testing.
Dockerfile
27
star
71

galaxy-importer

Galaxy content importer
Python
26
star
72

project-config

Zuul configuration files for the Ansible tenant
Python
25
star
73

awx-logos

Less
25
star
74

role-install-gcloud

Install Google Cloud SDK and Kubernetes kubectl CLI.
Shell
24
star
75

ansible-zuul-jobs

Zuul job definitions for the Ansible tenant.
Python
23
star
76

ansible-sdk

The Ansible SDK
Python
23
star
77

azure-testing

Former home for Ansible Azure module testing. Testing is now part of the main Ansible repository.
21
star
78

network-infra-playbooks

Playbooks and roles for installing and managing Ansible networking CI
Shell
21
star
79

ansible-policy

ansible-policy is a prototype implementation which allows us to define and set constraints to the Ansible project in OPA Rego language.
Python
21
star
80

galaxy-issues

This repository exists solely for the tracking of user issues with Ansible Galaxy.
20
star
81

vcenter-test-container

vCenter simulator container for testing.
Python
20
star
82

docsite

Static HTML and assets for docs.ansible.com
HTML
19
star
83

ansible-content-actions

Combine GitHub Actions to create a streamlined workflow for testing Ansible collection repositories on GitHub.
19
star
84

django-gulp-nginx

Django + PostgreSQL + Nginx with Gulp-built static assets framework, managed with Ansible Container
JavaScript
19
star
85

aap-docs

Asciidoc technical content for Ansible Automation Platform
19
star
86

terraform-provider-aap

Terraform Provider for Ansible Automation Platform
Go
18
star
87

pinakes

Python
18
star
88

ansible_tower_client_ruby

Ruby gem for the Ansible Tower REST API
Ruby
18
star
89

ansible-compat

A python package containing functions that help interacting with various versions of Ansible
Python
18
star
90

community-docs

docs.ansible.com/community
18
star
91

ambassadors

A repository of useful materials for Ansible Ambassadors around the world.
17
star
92

team-devtools

Shared practices, workflows and decisions impacting Ansible devtools projects
Dockerfile
17
star
93

test-network-modules

Playbooks for testing Ansible core network modules
JavaScript
17
star
94

ansible-dev-environment

Build and maintain a development environment including ansible collections and their python dependencies
Python
17
star
95

docker-testing

New Docker modules.
Shell
17
star
96

network

Ansible collection for network devices
16
star
97

tower-nagios-integration

Scripts and documentation related to the integration of Ansible Tower with Nagios.
Python
15
star
98

django-template

A Django project template for Ansible Container
Python
15
star
99

logos

Ansible upstream logos
Shell
14
star
100

nginx-container

Add an nginx service to your Ansible Container project
Python
14
star