如何将 json 字符串从 Jenkins 参数传递到 Shell ansible 参数

问题描述 投票:0回答:1

我有一个 Jenkins 管道,它运行一个包含一些 ansible 的 shell 脚本。脚本的 ansible 部分采用来自 Jenkins 作业的参数

ANSIBLE_ARG
。此参数可以采用任意数量的参数
-v
-b
,或者在本问题中
-e

但是,当我将 json 字符串传递给

-e
标志时,ansible 无法处理命令。

pipeline {
  
  agent any
  parameters {
    string(name: 'ANSIBLE_ARG', defaultValue='', description='argument to pass to ansible)
  }
  stages {
    stage('reproduce ansible error') {
      steps {
        sh '''
echo "${ANSIBLE_ARG}"
ansible -i localhost, -c local ${ANSIBLE_ARG} all -m debug -a "var=key"
        '''
    }
  }
}

我将

ANSIBLE_ARG
设置为
-e {"key":"some value with spaces"}

来调用它

Ansible 抛出错误,指出无法识别的参数“带有空格”。

回显的命令显示单引号 (

'
) 正在替换中的各个点添加
ansible -i localhost, -c local -e '{"key":"some' value with 'spaces"}' all -m debug -a "var=key"

有没有办法让 Jenkins 不将单引号注入到我的 json 中间?

jenkins ansible parameters jenkins-pipeline substitution
1个回答
0
投票

你用

echo
做了正确的事情 - 你引用了你的变量扩展。当你打电话时也这样做
ansible

ansible -i localhost, -c local "${ANSIBLE_ARG}" all -m debug -a "var=key"

linter 会告诉你这一点。这就是为什么嵌套语法是一个坏主意。

© www.soinside.com 2019 - 2024. All rights reserved.