将jenkins凭证从pipline传递到ansible剧本

时间:2019-09-17 12:09:04

标签: jenkins ansible jenkins-pipeline

我尝试将jenkins pipline的凭证作为环境变量传递给ansible剧本到docker。

詹金斯风范:

steps {
  withCredentials([usernamePassword(credentialsId: 'creds', passwordVariable: 'pass', usernameVariable: 'usr')]) {
    ansiblePlaybook(
      playbook: 'AnsiblePlaybook.yml',
      inventory: 'AnsibleInventory.ini',
      installation: 'ansible-latest',
      disableHostKeyChecking: true,
      become: 'yes',
      extras: '-vvv',
      extraVars: [
        usr: ${usr},
        pass: ${pass}
      ]
    )
  }
}

AnsiblePlaybook.yml:

docker_container:
  name: mysql
  image: mysql:5.6
  state: started
  env:
    MYSQL_USER: "{{ usr }}"
    MYSQL_PASSWORD: "{{ pass }}"
    MYSQL_RANDOM_ROOT_PASSWORD: "yes"

我遇到错误:

java.lang.NoSuchMethodError:在步骤之间找不到这样的DSL方法“ $”

2 个答案:

答案 0 :(得分:0)

您可以将参数声明为变量,并将其用于构建ansible:

Jenkinsfile参数:https://jenkins.io/doc/book/pipeline/syntax/#parameters-example

可与参数一起使用:https://docs.ansible.com/ansible/latest/user_guide/playbooks_environment.html

答案 1 :(得分:0)

我有同样的问题。
您使用了错误的语法。 您忘记了" 这是正确的示例:

steps {
  withCredentials([usernamePassword(credentialsId: 'creds', passwordVariable: 'pass', usernameVariable: 'usr')]) {
    ansiblePlaybook(
      playbook: 'AnsiblePlaybook.yml',
      inventory: 'AnsibleInventory.ini',
      installation: 'ansible-latest',
      disableHostKeyChecking: true,
      become: 'yes',
      extras: '-vvv',
      extraVars: [
        usr: "${usr}",  // <================ Here you need extra "
        pass: "${pass}" // <================ Here you need extra "
      ]
    )
  }
}