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
parameters
block 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
stages
block, you have one stage named ‘HTTP Request’. - HTTP Request Stage: Within the ‘HTTP Request’ stage, you have a
steps
block containing ascript
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 thehttpRequest
step. The URL includes the value of theparam1
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 namedresponse.txt
using thewriteFile
step. - Print Statements: The script also prints out the status, response content, and headers of the HTTP response using
println
- Script Block: Inside the