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.py [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.py terraform --state my_state.json --output inventory.txt
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
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
Hashes for state2inventory-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 633255cdc4b1764afaf82a35c2054764f617b45c06a42effc0b71dc52ad64570 |
|
MD5 | ffb38442c1571f1b17ab3fe503a21e98 |
|
BLAKE2b-256 | b66d69b17cb34a8b6b5c60f6b8bb4526eafaaa2d0382a01970d40e1da01f901e |