github.com/apprenda/kismatic@v1.12.0/pkg/ansible/inventory.go (about)

     1  package ansible
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  )
     7  
     8  // Inventory is a collection of Nodes, keyed by role.
     9  type Inventory struct {
    10  	Roles []Role
    11  }
    12  
    13  // Role is an Ansible role, containing nodes that belong to the role.
    14  type Role struct {
    15  	// Name of the role
    16  	Name string
    17  	// The nodes that belong to this role
    18  	Nodes []Node
    19  }
    20  
    21  // Node is an Ansible target node
    22  type Node struct {
    23  	// Host is the hostname of the target node
    24  	Host string
    25  	// PublicIP is the publicly accessible IP
    26  	PublicIP string
    27  	// InternalIP is the internal IP, if different from PublicIP.
    28  	InternalIP string
    29  	// SSHPrivateKey is the private key to be used for SSH authentication
    30  	SSHPrivateKey string
    31  	// SSHPort is the SSH port number for connecting to the node
    32  	SSHPort int
    33  	// SSHUser is the SSH user for logging into the node
    34  	SSHUser string
    35  }
    36  
    37  // ToINI converts the inventory into INI format
    38  func (i Inventory) ToINI() []byte {
    39  	w := &bytes.Buffer{}
    40  	for _, role := range i.Roles {
    41  		fmt.Fprintf(w, "[%s]\n", role.Name)
    42  		for _, n := range role.Nodes {
    43  			internalIP := n.PublicIP
    44  			if n.InternalIP != "" {
    45  				internalIP = n.InternalIP
    46  			}
    47  			fmt.Fprintf(w, "%q ansible_host=%q internal_ipv4=%q ansible_ssh_private_key_file=%q ansible_port=%d ansible_user=%q\n", n.Host, n.PublicIP, internalIP, n.SSHPrivateKey, n.SSHPort, n.SSHUser)
    48  		}
    49  	}
    50  
    51  	return w.Bytes()
    52  }