ansible template parsing workaround

Whilst trying to template a python script with ansible 2.4 I’ve got a weird error:

fatal: [HOSTNAME]: FAILED! => {
"changed": false,
"failed": true,
"msg": "AnsibleError: template error while templating string: Missing end of comment tag. String: #!/usr/bin/python\nimport os\nimport json\n\nif __name__ == \"__main__\":\n # Iterate over all block devices, but ignore them if they are in the skippable set\n skippable = (\"sr\", \"loop\", \"ram\")\n devices = (device for device in os.listdir(\"/sys/class/block\")\n if not any(ignore in device for ignore in skippable))\n data = [{\"{#DEVICENAME}\": device} for device in devices]\n print(json.dumps({\"data\": data}, indent=4))\n"
}

Turns out it tries to somehow parse it. We can workaround it by wrapping the contents of file with either %raw% escape string (easier) or unsafe one.

Leave a Reply

Your email address will not be published. Required fields are marked *