ansible / ansible

Ansible is a radically simple IT automation platform that makes your applications and systems easier to deploy and maintain. Automate everything from code deployment to network configuration to cloud management, in a language that approaches plain English, using SSH, with no agents to install on remote systems. https://docs.ansible.com.
https://www.ansible.com/
GNU General Public License v3.0
62.67k stars 23.87k forks source link

Ansible URI Module --data-urlencode not supported? #25805

Closed jvelasquez closed 7 years ago

jvelasquez commented 7 years ago
ISSUE TYPE
COMPONENT NAME

URI

ANSIBLE VERSION
ansible --version
ansible 2.3.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)]
CONFIGURATION

none

OS / ENVIRONMENT
[root@ansible-tower vagrant]# cat /etc/*release*
CentOS release 6.6 (Final)
CentOS release 6.6 (Final)
CentOS release 6.6 (Final)
cpe:/o:centos:linux:6:GA
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

[root@node-01 vagrant]# curl -k -u admin:password -s -XPOST --data-urlencode "script=$(<jenkins_example.groovy)" http://localhost:8080/scriptText -vvvv
* About to connect() to localhost port 8080 (#0)
*   Trying ::1... connected
* Connected to localhost (::1) port 8080 (#0)
* Server auth using Basic with user 'admin'
> POST /scriptText HTTP/1.1
> Authorization: Basic YWRtaW46cGFzc3dvcmQ=
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: localhost:8080
> Accept: */*
> Content-Length: 191
> Content-Type: application/x-www-form-urlencoded
>
< HTTP/1.1 200 OK
< Date: Fri, 16 Jun 2017 16:28:20 GMT
< X-Content-Type-Options: nosniff
< Content-Type: text/plain;charset=utf-8
< Content-Length: 9
< Server: Jetty(9.4.z-SNAPSHOT)
<
& test &
* Connection #0 to host localhost left intact
* Closing connection #0

[root@node-01 vagrant]# curl -k -u admin:password -s -XPOST --data "script=$(<jenkins_example.groovy)" http://localhost:8080/scriptText -vvvv
* About to connect() to localhost port 8080 (#0)
*   Trying ::1... connected
* Connected to localhost (::1) port 8080 (#0)
* Server auth using Basic with user 'admin'
> POST /scriptText HTTP/1.1
> Authorization: Basic YWRtaW46cGFzc3dvcmQ=
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: localhost:8080
> Accept: */*
> Content-Length: 139
> Content-Type: application/x-www-form-urlencoded
>
< HTTP/1.1 200 OK
< Date: Fri, 16 Jun 2017 16:29:24 GMT
< X-Content-Type-Options: nosniff
< Content-Type: text/plain;charset=utf-8
< Content-Length: 8147
< Server: Jetty(9.4.z-SNAPSHOT)
<
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 6: unexpected char: ''' @ line 6, column 21.
   def test_variable = '
                       ^

1 error

    at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
    at org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:150)
    at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:120)
    at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:132)
    at org.codehaus.groovy.control.SourceUnit.addError(SourceUnit.java:350)
    at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:139)
    at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:110)
    at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:234)
    at org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:168)
    at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:943)
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:605)
    at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
    at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
    at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
    at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:584)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:623)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:594)
    at hudson.util.RemotingDiagnostics$Script.call(RemotingDiagnostics.java:142)
    at hudson.util.RemotingDiagnostics$Script.call(RemotingDiagnostics.java:114)
    at hudson.remoting.LocalChannel.call(LocalChannel.java:45)
    at hudson.util.RemotingDiagnostics.executeGroovy(RemotingDiagnostics.java:111)
    at jenkins.model.Jenkins._doScript(Jenkins.java:4356)
    at jenkins.model.Jenkins.doScriptText(Jenkins.java:4334)
    at java.lang.invoke.MethodHandle.invokeWithArguments(MethodHandle.java:627)
    at org.kohsuke.stapler.Function$MethodFunction.invoke(Function.java:343)
    at org.kohsuke.stapler.Function.bindAndInvoke(Function.java:184)
    at org.kohsuke.stapler.Function.bindAndInvokeAndServeResponse(Function.java:117)
    at org.kohsuke.stapler.MetaClass$1.doDispatch(MetaClass.java:129)
    at org.kohsuke.stapler.NameBasedDispatcher.dispatch(NameBasedDispatcher.java:58)
    at org.kohsuke.stapler.Stapler.tryInvoke(Stapler.java:715)
    at org.kohsuke.stapler.Stapler.invoke(Stapler.java:845)
    at org.kohsuke.stapler.Stapler.invoke(Stapler.java:649)
    at org.kohsuke.stapler.Stapler.service(Stapler.java:238)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:841)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1650)
    at hudson.util.PluginServletFilter$1.doFilter(PluginServletFilter.java:135)
    at hudson.util.PluginServletFilter.doFilter(PluginServletFilter.java:138)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
    at hudson.security.csrf.CrumbFilter.doFilter(CrumbFilter.java:49)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
    at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:84)
    at hudson.security.UnwrapSecurityExceptionFilter.doFilter(UnwrapSecurityExceptionFilter.java:51)
    at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
    at jenkins.security.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:117)
    at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
    at org.acegisecurity.providers.anonymous.AnonymousProcessingFilter.doFilter(AnonymousProcessingFilter.java:125)
    at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
    at org.acegisecurity.ui.rememberme.RememberMeProcessingFilter.doFilter(RememberMeProcessingFilter.java:142)
    at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
    at org.acegisecurity.ui.AbstractProcessingFilter.doFilter(AbstractProcessingFilter.java:271)
    at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
    at jenkins.security.BasicHeaderProcessor.success(BasicHeaderProcessor.java:139)
    at jenkins.security.BasicHeaderProcessor.doFilter(BasicHeaderProcessor.java:81)
    at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
    at org.acegisecurity.context.HttpSessionContextIntegrationFilter.doFilter(HttpSessionContextIntegrationFilter.java:249)
    at hudson.security.HttpSessionContextIntegrationFilter2.doFilter(HttpSessionContextIntegrationFilter2.java:67)
    at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
    at hudson.security.ChainedServletFilter.doFilter(ChainedServletFilter.java:90)
    at hudson.security.HudsonFilter.doFilter(HudsonFilter.java:171)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
    at org.kohsuke.stapler.compression.CompressionFilter.doFilter(CompressionFilter.java:49)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
    at hudson.util.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:82)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
    at org.kohsuke.stapler.DiagnosticThreadNameFilter.doFilter(DiagnosticThreadNameFilter.java:30)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:533)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:524)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
    at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:190)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1595)
    at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:188)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1253)
    at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:168)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:473)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1564)
    at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:166)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1155)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
    at org.eclipse.jetty.server.Server.handle(Server.java:564)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:317)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:110)
    at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
    at org.eclipse.jetty.util.thread.Invocable.invokePreferred(Invocable.java:128)
    at org.eclipse.jetty.util.thread.Invocable$InvocableExecutor.invoke(Invocable.java:222)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:294)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:199)
    at winstone.BoundedExecutorService$1.run(BoundedExecutorService.java:77)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:748)
* Connection #0 to host localhost left intact
* Closing connection #0

The actual referenced script is the following:

import jenkins.*
import jenkins.model.*
import hudson.*
import hudson.model.*

def test_variable = '& test &'

println test_variable
STEPS TO REPRODUCE
  1. Have a jenkins instance running on localhost for example.
  2. Either disable security in jenkins or use the user/password in the correspondent fields
  3. Disable CSRF in jenkins (localhost:/configureSecurity --> CSRF Protection
  4. Specify the following variables:
    1. jenkins_master_url: typically will default to http://localhost:8080
    2. jenkins_master_api_user: e.g. admin
    3. jenkins_master_api_token: Either the user token or the password
  5. Create the template jenkins_example.groovy.j2 under roles/templates/groovy folder with the contents specified in the previous section.
  6. In the tasks/main.yml paste the code below:
- name: "Test script into Jenkins"
  uri:
    url: "{{ jenkins_master_url }}/scriptText"
    method: POST
    validate_certs: no
    user: "{{ jenkins_master_api_user }}"
    password: "{{ jenkins_master_api_token }}"
    force_basic_auth: yes
    headers:
      Content-Type: "application/x-www-form-urlencoded"
    body: "script={{ lookup('template', 'groovy/jenkins_example.groovy.j2') }}"
EXPECTED RESULTS

I expected to be able to specify the "data-urlencode" or something like that in the body / headers.

ACTUAL RESULTS
TASK [custom : Test script into Jenkins] ***************************************
task path: /var/lib/awx/projects/ansible-tower-vars-test-project/roles/custom/tasks/main.yml:45
Using module file /usr/lib/python2.6/site-packages/ansible/modules/network/basics/uri.py
<node-01.local> ESTABLISH SSH CONNECTION FOR USER: vagrant
<node-01.local> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=vagrant -o ConnectTimeout=10 -o ControlPath=/tmp/ansible_tower_wXYD7h/cp/ansible-ssh-%h-%p-%r node-01.local '/bin/sh -c '"'"'echo ~ && sleep 0'"'"''
<node-01.local> (0, '/home/vagrant\n', '')
<node-01.local> ESTABLISH SSH CONNECTION FOR USER: vagrant
<node-01.local> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=vagrant -o ConnectTimeout=10 -o ControlPath=/tmp/ansible_tower_wXYD7h/cp/ansible-ssh-%h-%p-%r node-01.local '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo /home/vagrant/.ansible/tmp/ansible-tmp-1497631152.86-186582555068734 `" && echo ansible-tmp-1497631152.86-186582555068734="` echo /home/vagrant/.ansible/tmp/ansible-tmp-1497631152.86-186582555068734 `" ) && sleep 0'"'"''
<node-01.local> (0, 'ansible-tmp-1497631152.86-186582555068734=/home/vagrant/.ansible/tmp/ansible-tmp-1497631152.86-186582555068734\n', '')
<node-01.local> PUT /tmp/tmpDWbDgw TO /home/vagrant/.ansible/tmp/ansible-tmp-1497631152.86-186582555068734/uri.py
<node-01.local> SSH: EXEC sftp -b - -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=vagrant -o ConnectTimeout=10 -o ControlPath=/tmp/ansible_tower_wXYD7h/cp/ansible-ssh-%h-%p-%r '[node-01.local]'
<node-01.local> (0, 'sftp> put /tmp/tmpDWbDgw /home/vagrant/.ansible/tmp/ansible-tmp-1497631152.86-186582555068734/uri.py\nUploading /tmp/tmpDWbDgw to /home/vagrant/.ansible/tmp/ansible-tmp-1497631152.86-186582555068734/uri.py\n', '')
<node-01.local> ESTABLISH SSH CONNECTION FOR USER: vagrant
<node-01.local> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=vagrant -o ConnectTimeout=10 -o ControlPath=/tmp/ansible_tower_wXYD7h/cp/ansible-ssh-%h-%p-%r node-01.local '/bin/sh -c '"'"'chmod u+x /home/vagrant/.ansible/tmp/ansible-tmp-1497631152.86-186582555068734/ /home/vagrant/.ansible/tmp/ansible-tmp-1497631152.86-186582555068734/uri.py && sleep 0'"'"''
<node-01.local> (0, '', '')
<node-01.local> ESTABLISH SSH CONNECTION FOR USER: vagrant
<node-01.local> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=vagrant -o ConnectTimeout=10 -o ControlPath=/tmp/ansible_tower_wXYD7h/cp/ansible-ssh-%h-%p-%r -tt node-01.local '/bin/sh -c '"'"'/usr/bin/python /home/vagrant/.ansible/tmp/ansible-tmp-1497631152.86-186582555068734/uri.py; rm -rf "/home/vagrant/.ansible/tmp/ansible-tmp-1497631152.86-186582555068734/" > /dev/null 2>&1 && sleep 0'"'"''
<node-01.local> (0, '\r\n{"status": 200, "content_length": "8147", "date": "Fri, 16 Jun 2017 16:39:13 GMT", "url": "http://node-01.local:8080/scriptText", "changed": false, "x_content_type_options": "nosniff", "server": "Jetty(9.4.z-SNAPSHOT)", "content": "org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:\\nScript1.groovy: 6: unexpected char: \'\'\' @ line 6, column 21.\\n   def test_variable = \'\\n                       ^\\n\\n1 error\\n\\n\\tat org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)\\n\\tat org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:150)\\n\\tat org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:120)\\n\\tat org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:132)\\n\\tat org.codehaus.groovy.control.SourceUnit.addError(SourceUnit.java:350)\\n\\tat org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:139)\\n\\tat org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:110)\\n\\tat org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:234)\\n\\tat org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:168)\\n\\tat org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:943)\\n\\tat org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:605)\\n\\tat org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)\\n\\tat org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)\\n\\tat groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)\\n\\tat groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)\\n\\tat groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)\\n\\tat groovy.lang.GroovyShell.parse(GroovyShell.java:700)\\n\\tat groovy.lang.GroovyShell.evaluate(GroovyShell.java:584)\\n\\tat groovy.lang.GroovyShell.evaluate(GroovyShell.java:623)\\n\\tat groovy.lang.GroovyShell.evaluate(GroovyShell.java:594)\\n\\tat hudson.util.RemotingDiagnostics$Script.call(RemotingDiagnostics.java:142)\\n\\tat hudson.util.RemotingDiagnostics$Script.call(RemotingDiagnostics.java:114)\\n\\tat hudson.remoting.LocalChannel.call(LocalChannel.java:45)\\n\\tat hudson.util.RemotingDiagnostics.executeGroovy(RemotingDiagnostics.java:111)\\n\\tat jenkins.model.Jenkins._doScript(Jenkins.java:4356)\\n\\tat jenkins.model.Jenkins.doScriptText(Jenkins.java:4334)\\n\\tat java.lang.invoke.MethodHandle.invokeWithArguments(MethodHandle.java:627)\\n\\tat org.kohsuke.stapler.Function$MethodFunction.invoke(Function.java:343)\\n\\tat org.kohsuke.stapler.Function.bindAndInvoke(Function.java:184)\\n\\tat org.kohsuke.stapler.Function.bindAndInvokeAndServeResponse(Function.java:117)\\n\\tat org.kohsuke.stapler.MetaClass$1.doDispatch(MetaClass.java:129)\\n\\tat org.kohsuke.stapler.NameBasedDispatcher.dispatch(NameBasedDispatcher.java:58)\\n\\tat org.kohsuke.stapler.Stapler.tryInvoke(Stapler.java:715)\\n\\tat org.kohsuke.stapler.Stapler.invoke(Stapler.java:845)\\n\\tat org.kohsuke.stapler.Stapler.invoke(Stapler.java:649)\\n\\tat org.kohsuke.stapler.Stapler.service(Stapler.java:238)\\n\\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:790)\\n\\tat org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:841)\\n\\tat org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1650)\\n\\tat hudson.util.PluginServletFilter$1.doFilter(PluginServletFilter.java:135)\\n\\tat hudson.util.PluginServletFilter.doFilter(PluginServletFilter.java:138)\\n\\tat org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)\\n\\tat hudson.security.csrf.CrumbFilter.doFilter(CrumbFilter.java:49)\\n\\tat org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)\\n\\tat hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:84)\\n\\tat hudson.security.UnwrapSecurityExceptionFilter.doFilter(UnwrapSecurityExceptionFilter.java:51)\\n\\tat hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)\\n\\tat jenkins.security.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:117)\\n\\tat hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)\\n\\tat org.acegisecurity.providers.anonymous.AnonymousProcessingFilter.doFilter(AnonymousProcessingFilter.java:125)\\n\\tat hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)\\n\\tat org.acegisecurity.ui.rememberme.RememberMeProcessingFilter.doFilter(RememberMeProcessingFilter.java:142)\\n\\tat hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)\\n\\tat org.acegisecurity.ui.AbstractProcessingFilter.doFilter(AbstractProcessingFilter.java:271)\\n\\tat hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)\\n\\tat jenkins.security.BasicHeaderProcessor.success(BasicHeaderProcessor.java:139)\\n\\tat jenkins.security.BasicHeaderProcessor.doFilter(BasicHeaderProcessor.java:81)\\n\\tat hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)\\n\\tat org.acegisecurity.context.HttpSessionContextIntegrationFilter.doFilter(HttpSessionContextIntegrationFilter.java:249)\\n\\tat hudson.security.HttpSessionContextIntegrationFilter2.doFilter(HttpSessionContextIntegrationFilter2.java:67)\\n\\tat hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)\\n\\tat hudson.security.ChainedServletFilter.doFilter(ChainedServletFilter.java:90)\\n\\tat hudson.security.HudsonFilter.doFilter(HudsonFilter.java:171)\\n\\tat org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)\\n\\tat org.kohsuke.stapler.compression.CompressionFilter.doFilter(CompressionFilter.java:49)\\n\\tat org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)\\n\\tat hudson.util.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:82)\\n\\tat org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)\\n\\tat org.kohsuke.stapler.DiagnosticThreadNameFilter.doFilter(DiagnosticThreadNameFilter.java:30)\\n\\tat org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)\\n\\tat org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:533)\\n\\tat org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)\\n\\tat org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:524)\\n\\tat org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)\\n\\tat org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:190)\\n\\tat org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1595)\\n\\tat org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:188)\\n\\tat org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1253)\\n\\tat org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:168)\\n\\tat org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:473)\\n\\tat org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1564)\\n\\tat org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:166)\\n\\tat org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1155)\\n\\tat org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)\\n\\tat org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)\\n\\tat org.eclipse.jetty.server.Server.handle(Server.java:564)\\n\\tat org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:317)\\n\\tat org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251)\\n\\tat org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)\\n\\tat org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:110)\\n\\tat org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)\\n\\tat org.eclipse.jetty.util.thread.Invocable.invokePreferred(Invocable.java:128)\\n\\tat org.eclipse.jetty.util.thread.Invocable$InvocableExecutor.invoke(Invocable.java:222)\\n\\tat org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:294)\\n\\tat org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:199)\\n\\tat winstone.BoundedExecutorService$1.run(BoundedExecutorService.java:77)\\n\\tat java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)\\n\\tat java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)\\n\\tat java.lang.Thread.run(Thread.java:748)\\n", "invocation": {"module_args": {"directory_mode": null, "force": false, "remote_src": null, "follow_redirects": "safe", "body_format": "raw", "owner": null, "follow": false, "group": null, "use_proxy": true, "unsafe_writes": null, "serole": null, "content": null, "setype": null, "status_code": [200], "return_content": true, "method": "POST", "body": "script=import jenkins.*\\nimport jenkins.model.*\\nimport hudson.*\\nimport hudson.model.*\\n\\ndef test_variable = \'& test &\'\\n\\nprintln test_variable\\n", "url_username": "admin", "url_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "dest": null, "selevel": null, "force_basic_auth": true, "removes": null, "http_agent": "ansible-httpget", "user": "admin", "regexp": null, "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "src": null, "url": "http://node-01.local:8080/scriptText", "validate_certs": false, "seuser": null, "creates": null, "headers": {"Content-Type": "application/x-www-form-urlencoded", "Authorization": "Basic YWRtaW46YjNmZDQzNjhiZmIzNWQ4M2IwMGFlNDUyNmQwZWVmNWQ="}, "delimiter": null, "mode": null, "timeout": 30, "attributes": null, "backup": null}}, "connection": "close", "content_type": "text/plain;charset=utf-8", "msg": "OK (8147 bytes)", "redirected": false}\r\n', 'Shared connection to node-01.local closed.\r\n')
ok: [node-01.local] => {
    "changed": false, 
    "connection": "close", 
    "content": "org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:\nScript1.groovy: 6: unexpected char: ''' @ line 6, column 21.\n   def test_variable = '\n                       ^\n\n1 error\n\n\tat org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)\n\tat org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:150)\n\tat org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:120)\n\tat org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:132)\n\tat org.codehaus.groovy.control.SourceUnit.addError(SourceUnit.java:350)\n\tat org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:139)\n\tat org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:110)\n\tat org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:234)\n\tat org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:168)\n\tat org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:943)\n\tat org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:605)\n\tat org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)\n\tat org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)\n\tat groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)\n\tat groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)\n\tat groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)\n\tat groovy.lang.GroovyShell.parse(GroovyShell.java:700)\n\tat groovy.lang.GroovyShell.evaluate(GroovyShell.java:584)\n\tat groovy.lang.GroovyShell.evaluate(GroovyShell.java:623)\n\tat groovy.lang.GroovyShell.evaluate(GroovyShell.java:594)\n\tat hudson.util.RemotingDiagnostics$Script.call(RemotingDiagnostics.java:142)\n\tat hudson.util.RemotingDiagnostics$Script.call(RemotingDiagnostics.java:114)\n\tat hudson.remoting.LocalChannel.call(LocalChannel.java:45)\n\tat hudson.util.RemotingDiagnostics.executeGroovy(RemotingDiagnostics.java:111)\n\tat jenkins.model.Jenkins._doScript(Jenkins.java:4356)\n\tat jenkins.model.Jenkins.doScriptText(Jenkins.java:4334)\n\tat java.lang.invoke.MethodHandle.invokeWithArguments(MethodHandle.java:627)\n\tat org.kohsuke.stapler.Function$MethodFunction.invoke(Function.java:343)\n\tat org.kohsuke.stapler.Function.bindAndInvoke(Function.java:184)\n\tat org.kohsuke.stapler.Function.bindAndInvokeAndServeResponse(Function.java:117)\n\tat org.kohsuke.stapler.MetaClass$1.doDispatch(MetaClass.java:129)\n\tat org.kohsuke.stapler.NameBasedDispatcher.dispatch(NameBasedDispatcher.java:58)\n\tat org.kohsuke.stapler.Stapler.tryInvoke(Stapler.java:715)\n\tat org.kohsuke.stapler.Stapler.invoke(Stapler.java:845)\n\tat org.kohsuke.stapler.Stapler.invoke(Stapler.java:649)\n\tat org.kohsuke.stapler.Stapler.service(Stapler.java:238)\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:790)\n\tat org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:841)\n\tat org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1650)\n\tat hudson.util.PluginServletFilter$1.doFilter(PluginServletFilter.java:135)\n\tat hudson.util.PluginServletFilter.doFilter(PluginServletFilter.java:138)\n\tat org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)\n\tat hudson.security.csrf.CrumbFilter.doFilter(CrumbFilter.java:49)\n\tat org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)\n\tat hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:84)\n\tat hudson.security.UnwrapSecurityExceptionFilter.doFilter(UnwrapSecurityExceptionFilter.java:51)\n\tat hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)\n\tat jenkins.security.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:117)\n\tat hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)\n\tat org.acegisecurity.providers.anonymous.AnonymousProcessingFilter.doFilter(AnonymousProcessingFilter.java:125)\n\tat hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)\n\tat org.acegisecurity.ui.rememberme.RememberMeProcessingFilter.doFilter(RememberMeProcessingFilter.java:142)\n\tat hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)\n\tat org.acegisecurity.ui.AbstractProcessingFilter.doFilter(AbstractProcessingFilter.java:271)\n\tat hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)\n\tat jenkins.security.BasicHeaderProcessor.success(BasicHeaderProcessor.java:139)\n\tat jenkins.security.BasicHeaderProcessor.doFilter(BasicHeaderProcessor.java:81)\n\tat hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)\n\tat org.acegisecurity.context.HttpSessionContextIntegrationFilter.doFilter(HttpSessionContextIntegrationFilter.java:249)\n\tat hudson.security.HttpSessionContextIntegrationFilter2.doFilter(HttpSessionContextIntegrationFilter2.java:67)\n\tat hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)\n\tat hudson.security.ChainedServletFilter.doFilter(ChainedServletFilter.java:90)\n\tat hudson.security.HudsonFilter.doFilter(HudsonFilter.java:171)\n\tat org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)\n\tat org.kohsuke.stapler.compression.CompressionFilter.doFilter(CompressionFilter.java:49)\n\tat org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)\n\tat hudson.util.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:82)\n\tat org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)\n\tat org.kohsuke.stapler.DiagnosticThreadNameFilter.doFilter(DiagnosticThreadNameFilter.java:30)\n\tat org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)\n\tat org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:533)\n\tat org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)\n\tat org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:524)\n\tat org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)\n\tat org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:190)\n\tat org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1595)\n\tat org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:188)\n\tat org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1253)\n\tat org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:168)\n\tat org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:473)\n\tat org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1564)\n\tat org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:166)\n\tat org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1155)\n\tat org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)\n\tat org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)\n\tat org.eclipse.jetty.server.Server.handle(Server.java:564)\n\tat org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:317)\n\tat org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251)\n\tat org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)\n\tat org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:110)\n\tat org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)\n\tat org.eclipse.jetty.util.thread.Invocable.invokePreferred(Invocable.java:128)\n\tat org.eclipse.jetty.util.thread.Invocable$InvocableExecutor.invoke(Invocable.java:222)\n\tat org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:294)\n\tat org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:199)\n\tat winstone.BoundedExecutorService$1.run(BoundedExecutorService.java:77)\n\tat java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)\n\tat java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)\n\tat java.lang.Thread.run(Thread.java:748)\n", 
    "content_length": "8147", 
    "content_type": "text/plain;charset=utf-8", 
    "date": "Fri, 16 Jun 2017 16:39:13 GMT", 
    "invocation": {
        "module_args": {
            "attributes": null, 
            "backup": null, 
            "body": "script=import jenkins.*\nimport jenkins.model.*\nimport hudson.*\nimport hudson.model.*\n\ndef test_variable = '& test &'\n\nprintln test_variable\n", 
            "body_format": "raw", 
            "content": null, 
            "creates": null, 
            "delimiter": null, 
            "dest": null, 
            "directory_mode": null, 
            "follow": false, 
            "follow_redirects": "safe", 
            "force": false, 
            "force_basic_auth": true, 
            "group": null, 
            "headers": {
                "Authorization": "Basic YWRtaW46YjNmZDQzNjhiZmIzNWQ4M2IwMGFlNDUyNmQwZWVmNWQ=", 
                "Content-Type": "application/x-www-form-urlencoded"
            }, 
            "http_agent": "ansible-httpget", 
            "method": "POST", 
            "mode": null, 
            "owner": null, 
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
            "regexp": null, 
            "remote_src": null, 
            "removes": null, 
            "return_content": true, 
            "selevel": null, 
            "serole": null, 
            "setype": null, 
            "seuser": null, 
            "src": null, 
            "status_code": [
                200
            ], 
            "timeout": 30, 
            "unsafe_writes": null, 
            "url": "http://node-01.local:8080/scriptText", 
            "url_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
            "url_username": "admin", 
            "use_proxy": true, 
            "user": "admin", 
            "validate_certs": false
        }
    }, 
    "msg": "OK (8147 bytes)", 
    "redirected": false, 
    "server": "Jetty(9.4.z-SNAPSHOT)", 
    "status": 200, 
    "url": "http://node-01.local:8080/scriptText", 
    "x_content_type_options": "nosniff"
}
META: ran handlers
META: ran handlers
sivel commented 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:

jvelasquez commented 7 years ago

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"
}