0

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:

  1. activate virtual env, PATH is updated with local virtual env binaries, including ansible-playbook
  2. use command to make bash skip searching for functions and aliases so the function does not call itself in a loop
  3. deactivate virtual env

NOTE: sh is linked to bash in my Jenkins node so bash should be executed by the pipeline.

lubik
  • 79
  • 3
  • 9
  • https://stackoverflow.com/a/30541898/7976758 and https://stackoverflow.com/a/23069201/7976758 Found in https://stackoverflow.com/search?q=%5Bvirtualenv%5D+activate+from+script – phd Jun 13 '22 at 08:23
  • Thank you but it requires updating the script which I cannot do because there would be a lot of changes in the infrastructure. I just simplified this case to a single shell script for demonstration. Moreover, not all my ansible Jenkins nodes (of different versions) run in virtual env. – lubik Jun 13 '22 at 08:32

0 Answers0