简体   繁体   中英

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}"
                ]
            )
        }
    }
}

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}"
                ]
            )
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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