I have a strange situation where the shell command "sudo su" is not working as expected in Jenkins Declarative pipeline. Below is my code:
stages {
stage('Ansible-dynamichost-generation') {
steps {
ansiColor('xterm') {
sh 'chmod -R 777 /home/jenkins-slave/'
sh 'sudo su ansible'
sh '''#!/bin/bash -l
#Execute commands
echo "$WORKSPACE"
whoami
'''
}
}
}
}
This pipeline is being run by a jenkins-slave user. However, I have a shell command in pipeline that is supposed to switch to a user called "ansible". But, when the "whoami" command is executed, it returns with an output of "jenkins-slave" itself. Can anyone help as to what is going wrong here ?