Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weshayutin/1a4d66191132851ebe56446b9cb8cd91 to your computer and use it in GitHub Desktop.
Save weshayutin/1a4d66191132851ebe56446b9cb8cd91 to your computer and use it in GitHub Desktop.
quick and dirty tool to scan an ansible archive and return a complete list of modules used throughout the archive.
import argparse
import collections
import os
import yaml
MODULES = collections.defaultdict(int)
POP_ITEMS = ['always',
'args',
'assert',
'async',
'async_status',
'authorized_key',
'become',
'become_user',
'delay',
'delegate_to',
'environment',
'failed_when',
'group',
'ignore_errors',
'loop',
'loop_control',
'name',
'no_log',
'notify',
'poll',
'register',
'rescue',
'retires',
'run_once',
'sefcontext',
'state',
'tags',
'until',
'user',
'vars',
'when'
]
def parse_opts():
"""Parse options."""
parser = argparse.ArgumentParser(
description="Scan an ansible archive and return all used modules."
)
parser.add_argument(
"path", help="ansible archive to scan"
)
return parser.parse_args()
def parse_task(task):
for i in POP_ITEMS:
task.pop(i, None)
for k in task.keys():
if k.startswith("with_"):
continue
if k == "block":
for b in task[k]:
parse_task(task=b)
MODULES[k] += 1
def main():
opts = parse_opts()
for root, dirs, files in os.walk(opts.path):
for f in files:
file_path = os.path.join(root, f)
if f.endswith(('yml', 'yaml')) and os.path.exists(file_path):
with open(file_path) as y:
content = yaml.safe_load(y)
if isinstance(content, list):
for item in content:
if 'hosts' in item:
for task_key in ["pre_tasks", "tasks", "post_tasks"]:
task_values = item.get(task_key)
if task_values:
for task_value in task_values:
parse_task(task=task_value)
else:
parse_task(task=item)
print(yaml.safe_dump(dict(MODULES), default_flow_style=False))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment