Skip to main content

Generate an Ansible inventory file from an infrastructure state

Project description

State 2 inventory

Software License Pipeline Status semantic-release: angular

An easy way to generate your Ansible inventory from an infrastructure state.

Usage: state2inventory [OPTIONS] COMMAND [ARGS]...

  A CLI tool to generate an Ansible inventory from different infrastructure
  states.

Options:
  --version  Show the version and exit.
  --help     Show this message and exit.

Commands:
  multipass  Command to generate an Ansible inventory from a multipass...
  terraform  Command to generate an Ansible inventory from a terraform...

Multipass

Generate an Ansible inventory from a multipass state file (multipass list) and a configuration file.

Work in progress.

Terraform

Terraform provider, extract data for an Ansible inventory from the terraform command terraform show -json.

Usage

  • Install state2inventory, see installation step bellow,
  • Generate the file state by running terraform show -json > my_state.json
state2inventory terraform --state my_state.json --output inventory.txt
Options Description Default
--naming, -N Flag to format group variable name to Ansible convention[^1] disabled
--disable-host, -H Flag to remove ansible_host variable in inventory enabled

[^1] All in lower cases, special characters replaced by _, prefix by _ if group begins with a numeric character

Terraform provider options

You can customize the data you want to extract from the state JSON terraform file with the --key option:

Key Description Default Value
type The type of resource to extract openstack_compute_instance_v2
ip The IP field to extract for the ansible_host value in the inventory access_ip_v4
groups Metadata key containing the Ansible groups of that host is member of host_groups
vars Metadata key containing the Ansible variables defined in inventory host_vars

Example: --key {"type": "openstack_compute_instance_v2", "ip": "access_ip_v4" }

Some resource type value:

Cloud Type
Openstack (default) openstack_compute_instance_v2
Google Cloud Plateform google_compute_instance
Oracle Cloud Infrastructure oci_core_instance
Flexible Engine flexibleengine_compute_instance_v2

Terraform metadata

The Terraform optional list key/value metadata stores the host groups and the Ansible host variables in the inventory. The key host_groups key has as value the list of groups of the host separated by a semicolon ;. The key host_vars key has as value the list of connection variables of the host separated by a semicolon ;.

Example:

host_groups = "public;bastion;core"
host_vars = "ansible_user=ubuntu;ansible_python_interpreter=/usr/bin/python3"

Terraform provider generation example

resource "openstack_compute_instance_v2" "bastion" {
  name = "bastion01"
  ...
  metadata = {
    host_groups = "public;bastion;core"
    host_vars = "ansible_user=ubuntu;ansible_python_interpreter=/usr/bin/python3"
  }
  ...
}

generates

bastion01 ansible_host=192.168.0.156 ansible_user=ubuntu ansible_python_interpreter=/usr/bin/python3

[public]
bastion01

[bastion]
bastion01

[core]
bastion01

Installation

With Python environment

To use:

  • Minimal Python version: 3.10

Installation with Python pip:

python3 -m pip install state2inventory
state2inventory --help

With Docker

Images containing state2inventory:

  • jfxs/terraform-inventory
  • jfxs/ansible
docker run -it --rm -v $(pwd):/data jfxs/terraform-inventory terraform --state /data/my_state.json --output /data/inventory.txt

Authors

License

This program is free software: you can redistribute it and/or modify it under the terms of the MIT License (MIT). See the LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

state2inventory-1.1.1.tar.gz (6.2 kB view hashes)

Uploaded Source

Built Distribution

state2inventory-1.1.1-py3-none-any.whl (8.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page