Pipeline project installation and rest

We have misc labs ,which covers use cases

Lab1:

Add the Jenkins users in sudoers

vi /etc/sudoers

jenkins ALL=(ALL) NOPASSWD: ALL
  1. New Item–>Ansibleproj–>Pipeline–>ok
  2. In pipeline script section, please run paste below code
pipeline {
agent any
 stages {
  stage('Checkout') {
   steps {
    // Checkout your Ansible playbook repository
    git branch: 'main', url: ' https://github.com/amitopenwriteup/tfsource.git'
   }
  }
  stage('Install Ansible') {
   steps {
    // Install Ansible on the Jenkins agent
   sh 'sudo apt-get -y install ansible'
  }
 }
 stage('Run Ansible Playbook') {
   steps {
   // Run the Ansible playbook
  sh 'ansible-playbook -i inventory playbook.yaml'
  }
 }
}
}

Build now–>check build history–>Check console output

Incase, if it failsĀ  please check console output.

apt install python3-pip -y

Lab2: http request using pipeline

Dashboard->Manage Jenkins–>Plugin Manager–>Available plugin–>HTTP Request(install plugin without reboot

New Item–>name httpprojectpipeline–>pipeline–>ok

Paste the below pipeline project

pipeline {
agent any
 parameters {
  string(name: 'param1', defaultValue: 'example-value', description: 'Parameter 1')
 }
stages {
  stage('HTTP Request') {
   steps {
    script {
     def response = httpRequest "http://httpbin.org/response-headers?param1=${param1}"
     writeFile file: 'response.txt', text: response.content
     println("Status: ${response.status}")
     println("Response: ${response.content}")
     println("Headers: ${response.headers}")
  }
 }
}
}
}

Build now–>check the build history

  1. Parameters Declaration: The parameters block defines a single parameter named param1. This parameter is a string with a default value of 'example-value'. It’s used to customize the HTTP request URL.
  2. Stages Declaration: Inside the stages block, you have one stage named ‘HTTP Request’.
  3. HTTP Request Stage: Within the ‘HTTP Request’ stage, you have a steps block containing a script block. This is where the actual steps of the stage are defined.
    • Script Block: Inside the script block, you’re making an HTTP GET request using the httpRequest step. The URL includes the value of the param1 parameter as a query parameter.
    • Response Handling: The response from the HTTP request is stored in the response variable. The script then writes the response content to a file named response.txt using the writeFile step.
    • Print Statements: The script also prints out the status, response content, and headers of the HTTP response using println