This tutorial provides a step-by-step guide to understanding Ansible and managing services with it. You’ll learn how to automate service management, configure systems, and solve real-world problems using Ansible playbooks.
Step 1: Introduction to Ansible and Services
Ansible is an open-source automation tool used for configuration management, application deployment, and task automation. Services are background processes that run on a system, such as web servers or databases. Ansible simplifies the management of these services across multiple systems.
Key Concepts:
- Playbooks: YAML files that define tasks and configurations.
- Modules: Pre-built scripts that perform specific tasks (e.g., service module for managing services).
- Inventory: A list of managed nodes (servers).
Practice Exercise
Install Ansible on your local machine and create an inventory file with two fictional servers: web1.example.com and db1.example.com.
Show Solution
# Install Ansible
sudo apt update
sudo apt install ansible
# Create inventory file
cat > inventory <<EOF
[webservers]
web1.example.com
[dbservers]
db1.example.com
EOFStep 2: Writing Your First Playbook
Playbooks are the heart of Ansible automation. They describe the desired state of your systems using YAML syntax. A basic playbook includes:
- Hosts: The target servers.
- Tasks: Actions to perform.
- Modules: Tools to execute tasks.
Example: A playbook to ensure the Apache service is running.
---
- name: Ensure Apache is running
hosts: webservers
become: yes
tasks:
- name: Install Apache
apt:
name: apache2
state: present
- name: Ensure Apache is running
service:
name: apache2
state: startedPractice Exercise
Write a playbook to install and start the Nginx service on the webservers group.
Show Solution
---
- name: Ensure Nginx is running
hosts: webservers
become: yes
tasks:
- name: Install Nginx
apt:
name: nginx
state: present
- name: Ensure Nginx is running
service:
name: nginx
state: startedStep 3: Managing Services with the `service` Module
The service module is used to manage services on remote systems. It supports actions like starting, stopping, restarting, and enabling services at boot.
Common Parameters:
- name: Name of the service.
- state: Desired state (started, stopped, restarted).
- enabled: Whether the service should start on boot (yes or no).
- name: Restart MySQL service
service:
name: mysql
state: restarted
enabled: yesPractice Exercise
Write a playbook to stop the PostgreSQL service and disable it from starting on boot for the dbservers group.
Show Solution
---
- name: Stop and disable PostgreSQL
hosts: dbservers
become: yes
tasks:
- name: Stop PostgreSQL
service:
name: postgresql
state: stopped
- name: Disable PostgreSQL on boot
service:
name: postgresql
enabled: noStep 4: Handling Service Dependencies
Services often depend on other services or configurations. Ansible allows you to manage these dependencies using handlers and notify. Handlers are tasks that run only when notified by other tasks.
Example: Restarting Apache after updating its configuration.
- name: Update Apache configuration
copy:
dest: /etc/apache2/apache2.conf
content: 'ServerName example.com'
notify: Restart Apache
handlers:
- name: Restart Apache
service:
name: apache2
state: restartedPractice Exercise
Write a playbook to update the Nginx configuration file and restart the service only if the configuration changes.
Show Solution
---
- name: Update Nginx configuration
hosts: webservers
become: yes
tasks:
- name: Update Nginx config
copy:
dest: /etc/nginx/nginx.conf
content: 'worker_processes 4;'
notify: Restart Nginx
handlers:
- name: Restart Nginx
service:
name: nginx
state: restartedStep 5: Advanced Service Management: Conditional Execution
Ansible allows you to run tasks conditionally using the when keyword. This is useful for tasks that should only run under specific conditions, such as when a service is installed or a file exists.
Example: Install and start a service only if it is not already installed.
- name: Install and start Redis
hosts: dbservers
become: yes
tasks:
- name: Check if Redis is installed
command: dpkg -s redis
register: redis_status
ignore_errors: yes
- name: Install Redis
apt:
name: redis
state: present
when: redis_status.rc != 0
- name: Start Redis
service:
name: redis
state: started
when: redis_status.rc != 0Practice Exercise
Write a playbook to install and start the Docker service only if it is not already installed on the webservers group.
Show Solution
---
- name: Install and start Docker
hosts: webservers
become: yes
tasks:
- name: Check if Docker is installed
command: dpkg -s docker
register: docker_status
ignore_errors: yes
- name: Install Docker
apt:
name: docker.io
state: present
when: docker_status.rc != 0
- name: Start Docker
service:
name: docker
state: started
when: docker_status.rc != 0Step 6: Real-World Scenario: Automating a Web Application Deployment
In this step, you'll apply everything you've learned to automate the deployment of a web application. This includes:
- Installing a web server (Nginx).
- Deploying application code.
- Configuring and starting services.
- Ensuring dependencies are met.
---
- name: Deploy Web Application
hosts: webservers
become: yes
tasks:
- name: Install Nginx
apt:
name: nginx
state: present
- name: Deploy application code
copy:
src: /path/to/app
dest: /var/www/html
- name: Ensure Nginx is running
service:
name: nginx
state: started
- name: Restart Nginx if configuration changes
handlers:
- name: Restart Nginx
service:
name: nginx
state: restartedPractice Exercise
Write a playbook to deploy a Python Flask application. Ensure the application is served using Gunicorn and Nginx as a reverse proxy.
Show Solution
---
- name: Deploy Flask Application
hosts: webservers
become: yes
tasks:
- name: Install Nginx and Gunicorn
apt:
name:
- nginx
- gunicorn
state: present
- name: Deploy Flask app
copy:
src: /path/to/flask_app
dest: /var/www/flask_app
- name: Configure Nginx as reverse proxy
copy:
dest: /etc/nginx/sites-available/flask_app
content: |
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:8000;
}
}
notify: Restart Nginx
- name: Enable Nginx configuration
file:
src: /etc/nginx/sites-available/flask_app
dest: /etc/nginx/sites-enabled/flask_app
state: link
- name: Start Gunicorn
command: gunicorn --chdir /var/www/flask_app app:app --daemon
handlers:
- name: Restart Nginx
service:
name: nginx
state: restartedSign in to take Cornell notes on this lesson — they save automatically and stay with your account.