Jenkins – Launch Ansible playbook containing vault file reference from jenkinsfile

ansibleansible-2.xansible-vaultjenkinsjenkins-pipeline

I have a Jenkinsfile trying to launch an Ansible playbook which references some parameters stored in an Ansible vault encrypted file.

Ansible is installed in version 2.4.0.0

Here is a snippet of my jenkins file:

withCredentials([[$class: 'StringBinding', credentialsId: 'vault_token', variable: 'VAULT_TOKEN']]) {

                    ansiblePlaybook(
                            playbook: "./ansible/playbooks/deploy.yml",
                            inventory: "./ansible/hosts/hosts",
                            credentialsId: "$VAULT_TOKEN"
                }

And there is the playbook:

---
- hosts: managers
  become: true
  tasks:
  - include_vars: ../vaults/passwords.yml
  - name: Log into Docker repository
    docker_login:
      registry: my.registry.org
      username: "{{ reg_user }}"
      password: "{{ reg_password }}" 

This playbooks includes the vault file containing the encrypted values.
When Jenkins execute the Jenkinsfile, I get the following error: Attempting to decrypt but no vault secrets found

Why is ansible not using the credentialId i've passed to him in the Jenkinsfile and what is the good way to pass this credential?

Best Solution

  1. Please use 'vaultCredentialsId' instead of 'credentialsId' for vault token.
  2. Remove 'withCredentials'part and straightaway write like vaultCredentialsId:'vault_token' Ansible Plugin link
Related Question