Package: ansiblemetrics
The static source code measurement tool for AnsibleAnsibleMetricsis a Python-based static source code measurement tool to characterize Infrastructure-as-Code.
It helps quantify the characteristics of infrastructure code to support DevOps engineers when maintaining and evolving it.
It currently supports 46 source code metrics, though other metrics can be derived by combining the implemented ones.How to cite AnsibleMetricsIf you use AnsibleMetrics in a scientific publication, we would appreciate citations to the following paper:@article{DALLAPALMA2020100633,
    title = "AnsibleMetrics: A Python library for measuring Infrastructure-as-Code blueprints in Ansible",
    journal = "SoftwareX",
    volume = "12",
    pages = "100633",
    year = "2020",
    issn = "2352-7110",
    doi = "https://doi.org/10.1016/j.softx.2020.100633",
    url = "http://www.sciencedirect.com/science/article/pii/S2352711020303460",
    author = "Stefano {Dalla Palma} and Dario {Di Nucci} and Damian A. Tamburri",
    keywords = "Infrastructure as Code, Software metrics, Software quality",
    abstract = "Infrastructure-as-Code (IaC) has recently received increasing attention in the research community, mainly due to the paradigm shift it brings in software design, development, and operations management. However, while IaC represents an ever-increasing and widely adopted practice, concerns arise about the need for instruments that help DevOps engineers efficiently maintain, speedily evolve, and continuously improve Infrastructure-as-Code. In this paper, we present AnsibleMetrics, a Python-based static source code measurement tool to characterize Infrastructure-as-Code. Although we focus on Ansible, the most used language for IaC, our tool could be easily extended to support additional formats. AnsibleMetrics represents a step forward towards software quality support for DevOps engineers developing and maintaining infrastructure code."
}How to installInstallation is made simple by thePyPI repository.
Download the tool and install it with:pip install ansiblemetricsor, alternatively from the source code project directory:pip install -r requirements.txt
pip install .How to useCommand-lineRunansible-metrics --helpfor instructions about the usage:usage: ansible-metrics [-h] [--omit-zero-metrics] [-d DEST] [-o] [-v] src

Extract metrics from Ansible scripts.

positional arguments:
  src                   source file (playbook or tasks file) or
                        directory

optional arguments:
  -h, --help            show this help message and exit
  --omit-zero-metrics   omit metrics with value equal 0
  -d DEST, --dest DEST  destination path to save results
  -o, --output          shows output
  -v, --version         show program's version number and exitAssume that the following example is namedplaybook1.yml:----hosts:webserversvars:http_port:80remote_user:roottasks:-name:ensure apache is at the latest versionyum:name:httpdstate:latest-hosts:databasesremote_user:roottasks:-name:ensure postgresql is at the latest versionyum:name:postgresqlstate:latest-name:ensure that postgresql is startedservice:name:postgresqlstate:startedand is located within the folderplaybooksas follows:playbooks|- playbook1.yml|- playbook2.yml|- playbook3.ymlAlso, assume the user's working directory is theplaybooksfolder. Then, it is possible to extract source code characteristics from that blueprint by running the following command:ansible-metrics --omit-zero-metrics playbook1.yml --dest report.jsonFor this example, the report.json will result in{
    "filepath": "playbook1.yml",
    "avg_play_size": 10,
    "avg_task_size": 4,
    "lines_blank": 4,
    "lines_code": 20,
    "num_keys": 20,
    "num_parameters": 6,
    "num_plays": 2,
    "num_tasks": 3,
    "num_tokens": 50,
    "num_unique_names": 3,
    "num_vars": 1,
    "text_entropy": 4.37
}PythonAnsibleMetricscurrently supports up to 46 source code metrics, implemented in Python.
To extract the value for a given metric follow this pattern:fromansiblemetrics.<general|playbook>.metricimportMetricscript='a valid yaml script'value=Metric(script).count()wheremetricandMetrichave to be replaced with the name of the desired metric module to compute the value of a specific metric.The difference between thegeneraland theplaybookmodules lies in the fact that theplaybookmodule contains metrics specific to playbooks (for example, the number of plays and tasks), while thegeneralmodule contains metrics that can be generalized to other languages (for example, the lines of code).For example, to count the number of lines of code:fromansiblemetrics.general.lines_codeimportLinesCodescript="""---- hosts: alltasks:- name: This is a task!debug:msg: "Hello World""""print('Lines of executable code:',LinesCode(script).count())To extract the value for the 46 metrics at once,  import theansiblemetrics.metrics_extractorpackage and call the methodextract_all()(in this case the return value will be a json object):fromansiblemetrics.metrics_extractorimportextract_allscript="""---- hosts: alltasks:- name: This is a task!debug:msg: "Hello World""""metrics=extract_all(script)print('Lines of executable code:',metrics['lines_code'])