We have misc labs ,which covers use cases
Lab1:
Add the Jenkins users in sudoers
vi /etc/sudoers jenkins ALL=(ALL) NOPASSWD: ALL
- New Item–>Ansibleproj–>Pipeline–>ok
- 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
- Parameters Declaration: The
parametersblock defines a single parameter namedparam1. This parameter is a string with a default value of'example-value'. It’s used to customize the HTTP request URL. - Stages Declaration: Inside the
stagesblock, you have one stage named ‘HTTP Request’. - HTTP Request Stage: Within the ‘HTTP Request’ stage, you have a
stepsblock containing ascriptblock. This is where the actual steps of the stage are defined.- Script Block: Inside the
scriptblock, you’re making an HTTP GET request using thehttpRequeststep. The URL includes the value of theparam1parameter as a query parameter. - Response Handling: The response from the HTTP request is stored in the
responsevariable. The script then writes the response content to a file namedresponse.txtusing thewriteFilestep. - Print Statements: The script also prints out the status, response content, and headers of the HTTP response using
println
- Script Block: Inside the
