Vijesh
Vijesh

Reputation: 1208

Jenkins secret text credential as variable in pipeline script

I have created a credential test_cred of type secret text to store a password, which should be passed to an ansible playbook. I am passing this parameter as an extra variable root_pass to ansible, but the value root_pass is evaluated to string test_cred instead of the secret text contained in it. Can somebody please help to get the value of the credential test_cred so that I can pass it to ansible.

stages {
    stage('Execution') {
        steps {
            withCredentials([string(credentialsId: 'test_cred', variable: 'test')]) {
            }
            ansiblePlaybook(
                installation: 'ansible',
                inventory: "inventory/hosts",
                playbook: "${PLAYBOOK}",
                extraVars: [
                    server: "${params.Server}",
                    client: "${params.Client}",
                    root_pass: "${test}"
                ]
            )
        }
    }
}

Upvotes: 7

Views: 37249

Answers (1)

Vijesh
Vijesh

Reputation: 1208

Thank you Zeitounator. The working code is:

stages {
    stage('Execution') {
        steps {
            withCredentials([string(credentialsId: 'test_cred', variable: 'test')]) {
            
            ansiblePlaybook(
                installation: 'ansible',
                inventory: "inventory/hosts",
                playbook: "${PLAYBOOK}",
                extraVars: [
                    server: "${params.Server}",
                    client: "${params.Client}",
                    root_pass: "${test}"
                ]
            )
            }
        }
    }
}

Upvotes: 14

Related Questions