Ansible inventory helpers
psansible.inventory simplifies the management of static inventories by allowing the automation of every component that an inventory is composed of.
psansible.inventory allows to work with objects rather than with plain text / regexe's, and enable's complex scenarios around the automation of ansible inventories.
Currently, the following scenarios are supported:
- Inventory file import/manage/export
- Inventory structure import/manage/export
- Inventory entries (Read/edit/write)
- Inventory groups (Read/edit/write)
- Hierarchy definitions (Read/edit/write)
- group vars (Read/edit/write)
- host vars (Read/edit/write)
psansible.inventory enabled what dynamic inventories offers, but for static inventories (and does it in an easy, standard proefficient way).
First and formost you will need to install powershell. See the following aricle to install it on linux. Once installed, call
pwsh
from any shell to start it.
Once installed, you can download the module as following. (internet connection needed)
install-module -Name psansible.inventory
If downloading from the internet is not option, you can download the module locally, and copy / paste it manually
Import-AnsibleInventory -Path '/user/etc/inventory.ini'
New-AnsibleInventory
#return
EntryCollection : TotalEntries: 0
Hierarchy : AnsibleInventoryHierarchyCollection
VariableCollection : AnsibleVariableCollection
GroupCollection :
Path :
In the following example, we are creating a new inventory entry for a server called server123
which is member of the groups HR_Servers
and PW_01
.
New-AnsibleInventoryEntry -NodeName "server123" -Group "HR_Servers","PW_01"
#return
GroupMemberShip NodeName
--------------- --------
{HR_Servers PW_01} server123
The following example shows how to create a host variable
New-AnsibleInventoryVariable -Name "myvar" -value "123" -Type Host -ContainerName "server123"
#Returns
Name Value VarType ContainerName
---- ----- ------- -------------
myvar 123 Host server123
The following example shows how to create a group variable
New-AnsibleInventoryVariable -Name "mydata" -value "456" -Type Group -ContainerName "HR_Servers"
#Returns
Name Value VarType ContainerName
---- ----- ------- -------------
mydata 456 Group HR_Servers
The following example shows how to create a simple Hierarchy. Both HR_Servers
and MK_Servers
are child groups of prod
.
New-AnsibleInventoryHierarchyEntry -ParentName "prod" -Children "HR_Servers","MK_Servers"
Children Parent
-------- ------
{HR_Servers, MK_Servers} prod
The most simplest way of generating an inventory would be as followed.
Export Inventory to ini:
$Inventory = New-AnsibleInventory
Export-AnsibleInventory -Inventory $Inventory -OutputType INI -Path './Inventories/Windows/'
Export Inventory to json:
$Inventory = New-AnsibleInventory
Export-AnsibleInventory -Inventory $Inventory -OutputType json -Path './Inventories/Windows/'