shlex.split((没有在输入字符串上给出适当的输出。
在Python解释器中,将输入值存储在变量中会产生预期的输出。
但是,如果我通过脚本执行执行,则shlex.split()
输出不正确,并且输入字符串在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']
我在这里错过了什么吗?
shlex
输出是完全正确的,因为字符串中包含的字面"
字符。
cmd = ('"/usr/bin/ansible-playbook ' +
# ^- that right there
'--timeout=60 ' +
'--module-path /var/sandeep/playbooks/ ' +
'--extra-vars {} {}/{}"'.format(temp, "/tmp/sandeep", "/tmp/example.yaml"))
# and this right here -^
正如您的print(cmd)
所显示的:
"/usr/bin/ansible-playbook --timeout=60 --module-path /var/sandeep/playbooks/ --extra-vars whatever /tmp/sandeep//tmp/example.yaml"
...您的字符串以"
开头,并以"
结尾,这使其成为单个字符串,当由外壳解析时。
只是将这些角色拿出来,问题不再发生:
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))
但是,您有其他严重的错误,因为字符串串联本质上不适合构建命令行。而不是直接构建一个数组,而不是尝试采用该方法:
cmd = ['/usr/bin/ansible-playbook',
'--timeout=60',
'--module-path', '/var/sandeep/playbooks/',
'--extra-vars', temp, os.path.join('/tmp/sandeep', '/tmp/example.yml')]
...然后 temp
的值或其他带有空格或文字报价的变量将不再打破您的代码或允许注入任意参数。