1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Merge pull request #1776 from junegunn/inventory-group-var-ws

Allows whitespaces around assignment operators when defining group variables
This commit is contained in:
Michael DeHaan 2012-12-17 16:51:49 -08:00
commit 4bcdd24688
3 changed files with 17 additions and 3 deletions

View file

@ -24,6 +24,7 @@ from ansible.inventory.expand_hosts import detect_range
from ansible.inventory.expand_hosts import expand_hostname_range from ansible.inventory.expand_hosts import expand_hostname_range
from ansible import errors from ansible import errors
import shlex import shlex
import re
class InventoryParser(object): class InventoryParser(object):
""" """
@ -163,5 +164,10 @@ class InventoryParser(object):
if line.find("=") == -1: if line.find("=") == -1:
raise errors.AnsibleError("variables assigned to group must be in key=value form") raise errors.AnsibleError("variables assigned to group must be in key=value form")
else: else:
(k,v) = line.split("=",1) (k, v) = [e.strip() for e in line.split("=", 1)]
group.set_variable(k,v) # When the value is a single-quoted or double-quoted string
if re.match(r"^(['\"]).*\1$", v):
# Unquote the string
group.set_variable(k, re.sub(r"^['\"]|['\"]$", '', v))
else:
group.set_variable(k, v)

View file

@ -142,7 +142,9 @@ class TestInventory(unittest.TestCase):
print vars print vars
expected = dict( expected = dict(
a='1', b='2', c='3', d='10002', rga='1', rgb='2', rgc='3', a='1', b='2', c='3', d='10002', e='10003', f='10004 != 10005',
g=' g ', h=' h ', i="' i \"", j='" j',
rga='1', rgb='2', rgc='3',
inventory_hostname='rtp_a', inventory_hostname_short='rtp_a', inventory_hostname='rtp_a', inventory_hostname_short='rtp_a',
group_names=[ 'eastcoast', 'nc', 'redundantgroup', 'redundantgroup2', 'redundantgroup3', 'rtp', 'us' ] group_names=[ 'eastcoast', 'nc', 'redundantgroup', 'redundantgroup2', 'redundantgroup3', 'rtp', 'us' ]
) )

View file

@ -34,6 +34,12 @@ rgc=3
b=10000 b=10000
c=10001 c=10001
d=10002 d=10002
e = 10003
f = 10004 != 10005
g = " g "
h = ' h '
i = ' i "
j = " j
[rtp] [rtp]
rtp_a rtp_a