-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathansible_playtest_cli.py
More file actions
62 lines (49 loc) · 2.04 KB
/
Copy pathansible_playtest_cli.py
File metadata and controls
62 lines (49 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python3
"""
Command-line interface for ansible-playtest:
A tool for scenario-based testing of Ansible playbooks
"""
import os
import sys
import argparse
from ansible_playtest.core.playbook_runner import (
PlaybookRunner
)
# Add the src directory to path to make imports work
script_dir = os.path.dirname(os.path.abspath(__file__))
src_dir = os.path.join(script_dir, 'src')
if src_dir not in sys.path:
sys.path.insert(0, src_dir)
def main():
"""Main function for the ansible-playtest CLI"""
parser = argparse.ArgumentParser(description='Run Ansible playbooks with scenario-based testing')
parser.add_argument('playbook', help='Path to the playbook to test')
parser.add_argument('--scenario', '-s', required=True, help='Name of the scenario to use')
parser.add_argument('--inventory', '-i', help='Path to inventory file')
parser.add_argument('--extra-var', '-e', action='append', help='Extra variables (key=value format)')
parser.add_argument('--keep-mocks', '-k', action='store_true', help='Keep mock files after execution for debugging')
# SMTP server options
smtp_group = parser.add_argument_group('SMTP Server Options')
smtp_group.add_argument('--no-smtp', action='store_true', help='Disable mock SMTP server')
smtp_group.add_argument('--smtp-port', type=int, default=1025, help='Port for the mock SMTP server (default: 1025)')
args = parser.parse_args()
# Process extra vars
extra_vars = {}
if args.extra_var:
for var in args.extra_var:
if '=' in var:
key, value = var.split('=', 1)
extra_vars[key] = value
# Run the playbook with the specified scenario using the class
runner = PlaybookRunner()
success, _ = runner.run_playbook_with_scenario(
args.playbook,
args.scenario,
inventory_path=args.inventory,
extra_vars=extra_vars,
keep_mocks=args.keep_mocks,
)
# Exit with appropriate return code
sys.exit(0 if success else 1)
if __name__ == '__main__':
main()