Ansible Playbooks for Infrastructure Automation

Some Automation Scripts for Infrastructure Management Using Ansible

Automate important, repetitive tasks with these essential Ansible playbooks. Each playbook includes sample code and explanations to help you seamlessly integrate them into your infrastructure.


Table of Contents

  1. Server Provisioning Playbook
    • Description: Automates server setup tasks such as creating users, configuring SSH, and setting basic security settings.
    • Sample Tasks: Create user accounts, configure SSH access, and set up firewall rules.
  2. Web Server Setup Playbook
    • Description: Installs and configures a web server (Apache/Nginx) to serve web applications.
    • Sample Tasks: Install Apache or Nginx, configure virtual hosts, start and enable services.
  3. Database Setup and Configuration Playbook
    • Description: Automates the installation and setup of database servers like MySQL or PostgreSQL.
    • Sample Tasks: Install database software, configure database users, and start database service.
  4. User and Permission Management Playbook
    • Description: Manages user accounts, groups, and permissions across servers to control access.
    • Sample Tasks: Add and remove users, assign groups, and configure sudo privileges.
  5. Firewall Configuration Playbook
    • Description: Configures firewall rules to control network access and secure infrastructure.
    • Sample Tasks: Allow specific ports (e.g., HTTP, HTTPS), and deny all other traffic.
  6. Backup and Restore Playbook
    • Description: Automates backup of critical data and files, with options for restoring data as needed.
    • Sample Tasks: Archive important directories, set up database backups, automate file transfers to backup servers.
  7. System Update and Patch Management Playbook
    • Description: Keeps systems updated by automating security patches and software upgrades.
    • Sample Tasks: Run package updates, apply security patches, and schedule regular maintenance.
  8. Application Deployment Playbook
    • Description: Manages the deployment of applications, including pulling from version control and restarting services.
    • Sample Tasks: Pull code from Git, set up configuration files, and restart application services.
  9. Monitoring Setup Playbook
    • Description: Installs and configures monitoring tools (e.g., Prometheus or Nagios) to track infrastructure health.
    • Sample Tasks: Install monitoring software, configure alerts, and set up monitoring dashboards.
  10. Disk Space and Resource Cleanup Playbook
    • Description: Frees up disk space and optimizes system resources by cleaning up temporary and unnecessary files.
    • Sample Tasks: Remove unused packages, clear cache, and clean up log files.

Here are 10 essential automation scripts for infrastructure management using Ansible, each focusing on key tasks to streamline and automate common infrastructure needs. Each playbook includes sample code and explanations to make integration easy.

1. Server Provisioning Playbook

  • Purpose: Automates the setup of a new server, including user creation, SSH configuration, and security settings.
  • Sample Code:
    - name: Provision server
      hosts: all
      tasks:
        - name: Create a user
          user:
            name: "{{ username }}"
            state: present
        - name: Configure SSH access
          authorized_key:
            user: "{{ username }}"
            key: "{{ ssh_key }}"
    
  • Explanation: This playbook creates users and sets up SSH access, simplifying the first steps of provisioning.

2. Web Server Setup Playbook

  • Purpose: Installs and configures a web server (e.g., Apache or Nginx) for hosting websites or applications.
  • Sample Code:
    - name: Set up Apache web server
      hosts: web_servers
      tasks:
        - name: Install Apache
          apt:
            name: apache2
            state: present
        - name: Start Apache service
          service:
            name: apache2
            state: started
    
  • Explanation: This script installs and starts the Apache service, making the server ready to host web applications.

3. Database Setup and Configuration Playbook

  • Purpose: Automates the installation and configuration of databases like MySQL or PostgreSQL.
  • Sample Code:
    - name: Set up MySQL
      hosts: db_servers
      tasks:
        - name: Install MySQL
          apt:
            name: mysql-server
            state: present
        - name: Start MySQL service
          service:
            name: mysql
            state: started
    
  • Explanation: Sets up and starts the MySQL service, preparing the server for database use.

4. User and Permission Management Playbook

  • Purpose: Manages user accounts, groups, and permissions to control access.
  • Sample Code:
    - name: User and permission management
      hosts: all
      tasks:
        - name: Add a new user
          user:
            name: "{{ username }}"
            groups: "{{ group }}"
            state: present
    
  • Explanation: This playbook ensures the correct users and groups are created, with assigned permissions.

5. Firewall Configuration Playbook

  • Purpose: Configures the firewall to secure server access by allowing only necessary ports.
  • Sample Code:
    - name: Configure UFW firewall
      hosts: all
      tasks:
        - name: Allow HTTP and HTTPS
          ufw:
            rule: allow
            port: "{{ item }}"
        with_items:
          - 80
          - 443
    
  • Explanation: Opens only the HTTP and HTTPS ports, enhancing server security.

6. Backup and Restore Playbook

  • Purpose: Automates backup of files or databases, with the option for a restore process.
  • Sample Code:
    - name: Backup files and databases
      hosts: all
      tasks:
        - name: Compress and backup /var/www
          archive:
            path: /var/www
            dest: /backups/www_backup.tar.gz
    
  • Explanation: Archives and saves server data, ensuring data protection and easy restoration.

7. System Update and Patch Management Playbook

  • Purpose: Keeps systems secure and up-to-date by automating updates and patching.
  • Sample Code:
    - name: Update and upgrade packages
      hosts: all
      tasks:
        - name: Run update and upgrade
          apt:
            update_cache: yes
            upgrade: dist
    
  • Explanation: Automates updating and upgrading software packages, ensuring security and stability.

8. Application Deployment Playbook

  • Purpose: Manages application deployment, including pulling from version control, configuring settings, and restarting services.
  • Sample Code:
    - name: Deploy application
      hosts: app_servers
      tasks:
        - name: Clone repository
          git:
            repo: 'https://example.com/repo.git'
            dest: /var/www/app
        - name: Restart application
          service:
            name: app
            state: restarted
    
  • Explanation: Pulls the latest code and restarts the application service, automating deployment.

9. Monitoring Setup Playbook

  • Purpose: Installs and configures monitoring tools like Prometheus or Nagios for infrastructure health monitoring.
  • Sample Code:
    - name: Set up monitoring
      hosts: all
      tasks:
        - name: Install Prometheus
          apt:
            name: prometheus
            state: present
        - name: Start Prometheus
          service:
            name: prometheus
            state: started
    
  • Explanation: Sets up monitoring services, providing visibility into system health.

10. Disk Space and Resource Cleanup Playbook

  • Purpose: Regularly cleans up unnecessary files to free up disk space and optimize resource usage.
  • Sample Code:
    - name: Disk space cleanup
      hosts: all
      tasks:
        - name: Remove unused packages
          apt:
            autoremove: yes
        - name: Clear cache
          command: apt clean
    
  • Explanation: Automates system cleanup, freeing up resources and maintaining efficiency.

These scripts will help automate repetitive tasks and maintain infrastructure efficiency and reliability. Each playbook is structured to be reusable, making it easy to apply in diverse environments.

If you need any support regarding your website

If you like our blog, Please support us to improve

Buy Me a Coffee

Leave a Reply

Your email address will not be published. Required fields are marked *

RECENT POST
Leetcode Solutions

633. Sum of Square Numbers

Sum of Square Numbers Difficulty: Medium Topics: Math, Two Pointers, Binary Search Given a non-negative integer c, decide whether there’re

Leetcode Solutions

624. Maximum Distance in Arrays

Maximum Distance in Arrays Difficulty: Medium Topics: Array, Greedy You are given m arrays, where each array is sorted in