# State 2 inventory
[](LICENSE)
[](https://gitlab.com/op_so/ansible/state2inventory/pipelines)
[](https://github.com/semantic-release/semantic-release)
An easy way to generate your [`Ansible` inventory](https://docs.ansible.com/ansible/latest/getting_started/get_started_inventory.html) from an infrastructure state.
```bash
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`](https://developer.hashicorp.com/terraform/cli/commands/show).
### Usage
- Install state2inventory, see [installation](#installation) step bellow,
- Generate the file state by running `terraform show -json > my_state.json`
```bash
state2inventory terraform --state my_state.json --output inventory.txt
```
| Options | Description | Default |
| ---------------- | ------------------------------------------------------------ | ---------- |
| `--naming`, `-N` | Flag to format group variable name to [`Ansible` convention](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_variables.html#creating-valid-variable-names)[^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](https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html#connection-variables) of the host separated by a semicolon `;`.
Example:
```bash
host_groups = "public;bastion;core"
host_vars = "ansible_user=ubuntu;ansible_python_interpreter=/usr/bin/python3"
```
### Terraform provider generation example
```bash
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
```bash
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`:
```bash
python3 -m pip install state2inventory
state2inventory --help
```
### With `Docker`
Images containing `state2inventory`:
- `jfxs/terraform-inventory`
- `jfxs/ansible`
```bash
docker run -it --rm -v $(pwd):/data jfxs/terraform-inventory terraform --state /data/my_state.json --output /data/inventory.txt
```
## Authors
<!-- vale off -->
- **FX Soubirou** - *Initial work* - [GitLab repositories](https://gitlab.com/op_so)
<!-- vale on -->
## License
<!-- vale off -->
This program is free software: you can redistribute it and/or modify it under the terms of the MIT License (MIT). See the [LICENSE](https://opensource.org/licenses/MIT) for details.
<!-- vale on -->
Raw data
{
"_id": null,
"home_page": "https://gitlab.com/op_so/ansible/state2inventory/",
"name": "state2inventory",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.10,<4.0",
"maintainer_email": "",
"keywords": "",
"author": "FX Soubirou",
"author_email": "soubirou@yahoo.fr",
"download_url": "https://files.pythonhosted.org/packages/ff/17/6122921fbe9f798f9454d9ecdbc55d861a8dccec3d0a8073f1d65cc91737/state2inventory-1.1.1.tar.gz",
"platform": null,
"description": "# State 2 inventory\n\n[](LICENSE)\n[](https://gitlab.com/op_so/ansible/state2inventory/pipelines)\n[](https://github.com/semantic-release/semantic-release)\n\nAn easy way to generate your [`Ansible` inventory](https://docs.ansible.com/ansible/latest/getting_started/get_started_inventory.html) from an infrastructure state.\n\n```bash\nUsage: state2inventory [OPTIONS] COMMAND [ARGS]...\n\n A CLI tool to generate an Ansible inventory from different infrastructure\n states.\n\nOptions:\n --version Show the version and exit.\n --help Show this message and exit.\n\nCommands:\n multipass Command to generate an Ansible inventory from a multipass...\n terraform Command to generate an Ansible inventory from a terraform...\n```\n\n## `Multipass`\n\nGenerate an `Ansible` inventory from a `multipass` state file (`multipass list`) and a configuration file.\n\n**Work in progress**.\n\n## `Terraform`\n\nTerraform provider, extract data for an `Ansible` inventory from the terraform command [`terraform show -json`](https://developer.hashicorp.com/terraform/cli/commands/show).\n\n### Usage\n\n- Install state2inventory, see [installation](#installation) step bellow,\n- Generate the file state by running `terraform show -json > my_state.json`\n\n```bash\nstate2inventory terraform --state my_state.json --output inventory.txt\n```\n\n| Options | Description | Default |\n| ---------------- | ------------------------------------------------------------ | ---------- |\n| `--naming`, `-N` | Flag to format group variable name to [`Ansible` convention](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_variables.html#creating-valid-variable-names)[^1] | `disabled` |\n| `--disable-host`, `-H` | Flag to remove `ansible_host` variable in inventory | `enabled` |\n\n[^1] All in lower cases, special characters replaced by `_`, prefix by `_` if group begins with a numeric character\n\n### `Terraform` provider options\n\nYou can customize the data you want to extract from the state JSON terraform file with the `--key` option:\n\n| Key | Description | Default Value |\n| -------- | ----------------------------------------------------------------------- | ------------------------------- |\n| `type` | The type of resource to extract | `openstack_compute_instance_v2` |\n| `ip` | The `IP` field to extract for the `ansible_host` value in the inventory | `access_ip_v4` |\n| `groups` | Metadata key containing the `Ansible` groups of that host is member of | `host_groups` |\n| `vars` | Metadata key containing the `Ansible` variables defined in inventory | `host_vars` |\n\nExample: `--key {\"type\": \"openstack_compute_instance_v2\", \"ip\": \"access_ip_v4\" }`\n\nSome resource type value:\n\n| Cloud | Type |\n| ----------------------------- | ------------------------------------ |\n| `Openstack` (default) | `openstack_compute_instance_v2` |\n| `Google Cloud Plateform` | `google_compute_instance` |\n| `Oracle Cloud Infrastructure` | `oci_core_instance` |\n| `Flexible Engine` | `flexibleengine_compute_instance_v2` |\n\n### Terraform metadata\n\nThe `Terraform` optional list key/value `metadata` stores the host groups and the `Ansible` host variables in the inventory.\nThe key `host_groups` key has as value the list of groups of the host separated by a semicolon `;`.\nThe key `host_vars` key has as value the list of [connection variables](https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html#connection-variables) of the host separated by a semicolon `;`.\n\nExample:\n\n```bash\nhost_groups = \"public;bastion;core\"\nhost_vars = \"ansible_user=ubuntu;ansible_python_interpreter=/usr/bin/python3\"\n```\n\n### Terraform provider generation example\n\n```bash\nresource \"openstack_compute_instance_v2\" \"bastion\" {\n name = \"bastion01\"\n ...\n metadata = {\n host_groups = \"public;bastion;core\"\n host_vars = \"ansible_user=ubuntu;ansible_python_interpreter=/usr/bin/python3\"\n }\n ...\n}\n```\n\ngenerates\n\n```bash\nbastion01 ansible_host=192.168.0.156 ansible_user=ubuntu ansible_python_interpreter=/usr/bin/python3\n\n[public]\nbastion01\n\n[bastion]\nbastion01\n\n[core]\nbastion01\n```\n\n## Installation\n\n### With `Python` environment\n\nTo use:\n\n- Minimal Python version: 3.10\n\nInstallation with Python `pip`:\n\n```bash\npython3 -m pip install state2inventory\nstate2inventory --help\n```\n\n### With `Docker`\n\nImages containing `state2inventory`:\n\n- `jfxs/terraform-inventory`\n- `jfxs/ansible`\n\n```bash\ndocker run -it --rm -v $(pwd):/data jfxs/terraform-inventory terraform --state /data/my_state.json --output /data/inventory.txt\n```\n\n## Authors\n\n<!-- vale off -->\n- **FX Soubirou** - *Initial work* - [GitLab repositories](https://gitlab.com/op_so)\n<!-- vale on -->\n\n## License\n\n<!-- vale off -->\nThis program is free software: you can redistribute it and/or modify it under the terms of the MIT License (MIT). See the [LICENSE](https://opensource.org/licenses/MIT) for details.\n<!-- vale on -->\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Generate an Ansible inventory file from an infrastructure state",
"version": "1.1.1",
"project_urls": {
"Documentation": "https://op_so.gitlab.io/ansible/state2inventory/",
"Homepage": "https://gitlab.com/op_so/ansible/state2inventory/"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "438ed9f974fd1673859576f7d310701d2df96ba8304688e208aadecdef7b6273",
"md5": "3c069ef4533047e2261049a945a43823",
"sha256": "6af6d8ddc2a405e73bea8e4cd9a6341db80b0c165b0f90366bdbec49dd138f43"
},
"downloads": -1,
"filename": "state2inventory-1.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "3c069ef4533047e2261049a945a43823",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10,<4.0",
"size": 7951,
"upload_time": "2023-11-05T19:03:06",
"upload_time_iso_8601": "2023-11-05T19:03:06.439610Z",
"url": "https://files.pythonhosted.org/packages/43/8e/d9f974fd1673859576f7d310701d2df96ba8304688e208aadecdef7b6273/state2inventory-1.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "ff176122921fbe9f798f9454d9ecdbc55d861a8dccec3d0a8073f1d65cc91737",
"md5": "e6268dd0e047e0b1e6ac5819c9378ce5",
"sha256": "cb614fcdef7a4a4bc0d095fa6d160cfafc89face756e6cdc577631211c5e2eac"
},
"downloads": -1,
"filename": "state2inventory-1.1.1.tar.gz",
"has_sig": false,
"md5_digest": "e6268dd0e047e0b1e6ac5819c9378ce5",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10,<4.0",
"size": 6204,
"upload_time": "2023-11-05T19:03:07",
"upload_time_iso_8601": "2023-11-05T19:03:07.787751Z",
"url": "https://files.pythonhosted.org/packages/ff/17/6122921fbe9f798f9454d9ecdbc55d861a8dccec3d0a8073f1d65cc91737/state2inventory-1.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-11-05 19:03:07",
"github": false,
"gitlab": true,
"bitbucket": false,
"codeberg": false,
"gitlab_user": "op_so",
"gitlab_project": "ansible",
"lcname": "state2inventory"
}