Skip to main content

The static source code measurement tool for Ansible

Build Status Documentation Codecov LGTM Grade pypi-version python-version

AnsibleMetrics is 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 AnsibleMetrics

If 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 install

Installation is made simple by the PyPI repository. Download the tool and install it with:

pip install ansiblemetrics

or, alternatively from the source code project directory:

pip install -r requirements.txt
pip install .

How to use

Command-line

Run ansible-metrics --help for 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 exit

Assume that the following example is named playbook1.yml:

---
- hosts: webservers
  vars:
    http_port: 80
  remote_user: root

  tasks:
  - name: ensure apache is at the latest version
    yum:
      name: httpd
      state: latest
      
- hosts: databases
  remote_user: root

  tasks:
  - name: ensure postgresql is at the latest version
    yum:
      name: postgresql
      state: latest
      
  - name: ensure that postgresql is started
    service:
      name: postgresql
      state: started
      

and is located within the folder playbooks as follows:

playbooks
   |- playbook1.yml
   |- playbook2.yml
   |- playbook3.yml

Also, assume the user's working directory is the playbooks folder. 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.json

For 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
}

Python

AnsibleMetrics currently supports up to 46 source code metrics, implemented in Python. To extract the value for a given metric follow this pattern:

from ansiblemetrics.<general|playbook>.metric import Metric

script = 'a valid yaml script'
value = Metric(script).count()

where metric and Metric have to be replaced with the name of the desired metric module to compute the value of a specific metric.
The difference between the general and the playbook modules lies in the fact that the playbook module contains metrics specific to playbooks (for example, the number of plays and tasks), while the general module 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:

from ansiblemetrics.general.lines_code import LinesCode

script = """
---
- hosts: all

  tasks:
  - 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 the ansiblemetrics.metrics_extractor package and call the method extract_all() (in this case the return value will be a json object):

from ansiblemetrics.metrics_extractor import extract_all

script = """
---
- hosts: all

  tasks:
  - name: This is a task!
    debug:
      msg: "Hello World"
"""

metrics = extract_all(script)
print('Lines of executable code:', metrics['lines_code'])

Metadata

Release files for ansiblemetrics 0.4.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ansiblemetrics 0.4.2
File Size Uploaded Provenance Actions
ansiblemetrics-0.4.2.tar.gz 59.6 kB Without provenance Details for ansiblemetrics-0.4.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ansiblemetrics 0.4.2
File Interpreter ABI Platform Actions
ansiblemetrics-0.4.2-py3-none-any.whl Python 3 none any Details for ansiblemetrics-0.4.2-py3-none-any.whl

Total release size: 161.5 kB

Release files / ansiblemetrics-0.4.2.tar.gz

Download URL ansiblemetrics-0.4.2.tar.gz
Size 59.6 kB
Tags Source
SHA-256 checksum
How to use checksums
f34fbae82d96ad7122c9491917f092cfb3043d9563f368fc4e90703392079247
BLAKE2b-256 checksum
How to use checksums
7b694a5cc1067edfefc153d3d3281e73d17ef529fc1f33506ef6d35fe290437a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release files / ansiblemetrics-0.4.2-py3-none-any.whl

Download URL ansiblemetrics-0.4.2-py3-none-any.whl
Size 101.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c3de14d2fcea68a598216b15a33d1e973d6019288530b282ca84da06aa95a8aa
BLAKE2b-256 checksum
How to use checksums
c3f97af437b714bcc804b24f757736889791ac7c6a94f275712a87ae44f463f0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Security for ansiblemetrics 0.4.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page