Ansible Package Automation

Description

This Task will:

1.Install common packages (nginx, git, curl) on multiple Linux servers.

2.Identify the OS type (Debian or RedHat) and install appropriate packages using apt or yum/dnf.

3.Allow easy scaling by adding new servers to the inventory.

4.Log the installation process for verification.

Extended Ansible Task: Automating Package Installation, Security Updates, Configurations, and Monitoring This Task extends our basic package installation playbook to include:

Package Installation - Installs essential packages (nginx, git, curl).

Security Updates - Ensures the system is up-to-date with patches.

Configuration Management - Configures nginx with a sample webpage.

Monitoring Setup - Installs and configures Prometheus Node Exporter for system monitoring.

CI/CD Integration - Optionally triggers updates via Jenkins/GitHub Actions.

Ansible Automation Task

Task Structure


    ansible-automation/
    │── inventory.ini
    │── playbook.yml
    │── roles/
    │   ├── common/
    │   │   ├── tasks/
    │   │   │   ├── main.yml
    │   │   │   ├── security.yml
    │   │   │   ├── monitoring.yml
    │   │   │   ├── nginx.yml
    │   │   ├── templates/
    │   │   │   ├── nginx.conf.j2
    │   │   ├── vars/
    │   │   │   ├── main.yml
    │── ansible.cfg
    │── jenkinsfile (Optional for CI/CD)

1.Inventory File (inventory.ini)

Define your servers.

[debian]
debian-server-1 ansible_host=192.168.1.10 ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/id_rsa

[redhat]
redhat-server-1 ansible_host=192.168.1.20 ansible_user=ec2-user ansible_ssh_private_key_file=~/.ssh/id_rsa

2.Playbook (playbook.yml)

---
- name: Automate package installation, security updates, configurations, user management, and monitoring
  hosts: all
  become: yes
  roles:
    - common
    

3.Role: Common Tasks (roles/common/tasks/main.yml)

This installs essential packages.

---
- name: Install common packages on Debian-based systems
  apt:
    name: "{{ common_packages }}"
    state: present
    update_cache: yes
  when: ansible_os_family == "Debian"

- name: Install common packages on Red Hat-based systems
  yum:
    name: "{{ common_packages }}"
    state: present
  when: ansible_os_family == "RedHat"

4.Security Updates (roles/common/tasks/security.yml)

This applies security patches.

---
- name: Update and upgrade Debian-based systems
  apt:
    upgrade: dist
    update_cache: yes
  when: ansible_os_family == "Debian"

- name: Update and upgrade Red Hat-based systems
  yum:
    name: "*"
    state: latest
  when: ansible_os_family == "RedHat"

5.Nginx Configuration (roles/common/tasks/nginx.yml)

This configures nginx with a sample webpage.

---
- name: Copy Nginx configuration
  template:
    src: nginx.conf.j2
    dest: /etc/nginx/nginx.conf
  notify: Restart nginx

- name: Start and enable nginx service
  service:
    name: nginx
    state: started
    enabled: yes

6.Nginx Configuration Template (roles/common/templates/nginx.conf.j2)

A simple nginx configuration.

server {
    listen 80;
    server_name _;
    location / {
        root /usr/share/nginx/html;
        index index.html;
    }
}
    

7.Monitoring Setup (roles/common/tasks/monitoring.yml)

This installs and configures Prometheus Node Exporter for system monitoring.

---
- name: Download Prometheus Node Exporter
  get_url:
    url: "https://github.com/prometheus/node_exporter/releases/latest/download/node_exporter-1.5.0.linux-amd64.tar.gz"
    dest: "/tmp/node_exporter.tar.gz"

- name: Extract Prometheus Node Exporter
  unarchive:
    src: "/tmp/node_exporter.tar.gz"
    dest: "/usr/local/bin/"
    remote_src: yes

- name: Create systemd service for Node Exporter
  copy:
    dest: "/etc/systemd/system/node_exporter.service"
    content: |
      [Unit]
      Description=Prometheus Node Exporter
      After=network.target

      [Service]
      User=nobody
      ExecStart=/usr/local/bin/node_exporter

      [Install]
      WantedBy=multi-user.target

- name: Start and enable Node Exporter
  systemd:
    name: node_exporter
    state: started
    enabled: yes
  

8.Variables (roles/common/vars/main.yml)

Define package lists.

---
common_packages:
  - nginx
  - git
  - curl
  - htop
  - net-tools
  - unzip

9.Handlers (roles/common/handlers/main.yml)

Handlers restart services when configurations change.

---
- name: Restart nginx
  service:
    name: nginx
    state: restarted

10.Running the Playbook

To execute:

ansible-playbook -i inventory.ini playbook.yml

11.CI/CD Pipeline (Optional)

This integrates with Jenkins.

Jenkinsfile

pipeline {
    agent any
    stages {
        stage('Checkout') {
            steps {
                git 'https://github.com/user/ansible-automation.git'
            }
        }
        stage('Run Ansible') {
            steps {
                sh 'ansible-playbook -i inventory.ini playbook.yml'
            }
        }
    }
}

12.Validation

Verify:

nginx -v
git --version
curl --version
systemctl status node_exporter

Outcome

✅ Automated package installation, security updates, configuration, and monitoring.

✅ Ensures servers are secure and optimized.

✅ Ready for CI/CD pipeline integration.