15 Useful Ansible Playbooks for Linux Server Configuration
![15 Useful Ansible Playbooks for Linux Server Configuration](/static/img/files/ansible.webp)
Ansible is a powerful open-source automation tool that simplifies the process of configuring and managing servers. With its ability to automate repetitive tasks, Ansible has become a go-to choice for system administrators and DevOps professionals. In this article, we will explore 15 useful Ansible playbooks specifically designed for Linux server configuration. These playbooks cover a range of tasks, from basic server setup to more advanced configurations.
1. Basic Server Setup
Playbook 1: Install Basic Packages
---
- name: Install basic packages
hosts: all
tasks:
- name: Update package cache
apt:
update_cache: yes
- name: Install essential packages
apt:
name: "{{ item }}"
state: present
with_items:
- vim
- htop
- git
This playbook ensures that essential packages like vim, htop, and git are installed on your Linux servers.
2. SSH Configuration
Playbook 2: Configure SSH
---
- name: Configure SSH
hosts: all
tasks:
- name: Copy SSH public key
authorized_key:
user: "{{ ansible_user }}"
key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
This playbook copies the SSH public key to authorized_keys, enhancing server security and facilitating key-based authentication.
3. Firewall Setup
Playbook 3: Configure Firewall
---
- name: Configure Firewall
hosts: all
tasks:
- name: Allow SSH
ufw:
rule: allow
port: 22
This playbook uses UFW to allow SSH traffic, providing a basic yet effective firewall setup.
4. Nginx Installation and Configuration
Playbook 4: Install Nginx
---
- name: Install Nginx
hosts: web_servers
tasks:
- name: Install Nginx
apt:
name: nginx
state: present
This playbook installs Nginx on designated web servers, laying the foundation for web hosting configurations.
5. Apache Installation and Configuration
Playbook 5: Install Apache
---
- name: Install Apache
hosts: web_servers
tasks:
- name: Install Apache
apt:
name: apache2
state: present
For those preferring Apache, this playbook installs and sets up Apache on specified web servers.
6. MySQL Database Setup
Playbook 6: Install MySQL
---
- name: Install MySQL
hosts: db_servers
tasks:
- name: Install MySQL Server
apt:
name: mysql-server
state: present
This playbook automates the installation of MySQL on designated database servers.
7. PostgreSQL Database Setup
Playbook 7: Install PostgreSQL
---
- name: Install PostgreSQL
hosts: db_servers
tasks:
- name: Install PostgreSQL Server
apt:
name: postgresql
state: present
For PostgreSQL enthusiasts, this playbook installs and configures PostgreSQL on specified database servers.
8. Create User Accounts
Playbook 8: Create User Accounts
---
- name: Create User Accounts
hosts: all
tasks:
- name: Add users
user:
name: "{{ item.name }}"
state: present
with_items:
- { name: 'user1' }
- { name: 'user2' }
This playbook streamlines the process of creating user accounts on all servers.
9. Configure Timezone
Playbook 9: Configure Timezone
---
- name: Configure Timezone
hosts: all
tasks:
- name: Set timezone to UTC
timezone:
name: UTC
Ensure consistent time settings across all servers by using this playbook to set the timezone.
10. Install Docker
Playbook 10: Install Docker
---
- name: Install Docker
hosts: docker_hosts
tasks:
- name: Install Docker
apt:
name: docker.io
state: present
For containerization needs, this playbook installs Docker on specified hosts.
11. Kubernetes Setup
Playbook 11: Install Kubernetes
---
- name: Install Kubernetes
hosts: k8s_hosts
tasks:
- name: Install Kubernetes components
shell: kubeadm init --pod-network-cidr=10.244.0.0/16
This playbook initiates the installation of Kubernetes on designated hosts, paving the way for container orchestration.
12. SSL Certificate Installation
Playbook 12: Install SSL Certificates
---
- name: Install SSL Certificates
hosts: web_servers
tasks:
- name: Copy SSL certificates
copy:
src: /path/to/ssl/certificates
dest: /etc/ssl/certs/
Automate the installation of SSL certificates on web servers with this playbook.
13. Monitoring Setup with Prometheus
Playbook 13: Install Prometheus for Monitoring
---
- name: Install Prometheus
hosts: monitoring_servers
tasks:
- name: Install Prometheus
shell: |
docker run -d -p 9090:9090 \
--name prometheus \
prom/prometheus
This playbook sets up Prometheus for monitoring on specified servers.
14. Log Management with ELK Stack
Playbook 14: Install ELK Stack
---
- name: Install ELK Stack
hosts: log_servers
tasks:
- name: Install ELK Stack
shell: |
docker-compose -f /path/to/elk/docker-compose.yml up -d
Use this playbook to automate the installation of the ELK (Elasticsearch, Logstash, Kibana) Stack for centralized log management.
15. Backup Configuration
Playbook 15: Configure Backup
---
- name: Configure Backup
hosts: backup_servers
tasks:
- name: Setup backup script
copy:
src: /path/to/backup/script.sh
dest: /etc/cron.daily/
mode: '0755'
Ensure data safety by utilizing this playbook to set up backup configurations on specified servers.
Related Searches and Questions asked:
That's it for this topic, Hope this article is useful. Thanks for Visiting us.