10 Essential Ansible Examples for System Administrators
In the fast-paced world of IT, system administrators are constantly seeking efficient and automated ways to manage and configure servers. Ansible, an open-source automation tool, has emerged as a powerful solution for streamlining these tasks. In this article, we'll explore ten essential Ansible examples that every system administrator should have in their toolkit. Whether you're a seasoned Ansible user or just getting started, these examples will help you automate common administrative tasks and save valuable time.
1. Installing Packages:
One of the fundamental tasks for system administrators is installing packages on servers. Ansible simplifies this process by allowing you to define the desired state and then applying it across multiple servers simultaneously.
---
- name: Install Nginx
hosts: web_servers
become: yes
tasks:
- name: Install Nginx
apt:
name: nginx
state: present
In this example, Ansible is instructed to install the Nginx package on a group of servers defined as "web_servers."
2. Managing Services:
Controlling services is another crucial aspect of system administration. Ansible makes it easy to start, stop, or restart services on multiple servers with a simple playbook.
---
- name: Restart Apache
hosts: app_servers
become: yes
tasks:
- name: Restart Apache
service:
name: apache2
state: restarted
Here, Ansible restarts the Apache service on the servers specified in the "app_servers" group.
3. Configuring Files:
System configuration often involves updating configuration files. Ansible allows you to manage these files efficiently by specifying the desired content and applying changes across servers.
---
- name: Configure SSH
hosts: all
become: yes
tasks:
- name: Update SSHd_config
lineinfile:
path: /etc/ssh/sshd_config
regexp: '^PermitRootLogin'
line: 'PermitRootLogin no'
This playbook disables root login via SSH by modifying the "sshd_config" file.
4. User Management:
Creating and managing user accounts is a common task for system administrators. Ansible provides a simple way to automate user management tasks across multiple servers.
---
- name: Add User
hosts: all
become: yes
tasks:
- name: Create User
user:
name: john_doe
password: $6$rounds=10000$random_salt$encrypted_password
In this example, Ansible creates a user named "john_doe" with a securely encrypted password.
5. System Updates:
Keeping servers up-to-date is crucial for security and performance. Ansible facilitates the automation of system updates across multiple servers.
---
- name: Update System
hosts: all
become: yes
tasks:
- name: Update Packages
apt:
upgrade: dist
This playbook ensures that all packages on the specified servers are upgraded to the latest available version.
6. Managing Firewalls:
Securing servers often involves configuring firewalls. Ansible simplifies firewall management by providing modules to define rules across servers.
---
- name: Configure Firewall
hosts: db_servers
become: yes
tasks:
- name: Allow SSH
ufw:
rule: allow
name: ssh
In this example, Ansible allows SSH traffic on servers designated as "db_servers" using the Uncomplicated Firewall (UFW) module.
7. Handling Cron Jobs:
Automating recurring tasks with cron jobs is a common practice. Ansible allows you to manage cron jobs easily across multiple servers.
---
- name: Schedule Backup
hosts: backup_servers
become: yes
tasks:
- name: Add Cron Job
cron:
name: "Backup Task"
minute: "0"
hour: "2"
job: "/path/to/backup_script.sh"
This playbook adds a cron job on servers specified in the "backup_servers" group, scheduling a backup task to run daily at 2:00 AM.
8. Docker Management:
Managing containers with Docker is prevalent in modern IT environments. Ansible facilitates the orchestration of Docker containers across servers.
---
- name: Start Docker Container
hosts: docker_servers
become: yes
tasks:
- name: Run Nginx Container
docker_container:
name: nginx_container
image: nginx
state: started
This example starts an Nginx container on servers defined as "docker_servers."
9. Dynamic Inventories:
Ansible supports dynamic inventories, allowing you to manage servers dynamically. This is particularly useful in cloud environments.
---
- name: Dynamic Inventory Example
hosts: tag_environment_production
become: yes
tasks:
- name: Install Application
apt:
name: my_application
state: present
Here, Ansible targets servers with the tag "environment=production" dynamically.
10. Role-based Playbooks:
Organizing Ansible playbooks into roles enhances modularity and maintainability. This example demonstrates how to structure playbooks using roles.
---
- name: Role-based Playbook
hosts: web_servers
become: yes
roles:
- common
- web_server
In this playbook, Ansible applies the "common" and "web_server" roles to the servers in the "web_servers" group.
Related Searches and Questions asked:
That's it for this topic, Hope this article is useful. Thanks for Visiting us.