Closed jvelasquez closed 7 years ago
This functionality is already supported via a jinja2 filter called urlencode
Should you need to ensure your data is URL encoded, use the urlencode
filter to do so.
http://jinja.pocoo.org/docs/dev/templates/#urlencode
If you have further questions please stop by IRC or the mailing list:
Thanks @sivel
Unfortunately I am getting the following error when using the urlencode
filter in jinja2
# defaults/main.yml
jenkins_test_variable: "& test &"
// templates/jenkins_example.groovy.j2
import jenkins.*
import jenkins.model.*
import hudson.*
import hudson.model.*
def test_variable = '{{ jenkins_test_variable | urlencode }}'
println test_variable
fatal: [node-01.local]: FAILED! => {
"failed": true,
"msg": "An unhandled exception occurred while running the lookup plugin 'template'. Error was a <class 'ansible.errors.AnsibleError'>, original message: template error while templating string: no filter named 'urlencode'. String: import jenkins.*\nimport jenkins.model.*\nimport hudson.*\nimport hudson.model.*\n\ndef test_variable = '{{ jenkins_test_variable | urlencode }}'\n\nprintln test_variable\n"
}
ISSUE TYPE
COMPONENT NAME
URI
ANSIBLE VERSION
CONFIGURATION
none
OS / ENVIRONMENT
SUMMARY
POSTing a groovy script containing
&
characters fails as it is needed to specify--data-urlencode
when sending the request. It looks like the ansible URI module defaults to--data
which will not work with these kind of files/scripts.Sample Outputs
The actual referenced script is the following:
STEPS TO REPRODUCE
jenkins_master_url
: typically will default tohttp://localhost:8080
jenkins_master_api_user
: e.g. adminjenkins_master_api_token
: Either the user token or the passwordjenkins_example.groovy.j2
underroles/templates/groovy
folder with the contents specified in the previous section.tasks/main.yml
paste the code below:EXPECTED RESULTS
I expected to be able to specify the "data-urlencode" or something like that in the body / headers.
ACTUAL RESULTS