'Jenkins ansible-playbook aws_ec2 unrecognized arguments: secret file error
I try to integrate ansible aws_ec2 dynamic inventory into a Jenkins, but I have this kind of problem:
ansible-playbook -i aws_ec2.yaml ping-pb.yaml --private-key **** -e ansible_ssh_user=ec2-user
usage: ansible-playbook [-h] [--version] [-v] [-k]
[--private-key PRIVATE_KEY_FILE] [-u REMOTE_USER]
---
[--list-tags] [--step] [--start-at-task START_AT_TASK]
playbook [playbook ...]
ansible-playbook: error: unrecognized arguments: provisioning configuration@tmp/secretFiles/bbe6eeff-027e-480b-ae88/keyforjenk010422.pem
I added my .pem file into a "secret file" storage of Jenkins credentials.
This is how my pipeline looks like:
steps {
withCredentials([file(credentialsId: 'rmp-pem', variable: 'PRIVATE')]) {
dir('Ansible') {
sh "ansible-playbook -i aws_ec2.yaml ping-pb.yaml --private-key $PRIVATE -e 'ansible_ssh_user=ec2-user'" } } } }
When I don't use the credentials but store the file in my Github repo it works fine, so the problem is.. in Jenkins? I also tried to store my private key as sshUserPrivateKey in Jenkins, but the result is the same.
Solution 1:[1]
It's because Jenkins seems to have generated a filename that contain spaces, so you'll need to quote that $PRIVATE shell variable to present it as one argument to --private-key
Due to the existing " string literal used by Groovy, you will need to escape the quote characters:
sh "ansible-playbook -i aws_ec2.yaml ping-pb.yaml --private-key \"$PRIVATE\" -e 'ansible_ssh_user=ec2-user'"
or, you can use Groovy's alternative syntax
sh """ansible-playbook -i aws_ec2.yaml ping-pb.yaml --private-key "$PRIVATE" -e 'ansible_ssh_user=ec2-user'"""
You need to use " specifically because '$PRIVATE' would not expand the shell variable
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mdaniel |
