Skip to content

Commit 6907ae5

Browse files
[PR #9539/94d5256a backport][stable-10] Fix #9538 Inventory iocage fails when DHCP is enabled (#9568)
Fix #9538 Inventory iocage fails when DHCP is enabled (#9539) * Fix #9538 Inventory iocage fails when DHCP is enbled. * Add changelog fragment 9539-iocage-inventory-dhcp.yml * Keep iocage_ip4 a string. * Rename the variable iocage_ip4 to iocage_ip4_dict in _parse_ip4. * Update the changelog fragment. * Rename _parse_ip4 parameter ip4_addr to ip4. * Fix changelog frangment present tense. * If IP is not available set iocage_ip4='-' instead of the empty string. * Update changelogs/fragments/9539-iocage-inventory-dhcp.yml Co-authored-by: Felix Fontein <[email protected]> --------- Co-authored-by: Felix Fontein <[email protected]> (cherry picked from commit 94d5256) Co-authored-by: Vladimir Botka <[email protected]>
1 parent 2314db5 commit 6907ae5

28 files changed

+243
-17
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bugfixes:
2+
- iocage inventory plugin - the plugin parses the IP4 tab of the jails list and put the elements into the new variable ``iocage_ip4_dict``. In multiple interface format the variable ``iocage_ip4`` keeps the comma-separated list of IP4 (https://github.com/ansible-collections/community.general/issues/9538).

plugins/inventory/iocage.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,27 @@
131131

132132

133133
def _parse_ip4(ip4):
134-
if ip4 == '-':
135-
return ip4
136-
return re.split('\\||/', ip4)[1]
134+
''' Return dictionary iocage_ip4_dict. default = {ip4: [], msg: ''}.
135+
If item matches ifc|IP or ifc|CIDR parse ifc, ip, and mask.
136+
Otherwise, append item to msg.
137+
'''
138+
139+
iocage_ip4_dict = {}
140+
iocage_ip4_dict['ip4'] = []
141+
iocage_ip4_dict['msg'] = ''
142+
143+
items = ip4.split(',')
144+
for item in items:
145+
if re.match('^\\w+\\|(?:\\d{1,3}\\.){3}\\d{1,3}.*$', item):
146+
i = re.split('\\||/', item)
147+
if len(i) == 3:
148+
iocage_ip4_dict['ip4'].append({'ifc': i[0], 'ip': i[1], 'mask': i[2]})
149+
else:
150+
iocage_ip4_dict['ip4'].append({'ifc': i[0], 'ip': i[1], 'mask': '-'})
151+
else:
152+
iocage_ip4_dict['msg'] += item
153+
154+
return iocage_ip4_dict
137155

138156

139157
class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
@@ -194,7 +212,6 @@ def get_inventory(self, path):
194212

195213
cmd_list = cmd.copy()
196214
cmd_list.append('list')
197-
cmd_list.append('--header')
198215
cmd_list.append('--long')
199216
try:
200217
p = Popen(cmd_list, stdout=PIPE, stderr=PIPE, env=my_env)
@@ -239,16 +256,26 @@ def get_inventory(self, path):
239256
return results
240257

241258
def get_jails(self, t_stdout, results):
242-
jails = [x.split() for x in t_stdout.splitlines()]
243-
for jail in jails:
259+
lines = t_stdout.splitlines()
260+
if len(lines) < 5:
261+
return
262+
indices = [i for i, val in enumerate(lines[1]) if val == '|']
263+
for line in lines[3::2]:
264+
jail = [line[i + 1:j].strip() for i, j in zip(indices[:-1], indices[1:])]
244265
iocage_name = jail[1]
266+
iocage_ip4_dict = _parse_ip4(jail[6])
267+
if iocage_ip4_dict['ip4']:
268+
iocage_ip4 = ','.join([d['ip'] for d in iocage_ip4_dict['ip4']])
269+
else:
270+
iocage_ip4 = '-'
245271
results['_meta']['hostvars'][iocage_name] = {}
246272
results['_meta']['hostvars'][iocage_name]['iocage_jid'] = jail[0]
247273
results['_meta']['hostvars'][iocage_name]['iocage_boot'] = jail[2]
248274
results['_meta']['hostvars'][iocage_name]['iocage_state'] = jail[3]
249275
results['_meta']['hostvars'][iocage_name]['iocage_type'] = jail[4]
250276
results['_meta']['hostvars'][iocage_name]['iocage_release'] = jail[5]
251-
results['_meta']['hostvars'][iocage_name]['iocage_ip4'] = _parse_ip4(jail[6])
277+
results['_meta']['hostvars'][iocage_name]['iocage_ip4_dict'] = iocage_ip4_dict
278+
results['_meta']['hostvars'][iocage_name]['iocage_ip4'] = iocage_ip4
252279
results['_meta']['hostvars'][iocage_name]['iocage_ip6'] = jail[7]
253280
results['_meta']['hostvars'][iocage_name]['iocage_template'] = jail[8]
254281
results['_meta']['hostvars'][iocage_name]['iocage_basejail'] = jail[9]

tests/unit/plugins/inventory/fixtures/iocage_inventory.yml renamed to tests/unit/plugins/inventory/fixtures/iocage/iocage_inventory.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ all:
55
test_101:
66
iocage_basejail: 'yes'
77
iocage_boot: 'off'
8+
iocage_ip4_dict:
9+
ip4:
10+
- ifc: vnet0
11+
ip: 10.1.0.101
12+
mask: '24'
13+
msg: ''
814
iocage_ip4: 10.1.0.101
915
iocage_ip6: '-'
1016
iocage_jid: '-'
@@ -157,6 +163,12 @@ all:
157163
test_102:
158164
iocage_basejail: 'yes'
159165
iocage_boot: 'off'
166+
iocage_ip4_dict:
167+
ip4:
168+
- ifc: vnet0
169+
ip: 10.1.0.102
170+
mask: '24'
171+
msg: ''
160172
iocage_ip4: 10.1.0.102
161173
iocage_ip6: '-'
162174
iocage_jid: '-'
@@ -309,6 +321,12 @@ all:
309321
test_103:
310322
iocage_basejail: 'yes'
311323
iocage_boot: 'off'
324+
iocage_ip4_dict:
325+
ip4:
326+
- ifc: vnet0
327+
ip: 10.1.0.103
328+
mask: '24'
329+
msg: ''
312330
iocage_ip4: 10.1.0.103
313331
iocage_ip6: '-'
314332
iocage_jid: '-'

tests/unit/plugins/inventory/fixtures/iocage_inventory.yml.license renamed to tests/unit/plugins/inventory/fixtures/iocage/iocage_inventory.yml.license

File renamed without changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
+------+----------+------+-------+------+-----------------+---------------------+-----+----------------+----------+
2+
| JID | NAME | BOOT | STATE | TYPE | RELEASE | IP4 | IP6 | TEMPLATE | BASEJAIL |
3+
+======+==========+======+=======+======+=================+=====================+=====+================+==========+
4+
| - | test_101 | off | down | jail | 13.4-RELEASE-p2 | vnet0|10.1.0.101/24 | - | ansible_client | yes |
5+
+------+----------------+------+-------+------+-----------------+--------------------+-----+----------------+-----+
6+
| - | test_102 | off | down | jail | 13.4-RELEASE-p2 | vnet0|10.1.0.102/24 | - | ansible_client | yes |
7+
+------+----------------+------+-------+------+-----------------+--------------------+-----+----------------+-----+
8+
| - | test_103 | off | down | jail | 13.4-RELEASE-p2 | vnet0|10.1.0.103/24 | - | ansible_client | yes |
9+
+------+----------------+------+-------+------+-----------------+--------------------+-----+----------------+-----+

tests/unit/plugins/inventory/fixtures/iocage_jails.txt.license renamed to tests/unit/plugins/inventory/fixtures/iocage/iocage_jails.txt.license

File renamed without changes.

tests/unit/plugins/inventory/fixtures/iocage_jails.yml renamed to tests/unit/plugins/inventory/fixtures/iocage/iocage_jails.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ _meta:
33
test_101:
44
iocage_basejail: 'yes'
55
iocage_boot: 'off'
6+
iocage_ip4_dict:
7+
ip4:
8+
- ifc: vnet0
9+
ip: 10.1.0.101
10+
mask: '24'
11+
msg: ''
612
iocage_ip4: 10.1.0.101
713
iocage_ip6: '-'
814
iocage_jid: '-'
@@ -13,6 +19,12 @@ _meta:
1319
test_102:
1420
iocage_basejail: 'yes'
1521
iocage_boot: 'off'
22+
iocage_ip4_dict:
23+
ip4:
24+
- ifc: vnet0
25+
ip: 10.1.0.102
26+
mask: '24'
27+
msg: ''
1628
iocage_ip4: 10.1.0.102
1729
iocage_ip6: '-'
1830
iocage_jid: '-'
@@ -23,6 +35,12 @@ _meta:
2335
test_103:
2436
iocage_basejail: 'yes'
2537
iocage_boot: 'off'
38+
iocage_ip4_dict:
39+
ip4:
40+
- ifc: vnet0
41+
ip: 10.1.0.103
42+
mask: '24'
43+
msg: ''
2644
iocage_ip4: 10.1.0.103
2745
iocage_ip6: '-'
2846
iocage_jid: '-'

tests/unit/plugins/inventory/fixtures/iocage_jails.yml.license renamed to tests/unit/plugins/inventory/fixtures/iocage/iocage_jails.yml.license

File renamed without changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
+------+----------------+------+-------+------+-----------------+--------------------+-----+----------------+----------+
2+
| JID | NAME | BOOT | STATE | TYPE | RELEASE | IP4 | IP6 | TEMPLATE | BASEJAIL |
3+
+======+================+======+=======+======+=================+====================+=====+================+==========+
4+
| 268 | test_111 | off | up | jail | 14.1-RELEASE-p6 | epair0b|10.1.0.174 | - | ansible_client | yes |
5+
+------+----------------+------+-------+------+-----------------+--------------------+-----+----------------+----------+
6+
| 269 | test_112 | off | up | jail | 14.1-RELEASE-p6 | epair0b|10.1.0.147 | - | ansible_client | yes |
7+
+------+----------------+------+-------+------+-----------------+--------------------+-----+----------------+----------+
8+
| 270 | test_113 | off | up | jail | 14.1-RELEASE-p6 | epair0b|10.1.0.231 | - | ansible_client | yes |
9+
+------+----------------+------+-------+------+-----------------+--------------------+-----+----------------+----------+

tests/unit/plugins/inventory/fixtures/iocage_properties.txt.license renamed to tests/unit/plugins/inventory/fixtures/iocage/iocage_jails_dhcp.txt.license

File renamed without changes.

0 commit comments

Comments
 (0)