如何使用 ansible 将 urlencoded 字符串传递给 jenkins api?

如何使用 ansible 将 urlencoded 字符串传递给 jenkins api?

我必须将值传递给jenkinsapi。但是 jenkins api 接受 中的值urlencode。我尝试urlencode在 ansible 中使用过滤器,但它给出了错误。

- name: Add credential to add node
  uri:
      body: |
          json={
              "": "0",
              "credentials": {
                "scope": "GLOBAL",
                "id": "jenkins_linux_slave2_auth",
                "username": "jenkins",
                "password": "",
                "privateKeySource": {
                  "stapler-class": "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$DirectEntryPrivateKeySource",
                  "privateKey": '{{"URL ENCODED STRING"|urlencode}}'
                },
                "description": "Jenkins Linux Slave2 Authentication",
                "stapler-class": "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey"
              }
            }
      force_basic_auth: yes
      method: POST
      password: "{{ jenkins_user_token }}"
      status_code: 302
      url: "{{ jenkins_url }}/credentials/store/system/domain/_/createCredentials"
      user: "{{ jenkins_user }}"
      validate_certs: no
  tags:
      - credential

此游戏出现错误

TASK [jenkins_config : Add credential to add node] ************************************************************************************************************************************************************************************
task path: /home/nile2691/ansible_playbooks/roles/jenkins_config/tasks/main.yaml:34
fatal: [localhost]: FAILED! => {
    "msg": "template error while templating string: no filter named 'urlencode'. String: json={\n    \"\": \"0\",\n    \"credentials\": {\n      \"scope\": \"GLOBAL\",\n      \"id\": \"jenkins_linux_slave2_auth\",\n      \"username\": \"jenkins\",\n      \"password\": \"\",\n      \"privateKeySource\": {\n        \"stapler-class\": \"com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$DirectEntryPrivateKeySource\",\n        \"privateKey\": '{{\"URL ENCODED STRING\"|urlencode}}'\n      },\n      \"description\": \"Jenkins Linux Slave2 Authentication\",\n      \"stapler-class\": \"com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey\"\n    }\n  }\n"
}

在詹金斯中还有其他方法可以通过 urlencode 传递值吗?

答案1

出现错误信息:

没有名为“urlencode”的过滤器

如果你看看这里,您会注意到它在 Jinja2 版本 2.7+ 中受支持。
请升级您的 Jinja2 软件包。

相关内容