/terraform-local-ansible-inventory

Terraform module to create an Ansible Inventory file from a list of server IPs

Primary LanguageHCL

Local Ansible Inventory

A Terraform module that will render groups of server IP addresses and variables into an Ansible inventory file and output it to the local filesystem at a specified location.

The module will also take arbitrary secrets (certificates, tokens, keys, etc.) and write them to files in a subdirectory of the output path.

You can also generate server labels into json format and export them as global variables in the inventory file for later use by Ansible.

Usage

provider "local" {
  version = "~> 1.3"
}

module "inventory_production" {
  source  = "Peymanpn/ansible-inventory/local"
  version = "0.6.3"


  servers = {
    workers = my_provider_server.worker_nodes.*.ipv4_address   # for a list of managers 
    manager = [my_provider_server.manager_node.ipv4_address]   # for a single server
  }

  host_vars = {
    db_master_volume = {
      hostname = "db_master"
      variables = {
        some__volume_id                                          = my_provider_volume.some_volume.id
        hostname                                                 = my_provider_server.some_server.name
        server_id                                                = my_provider_server.some_server.id
        ipv4                                                     = my_provider_server.some_server.ipv4_address
        ipv6                                                     = my_provider_server.some_server.ipv6_address
      }
  }
  global_vars = {
    operating_system        = "my_awesome_os"
    some_setting            = "some_value"

  }
  servers_labels = { # generates json encoded labels
    workers_group = {
      for server in ,u.worker :
      server.name => server.labels
    }
    manager_group = {
      tostring(my_provider_server.manager.name) = my_provider_server.manager.labels
    }
  }
  secrets = {
    tls_key  = "-----BEGIN RSA PRIVATE KEY----- MIIEow..."
    tls_cert = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD..."
  }
  output  = "inventory/${var.environment_name}/hosts.ini"
}

Sample Inventory

[workers]
5.6.11.11
5.6.11.12
5.6.11.13

[manager]
7.8.9.10


[manager:vars]
some_var=some_value

[all:vars]
hostname=

all_labels_json={"some_server":{"some_label":"some_value"}}

Sample generated labels

Note: Actual JSON string is minified

{
    "manager_group": {
        "manager": {
            "some_label": "some_value",
            "other_label": "some_other_value"
        }
    },
    "workers_group": {
        "worker-0": {
            "some_label_for_worker-0": "some_value",
            "other_label_for_worker-0": "some_other_value"
        }
    }
}

An inventory file will be created at the provided output path with ini format.

ChangeLog

0.4.1

it will create a [all] list beside the server - IP map

0.5.0

Needs to provide server IP collection. This has changed to be able to use private IPs as well as the public IPs. Now single servers (without count) will be listed

0.6.0

Added the ability to generate host variables exported from terraform.

host_vars = {
    some_host_variables = {
      host  = "my_awesome_host"
      key   = "host_some_variable"
      value = my_provider.some_volume.linux_device
    }
  }

will produce:

[my_awesome_host:vars]
host_some_variable=whatever was in my_provider.some_volume.linux_device

Module

can add from Terraform Registry at: Terraform Registry

Module Usage:

module "ansible-inventory" {
  source  = "Peymanpn/ansible-inventory/local"
  version = "0.6.3"
}