Generate an Ansible inventory file from an infrastructure state
Project description
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-inventory
jfxs/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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file state2inventory-1.1.1.tar.gz
.
File metadata
- Download URL: state2inventory-1.1.1.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.0 CPython/3.12.0 Linux/5.4.109+
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cb614fcdef7a4a4bc0d095fa6d160cfafc89face756e6cdc577631211c5e2eac |
|
MD5 | e6268dd0e047e0b1e6ac5819c9378ce5 |
|
BLAKE2b-256 | ff176122921fbe9f798f9454d9ecdbc55d861a8dccec3d0a8073f1d65cc91737 |
File details
Details for the file state2inventory-1.1.1-py3-none-any.whl
.
File metadata
- Download URL: state2inventory-1.1.1-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.0 CPython/3.12.0 Linux/5.4.109+
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6af6d8ddc2a405e73bea8e4cd9a6341db80b0c165b0f90366bdbec49dd138f43 |
|
MD5 | 3c069ef4533047e2261049a945a43823 |
|
BLAKE2b-256 | 438ed9f974fd1673859576f7d310701d2df96ba8304688e208aadecdef7b6273 |