简体   繁体   English

试图将变量从 jenkins 传递给 ansible playbook

[英]Trying to pass a variable from jenkins to ansible playbook

I have setup a webhook from gitlab to kick off a jenkins job based on a tag push event.我已经从 gitlab 设置了一个 webhook 来启动一个基于标签推送事件的 jenkins 工作。 Could be a merge or commit, but the devs want to use a tag.可能是合并或提交,但开发人员想要使用标签。 No prob, it works fine.没问题,它工作正常。 This job gets the hash of the tagged commit, and I've verified that also works.这项工作获取标记提交的哈希值,我已经验证它也有效。 This job then kicks off another job which runs an ansible playbook, and starts a build.然后,该作业启动另一个运行 ansible playbook 的作业,并开始构建。

So I am trying to pass the git commit hash as an ansible extra_var and I write the hash out to a file in the build step via execute shell command.所以我试图将 git commit hash 作为 ansible extra_var 传递,并在构建步骤中通过 execute shell 命令将散列写入文件。 I am using post Build Action to trigger the downstream job, and I am using the parameter from this property file.我正在使用后期构建操作来触发下游作业,并且我正在使用此属性文件中的参数。

The console output verifies this, I see the commit hash echoed out.控制台输出验证了这一点,我看到提交哈希回显。 the properties file contains:属性文件包含:

GIT_KEY=3432134325e4323423    (fake sample hash provided)

The issue is in the downstream job, when I try and pass the GIT_KEY as an extra var so the ansible playbook will fetch the right git commit.问题出在下游作业中,当我尝试将 GIT_KEY 作为额外变量传递时,ansible playbook 将获取正确的 git 提交。 I get the following error:我收到以下错误:

/usr/bin/git checkout --force ${GIT_KEY}", "failed": true, "msg": "Failed to checkout ${GIT_KEY}"

I've tested this statically setting the extra vars value, and it works, so I know the playbook works.我已经测试了这个静态设置额外 vars 值,并且它有效,所以我知道剧本有效。 Its just not interpolating this variable that I'm passing to it correctly, or as I would expect.它只是没有插入我正确传递给它的变量,或者正如我所期望的那样。

Does anyone know how to pass a jenkins paramter as an extra_var to ansible?有谁知道如何将 jenkins 参数作为 extra_var 传递给 ansible?

Assuming you are passing GIT_KEY like below:假设您正在传递GIT_KEY如下:

ansible-playbook my_playbook.yml -e GIT_KEY=$GIT_KEY -e JENKINS_BUILD_NUMBER=$BUILD_NUMBER

In your playbook, you reference the extra vars with {{ GIT_KEY }} and {{ JENKINS_BUILD_NUMBER }}在您的剧本中,您使用{{ GIT_KEY }}{{ JENKINS_BUILD_NUMBER }}引用额外的变量

Here is my pipeline code which I am using and passing variable to the ansible playbook.这是我正在使用的管道代码并将变量传递给 ansible playbook。

Jenkins Pipeline詹金斯管道

stage('Deploy to Dev Server'){
  steps{
         ansiblePlaybook credentialsId: 'Dev-Server', 
         disableHostKeyChecking: true, 
         extras: "-e DOCKER_TAG=${COMMIT_HASH} -e user=adminuser", 
         installation: 'Ansible Jenkins', 
         inventory: 'dev.inv', 
         playbook: 'deploy-ansible.yml'
     }
}

Ansible Playbook (deploy-ansible.yml) Ansible Playbook (deploy-ansible.yml)

- hosts: dev
  become: True
  tasks:
    - name: Start the container
      docker_container:
        name: backend_img
        image: "dockerhub_username/backend_img:{{COMMIT_HASH}}"
        state: started      
        published_ports:
          - 0.0.0.0:9090:9090
        env:
          ENV_MONGO_USER="{{user}}"

I couldn't get it to work with just the paramterized build plugin.我无法让它仅与参数化的构建插件一起工作。 But I got it to work with a workaround, I used the injectEnv plugin in the downstream job to read the KEY:Value from the file, and then I'm able to pass it as an extra var and ansible sees the value.但是我让它使用了一种解决方法,我在下游作业中使用了 injectEnv 插件从文件中读取了 KEY:Value,然后我可以将它作为额外的 var 传递,并且 ansible 可以看到该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM