Conditional statement

Ansible check lab1:

pipeline {
 agent any
 stages {
  stage('Check ansible') {
   steps {
    script {
     if (fileExists('/usr/bin/ansible')) {
        echo "File /usr/bin/ansible found!"
      } else {
        echo "File /usr/bin/ansible not found!"
       }
     }
   }
  }
 }
}

Lab2: Check and install package

Part1: modify the sudoers file

vi /etc/sudoers
jenkins ALL=(ALL) NOPASSWD: ALL
save the file esc key (:wq!)
systemctl restart jenkins

Part2

pipeline {
 agent any
  stages {
   stage('Check Terraform') {
    steps {
      script {
       if (fileExists('/usr/bin/terraform')) {
         echo "File /usr/bin/terraform found!"
       } else {
        echo "File /usr/bin/terraform not found! Installing Terraform..."
// Download HashiCorp GPG key and add it to keyring
       sh 'wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg'
// Add HashiCorp repository to sources.list.d
      sh 'echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list > /dev/null'
// Update package list
     sh 'sudo apt-get update'
// Install Terraform
     sh 'sudo apt-get install terraform'
    }
   }
  }
 }
}
}

Assignment:

Try same conditional statement:

sudo apt -y install cucumber