0

i tried running ansible playbook to run tasks from jenkins pipeline. I have stored the creds as jenkins secrets and pass them as environment. But it gives and error "error: unrecognized arguments: ****"

environment {
      SERVICE_ACCOUNT_ID = credentials('SERVICE_ACCOUNT_ID')
      SERVICE_ACCOUNT_PWD = credentials('SERVICE_ACCOUNT_PWD')
}

now, running this block of set to execute ansible playbook

result = sh (
                    script: "ansible-playbook -i hostname.txt -u '${env.SERVICE_ACCOUNT_ID}' install_agent_onprem-TM.yaml -k '${env.SERVICE_ACCOUNT_PWD}'",
                    returnStdout: true
                ).trim()

However, it threw an error and didnt execute the ansible command

+ ansible-playbook -i hostname.txt -u **** install_agent_onprem-TM.yaml -k ****
usage: ansible-playbook [-h] [--version] [-v] [-k]
                        [--private-key PRIVATE_KEY_FILE] [-u REMOTE_USER]
                        [-c CONNECTION] [-T TIMEOUT]
                        [--ssh-common-args SSH_COMMON_ARGS]
                        [--sftp-extra-args SFTP_EXTRA_ARGS]
                        [--scp-extra-args SCP_EXTRA_ARGS]
                        [--ssh-extra-args SSH_EXTRA_ARGS] [--force-handlers]
                        [--flush-cache] [-b] [--become-method BECOME_METHOD]
                        [--become-user BECOME_USER] [-K] [-t TAGS]
                        [--skip-tags SKIP_TAGS] [-C] [--syntax-check] [-D]
                        [-i INVENTORY] [--list-hosts] [-l SUBSET]
                        [-e EXTRA_VARS] [--vault-id VAULT_IDS]
                        [--ask-vault-password | --vault-password-file VAULT_PASSWORD_FILES]
                        [-f FORKS] [-M MODULE_PATH] [--list-tasks]
                        [--list-tags] [--step] [--start-at-task START_AT_TASK]
                        playbook [playbook ...]
ansible-playbook: error: unrecognized arguments: ****
shailysharma
  • 61
  • 1
  • 5
  • 3
    `-k` asks for the password as a prompt, it does not allow you to pass it as an argument, you'll need to adapt the `ansible_password` variable for this, which you can do as an extra variable (`-e ansible_password=my_password`) – β.εηοιτ.βε Aug 09 '23 at 19:18
  • 1
    Does this answer your question? [How to pass a user / password in ansible command](https://stackoverflow.com/questions/37004686/how-to-pass-a-user-password-in-ansible-command). Although you are not looking for the accepted answer but for this one: https://stackoverflow.com/a/50640856/2123530 – β.εηοιτ.βε Aug 09 '23 at 19:48
  • 1
    Thanks, It actually worked! I have removed -k and add -e ansible_password='${env.SERVICE_ACCOUNT_PWD} during run-time. and it actually worked – shailysharma Aug 09 '23 at 20:07
  • I believe the JP Ansible plugin has a native integration with the Jenkins credentials plugin for this. – Matthew Schuchard Aug 09 '23 at 20:27
  • 1
    @MattSchuchard, indeed: https://plugins.jenkins.io/ansible/#plugin-content-password – β.εηοιτ.βε Aug 09 '23 at 22:01

0 Answers0