I use a shell script that, let's assume, I cannot update:
#!/bin/bash
ansible-playbook $1
The script is called in Jenkins declarative pipeline as:
sh "myscript.sh my-playbook.yml"
The pipeline runs on Jenkins node where ansible (2.9) is installed in python (2.7) virtual env. ansible-playbook
is hence not available before the virtual env is activated.
Is it possible to call ansible-playbook
without activating it in the shell script? I am allowed to do changes on the Jenkins node only.
I tried to create a function in ~/.bashrc
(and source it):
function ansible-playbook() {
source /path/to/my/venv/bin/activate
command ansible-playbook "$@"
deactivate
}
It works when called in the shell, but Jenkins pipeline fails on "ansible-playbook: command not found"
. Is it not the correct approach to:
- activate virtual env, PATH is updated with local virtual env binaries, including
ansible-playbook
- use
command
to make bash skip searching for functions and aliases so the function does not call itself in a loop - deactivate virtual env
NOTE: sh is linked to bash in my Jenkins node so bash should be executed by the pipeline.