以下Python代码有效。我可以在Python Shell中执行它,没有语法错误。 Python3或Python2都很好。
def test():
x = {
'x': '1',
'y': '2',
'z': '3',
}
s = ' '.join(f'{key}={value}' for (key, value) in x.items())
return s
当我使用Ansible提供相同的代码时,Ansible报告此函数中的语法无效
TASK [lvm : Volume group] ******************************************************
[WARNING]: Skipping plugin (/home/ostraaten/git/tpelcm/ansible/roles/internal/b
ackup/filter_plugins/backup_filters.py) as it seems to be invalid: invalid
syntax (backup_filters.py, line 39)
^CAnsible failed to complete successfully. Any error output should be
visible above. Please fix these errors and try again.
违规行是
s = ' '.join(f'{key}={value}' for (key, value) in x.items())
当我删除该行时,不再有语法错误。为什么Ansible认为此语法无效?该代码应如何用Ansible的Python编写?
答案 0 :(得分:1)
f-strings当然不是Python2;它们是在Python 3.6中引入的。
尝试:s = ' '.join('%s=%s' % (key, value) for (key, value) in x.items())