State 2 inventory
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-inventoryjfxs/ansible
docker run -it --rm -v $(pwd):/data jfxs/terraform-inventory terraform --state /data/my_state.json --output /data/inventory.txt
Authors
- FX Soubirou - Initial work - GitLab repositories
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.
Release files for state2inventory 1.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| state2inventory-1.1.1.tar.gz | 6.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| state2inventory-1.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 14.2 kB
Release files / state2inventory-1.1.1.tar.gz
| Download URL | state2inventory-1.1.1.tar.gz |
|---|---|
| Size | 6.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cb614fcdef7a4a4bc0d095fa6d160cfafc89face756e6cdc577631211c5e2eac
|
|
BLAKE2b-256 checksum How to use checksums |
ff176122921fbe9f798f9454d9ecdbc55d861a8dccec3d0a8073f1d65cc91737
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.0 CPython/3.12.0 Linux/5.4.109+
|
Release files / state2inventory-1.1.1-py3-none-any.whl
| Download URL | state2inventory-1.1.1-py3-none-any.whl |
|---|---|
| Size | 8.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6af6d8ddc2a405e73bea8e4cd9a6341db80b0c165b0f90366bdbec49dd138f43
|
|
BLAKE2b-256 checksum How to use checksums |
438ed9f974fd1673859576f7d310701d2df96ba8304688e208aadecdef7b6273
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.0 CPython/3.12.0 Linux/5.4.109+
|