I am writing some ansible playbooks that wrap the VirtualBox VBoxManage cli. The data that is returned is in a tabular colon-separated format, but I'd like it to be in something like JSON/YAML for better parsing.
For example I'd like to take the output of the following command:
$ VBoxManage list hostonlyifs -l
Name: VirtualBox Host-Only Ethernet Adapter #2
GUID: 355aa3ae-0a32-49e6-8532-4d18fd9baea2
DHCP: Disabled
IPAddress: 10.0.10.10
NetworkMask: 255.255.255.0
IPV6Address: fe80::acc9:a7bd:d178:b911
IPV6NetworkMaskPrefixLength: 64
HardwareAddress: 0a:00:27:00:00:3b
MediumType: Ethernet
Wireless: No
Status: Up
VBoxNetworkName: HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter #2
Name: VirtualBox Host-Only Ethernet Adapter
GUID: 405a4779-a6f3-47d9-bf83-6a2503a093f2
DHCP: Disabled
IPAddress: 192.168.56.1
NetworkMask: 255.255.255.0
IPV6Address: fe80::ede5:3927:714c:3958
IPV6NetworkMaskPrefixLength: 64
HardwareAddress: 0a:00:27:00:00:06
MediumType: Ethernet
Wireless: No
Status: Up
VBoxNetworkName: HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter
and reference the IP address for the HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter #2
interface.
An example ansible task approaching what I'm looking for:
#!/usr/bin/env ansible-playbook
- hosts: localhost
become: false
gather_facts: false
vars_files:
- vars/main.yml
tasks:
- name: List all host-only interfaces
shell: "VBoxManage list hostonlyifs
| <magic>"
changed_when: false
register: vbox_hostonlyifs
- name: Use information from the previous command somehow
shell: "VBoxManage hostonlyif ipconfig
'{{ vbox_hostonlyifs.stdout | <magic> }}'
--ip 10.0.10.0"
I'm thinking maybe a combination of sed/regex can do the trick.
I have a regex expression that will capture the keys and values from all the blocks, but not the blocks themselves.
(^[a-zA-Z0-9]+:)(?:[ \t]+)(.*)$