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
exec - Execute InSpec ProfileRun 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.ymlSingle host:
user@hostnameLocal:
local://Docker:
docker://container_name
--reporter REPORTER- Output format for resultsSingle:
json,html,junit,cliMultiple:
json:path.json html:path.htmlDefault:
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
convert - Convert InSpec Profile to Ansible CollectionTransform 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 collectionDefault:
./collections
-n, --namespace NAME- Ansible Galaxy namespaceDefault:
compliance
-c, --collection-name NAME- Collection nameDefault: 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
supermarket - Interact with Chef SupermarketSearch 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 directorytarget(str): Target specificationreporter(str): Output format (json, html, junit, cli)output_path(Optional[str]): Path for report outputcontrols(Optional[List[str]]): List of control IDstags(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 profileoutput_dir(str): Output directory for collectionnamespace(str): Ansible Galaxy namespacecollection_name(Optional[str]): Collection name (auto-generated if None)force(bool): Overwrite existing collectionbuild(bool): Build collection tarball
Methods:
convert() -> str- Convert profile, returns collection pathbuild() -> 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 versionprofiles(List[InSpecProfile]): List of profilesplatform(InSpecPlatform): Platform informationstatistics(InSpecStatistics): Execution statisticserrors(List[str]): Execution errors
Methods:
to_dict() -> Dict- Convert to dictionaryto_json(indent: int = 2) -> str- Generate JSON stringsave(path: str, indent: int = 2) -> None- Save to file
InSpecControl
Represents a single control.
Attributes:
id(str): Control IDtitle(str): Control titledesc(str): Descriptionimpact(float): Impact score (0.0 - 1.0)tags(Dict): Tagscode(str): Control codesource_location(Dict): Source file locationresults(List[Dict]): Test results
InSpecProfile
Represents an InSpec profile.
Attributes:
name(str): Profile nametitle(str): Profile titleversion(str): Versionsummary(str): Summarycontrols(List[InSpecControl]): Controls
InSpecPlatform
Platform information.
Attributes:
name(str): Platform namerelease(str): Release versiontarget_id(str): Target identifier
InSpecStatistics
Execution statistics.
Attributes:
duration(float): Execution duration in secondscontrols(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 formatANSIBLE_INSPEC_OUTPUT_DIR- Default output directoryANSIBLE_CONFIG- Ansible configuration fileANSIBLE_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