0
0
Jenkinsdevops~5 mins

System configuration management in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
System configuration management helps automate the setup and maintenance of software and settings on servers. It solves the problem of manually configuring many machines, which can be slow and error-prone.
When you want to automatically install and configure software on multiple servers.
When you need to ensure all servers have the same settings and software versions.
When you want to quickly recover or replace a server with the exact same setup.
When you want to track and control changes to server configurations over time.
When you want to reduce human errors in manual server setup.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
    agent any
    stages {
        stage('Install and Configure') {
            steps {
                // Install nginx web server
                sh 'sudo apt-get update'
                sh 'sudo apt-get install -y nginx'
                // Start nginx service
                sh 'sudo systemctl start nginx'
                // Enable nginx to start on boot
                sh 'sudo systemctl enable nginx'
            }
        }
    }
}

This Jenkinsfile defines a pipeline that automates system configuration tasks.

The agent any means it can run on any available Jenkins agent.

In the Install and Configure stage, it runs shell commands to update package lists, install nginx, start the nginx service, and enable it to start automatically on boot.

Commands
Check if Jenkins Job Builder is installed and see its version to ensure the environment is ready.
Terminal
jenkins-jobs --version
Expected OutputExpected
1.0.0
Trigger the Jenkins pipeline named 'my-pipeline' which runs the system configuration steps.
Terminal
jenkins-cli -s http://localhost:8080 build my-pipeline
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] stage [Pipeline] { (Install and Configure) [Pipeline] sh + sudo apt-get update ... (output of apt-get update) [Pipeline] sh + sudo apt-get install -y nginx ... (output of nginx installation) [Pipeline] sh + sudo systemctl start nginx [Pipeline] sh + sudo systemctl enable nginx [Pipeline] } [Pipeline] End of Pipeline Finished: SUCCESS
-s - Specifies the Jenkins server URL
Verify that the nginx service is running after the configuration steps.
Terminal
systemctl status nginx
Expected OutputExpected
● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2024-06-07 10:00:00 UTC; 1min ago Main PID: 1234 (nginx) Tasks: 3 (limit: 4915) Memory: 5.0M CGroup: /system.slice/nginx.service ├─1234 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; └─1235 nginx: worker process
status - Shows the current status of the service
Key Concept

If you remember nothing else from this pattern, remember: automating system setup with pipelines saves time and avoids errors.

Common Mistakes
Running configuration commands manually on each server.
This is slow, error-prone, and hard to keep consistent across servers.
Use automated pipelines like Jenkins to run configuration commands on all servers reliably.
Not enabling services to start on boot after installation.
The service will stop after a reboot, causing downtime.
Always enable services with systemctl enable to start automatically on system boot.
Not verifying service status after configuration.
You might think the service is running when it is not, causing failures.
Check service status with systemctl status to confirm it is active and running.
Summary
Use Jenkins pipelines to automate system configuration tasks like software installation and service setup.
Run shell commands in pipeline stages to update packages, install software, and manage services.
Verify the service status after configuration to ensure it is running correctly.