shlex.split() is not giving the proper output on the input string.
In python interpreter, storing the input value in a variable produces the expected output.
But if i execute via a script, shlex.split()
output is incorrect and input string did not split on whitespace.
>>> import shlex
>>> var = "/usr/bin/ansible-playbook --timeout=60 --module-path /var/sandeep> /playbooks/ --extra-vars '{ \"text\": \"DUMMY\", \"addition\": [\"1\", \"2\", \"3\", ], \"deletion\": [], \"update\": \"update\", \"path\": \"/var/sandeep\", }' /tmp/sandeep//tmp/example.yaml"
>>>
>>>
>>> shlex.split(var)
['/usr/bin/ansible-playbook', '--timeout=60', '--module-path', '/var/sandeep/playbooks/', '--extra-vars', '{ "text": "DUMMY", "addition": ["1", "2", "3", ], "deletion": [], "update": "update", "path": "/var/sandeep", }', '/tmp/sandeep//tmp/example.yaml']
def create_extra(text, extra_dict):
extra = "'{{ \\\"text\\\": \\\"{}\\\", ".format(text)
for key, value in extra_dict.items():
if isinstance(value, list):
extra += '\\\"{}\\\": ['.format(key)
for item in value:
extra += '\\\"{}\\\", '.format(item)
extra += '], '
elif isinstance(value, dict):
extra += '\\\"{}\\\": {{'.format(key)
for item_key, item_value in value.items():
extra += '\\\"{}\\\": \\\"{}\\\", '.format(item_key, item_value)
extra += "}, "
else:
extra += '\\\"{}\\\": \\\"{}\\\", '.format(key, value)
extra += "}'"
#print("extra: %s" % extra)
return extra
extra_dict = {'addition': ["1", "2", "3"],
'deletion': [],
'update': 'update',
'path' : '/var/sandeep'
}
temp = create_extra("DUMMY", extra_dict)
"""create_extra function formats and return string"""
cmd = ('"/usr/bin/ansible-playbook ' +
'--timeout=60 ' +
'--module-path /var/sandeep/playbooks/ ' +
'--extra-vars {} {}/{}"'.format(temp, "/tmp/sandeep", "/tmp/example.yaml"))
print(cmd)
print(shlex.split(cmd))
output of print(cmd)
"/usr/bin/ansible-playbook --timeout=60 --module-path /var/sandeep/playbooks/ --extra-vars '{ \"text\": \"DUMMY\", \"addition\": [\"1\", \"2\", \"3\", ], \"deletion\": [], \"update\": \"update\", \"path\": \"/var/sandeep\", }' /tmp/sandeep//tmp/example.yaml"
Expected results:
['/usr/bin/ansible-playbook', '--timeout=60', '--module-path', '/var/sandeep/playbooks/', '--extra-vars', '{ "text": "DUMMY", "addition": ["1", "2", "3", ], "deletion": [], "update": "update", "path": "/var/sandeep", }', '/tmp/sandeep//tmp/example.yaml']
Actual Results:
['/usr/bin/ansible-playbook --timeout=60 --module-path /var/sandeep/playbooks/ --extra-vars \'{ "text": "DUMMY", "addition": ["1", "2", "3", ], "deletion": [], "update": "update", "path": "/var/sandeep", }\' /tmp/sandeep//tmp/example.yaml']
Am i missing something here?