API Documentation

Complete API reference for ansible-inspec library and CLI.

Table of Contents


Installation

PyPI Installation

# Install latest version
pip install ansible-inspec

# Install specific version
pip install ansible-inspec==0.1.0

# Install with development dependencies
pip install ansible-inspec[dev]

Docker Installation

See Docker Usage Guide for detailed Docker instructions.


CLI Reference

Global Options

Global Flags:

  • --version - Show version and exit

  • --license - Show license information

  • --help, -h - Show help message

Commands

exec - Execute InSpec Profile

Run InSpec profiles on target systems using Ansible inventory.

Arguments:

  • PROFILE_PATH - Path to InSpec profile directory

Options:

  • -t, --target TARGET - Target specification (inventory file, host, or special target)

    • File path: /path/to/inventory.yml

    • Single host: user@hostname

    • Local: local://

    • Docker: docker://container_name

  • --reporter REPORTER - Output format for results

    • Single: json, html, junit, cli

    • Multiple: json:path.json html:path.html

    • Default: cli

  • --output PATH - Output file path for reports

  • --controls CONTROLS - Comma-separated list of control IDs to run

  • --tags TAGS - Comma-separated list of tags to filter controls

Examples:

convert - Convert InSpec Profile to Ansible Collection

Transform Ruby-based InSpec profiles into pure Ansible collections.

Arguments:

  • PROFILE_PATH - Path to InSpec profile directory or Supermarket profile name

Options:

  • -o, --output-dir DIR - Output directory for converted collection

    • Default: ./collections

  • -n, --namespace NAME - Ansible Galaxy namespace

    • Default: compliance

  • -c, --collection-name NAME - Collection name

    • Default: Derived from profile name

  • --no-build - Skip building the collection tarball

  • --force - Overwrite existing collection

  • --include-callback - Bundle compliance reporter callback plugin (enabled by default)

Examples:

supermarket - Interact with Chef Supermarket

Search and download compliance profiles from Chef Supermarket.

Subcommands:

search - Search for profiles

Options:

  • -l, --limit N - Limit results (default: 20)

  • -s, --sort FIELD - Sort by: name, downloads, updated (default: updated)

Examples:

info - Get profile details

Examples:

download - Download profile

Options:

  • -o, --output-dir DIR - Download destination (default: ./profiles)

Examples:


Python API

Basic Usage


Core Classes

ExecutionConfig

Configuration for InSpec profile execution.

Attributes:

  • profile_path (str): Path to InSpec profile directory

  • target (str): Target specification

  • reporter (str): Output format (json, html, junit, cli)

  • output_path (Optional[str]): Path for report output

  • controls (Optional[List[str]]): List of control IDs

  • tags (Optional[List[str]]): List of tags to filter

ExecutionResult

Results from InSpec profile execution.

Example:

Runner

Execute InSpec profiles.

Methods:

  • run() -> ExecutionResult - Execute the profile and return results

ProfileConverter

Convert InSpec profiles to Ansible collections.

Parameters:

  • profile_path (str): Path to InSpec profile

  • output_dir (str): Output directory for collection

  • namespace (str): Ansible Galaxy namespace

  • collection_name (Optional[str]): Collection name (auto-generated if None)

  • force (bool): Overwrite existing collection

  • build (bool): Build collection tarball

Methods:

  • convert() -> str - Convert profile, returns collection path

  • build() -> str - Build collection tarball, returns tarball path

Example:


Reporters

InSpec JSON Reporter

Generate InSpec-compatible JSON reports.

Classes:

InSpecJSONReport

Main report container.

Attributes:

  • version (str): InSpec version

  • profiles (List[InSpecProfile]): List of profiles

  • platform (InSpecPlatform): Platform information

  • statistics (InSpecStatistics): Execution statistics

  • errors (List[str]): Execution errors

Methods:

  • to_dict() -> Dict - Convert to dictionary

  • to_json(indent: int = 2) -> str - Generate JSON string

  • save(path: str, indent: int = 2) -> None - Save to file

InSpecControl

Represents a single control.

Attributes:

  • id (str): Control ID

  • title (str): Control title

  • desc (str): Description

  • impact (float): Impact score (0.0 - 1.0)

  • tags (Dict): Tags

  • code (str): Control code

  • source_location (Dict): Source file location

  • results (List[Dict]): Test results

InSpecProfile

Represents an InSpec profile.

Attributes:

  • name (str): Profile name

  • title (str): Profile title

  • version (str): Version

  • summary (str): Summary

  • controls (List[InSpecControl]): Controls

InSpecPlatform

Platform information.

Attributes:

  • name (str): Platform name

  • release (str): Release version

  • target_id (str): Target identifier

InSpecStatistics

Execution statistics.

Attributes:

  • duration (float): Execution duration in seconds

  • controls (Dict): Control counts

HTML Reporter

Generate interactive HTML reports.

HTML Features:

  • Interactive dashboard

  • Control filtering

  • Pass/fail statistics

  • Color-coded results

  • Execution error section

  • Responsive design

JUnit Reporter

Generate JUnit XML for CI/CD integration.

JUnit Features:

  • Test suite per profile

  • Test case per control

  • Failure details

  • Execution timing

  • CI/CD compatible


Converters

Profile Structure Analysis

Custom Resource Handling

The converter automatically detects and converts custom InSpec resources:

Conversion Workflow


Examples

Example 1: Local Compliance Check

Example 2: Multi-Host Compliance

Example 3: Convert and Deploy

Example 4: CI/CD Integration

Example 5: Custom Reporting

Example 6: Chef Supermarket Integration


Configuration

Environment Variables

  • ANSIBLE_INSPEC_REPORTER - Default reporter format

  • ANSIBLE_INSPEC_OUTPUT_DIR - Default output directory

  • ANSIBLE_CONFIG - Ansible configuration file

  • ANSIBLE_INVENTORY - Default inventory file

Ansible Configuration


Error Handling


Support and Resources

  • Documentation: https://github.com/Htunn/ansible-inspec#readme

  • PyPI: https://pypi.org/project/ansible-inspec/

  • Docker Hub: https://hub.docker.com/r/htunnthuthu/ansible-inspec

  • Issues: https://github.com/Htunn/ansible-inspec/issues

  • Changelog: https://github.com/Htunn/ansible-inspec/blob/main/CHANGELOG.md


License

GPL-3.0-or-later. See LICENSE for details.

Last updated