Skip to content

vossenjp/hosts2inventory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hosts2inventory

Parse a special hosts file to create an Ansible inventory

Introduction

I love Ansible, but I don't like the INI format of the inventory file because it's very WET. There are lots of ways around that, but for one reason or another I don't like them either, so I wrote this to work the way I think the Ansible inventory should work.

It's very simple: it takes a list of nodes with an indented list of either groups to which that node belongs, or inventory variables for that node, and it builds a WET Ansible INI inventory.

Installing

Just clone the repo someplace and use whichever files you want.

  1. hosts2inventory.pl does all the work
  2. hosts2inventory.sh is a trivial wrapper to "do the right thing" ("write" thing?)

Use

  1. ./hosts2inventory.sh
  2. cat /tmp/hosts.txt | ./hosts2inventory.pl (-o <hosts>)
  3. gc | ./hosts2inventory.pl (-o <hosts>)
  4. ./hosts2inventory.pl (-h -D) # for help or debugging

Use -o - for STDOUT, default if omitted is ./hosts.

Note gc is my alias gc='xsel -b' and it means "Get from the Clipboard" so I can create an inventory on-the-fly from random stuff I just typed into an editor.

Examples

If I run gc | ./hosts2inventory.pl -o - then:

This hosts2.txt file:

desktop1
    desktop
    gui
    workstation
    variable1=foo
laptop1
    gui
    laptop
    workstation

Becomes this hosts inventory file:

# DO NOT MANUALLY EDIT THIS FILE!
# Ansible inventory generated by 'hosts2inventory.pl' on 2020-12-30 13:54:10

# [all] 2 nodes
desktop1	variable1=foo
laptop1

[desktop] # 1 nodes
desktop1

[gui] # 2 nodes
desktop1
laptop1

[laptop] # 1 nodes
laptop1

[workstation] # 2 nodes
desktop1
laptop1

About

Parse a special hosts file to create an Ansible inventory

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published