---
# System prepended metadata

title: Additional Ansible Exercise

---

[toc]



### Introduction

:::success
These problem statements cover a range of scenarios that are commonly encountered in infrastructure automation. They will help you practice various Ansible concepts, such as package management, configuration management, service management, and system administration tasks. Feel free to adapt and expand upon these problem statements to suit your learning goals and requirements.

Please note that the provided playbooks are just samples and may need modifications based on your specific requirements, such as variables, file paths, and service names. Adjust them accordingly before running them in your environment.
:::

### Problem Statement 1: Configure a Web Server

#### Description
:::info
Write an Ansible playbook to provision and configure a web server (e.g., Apache or Nginx) on multiple target hosts. Ensure that the web server is installed, the necessary configuration files are set up, and the service is running.
:::
#### Sample Playbook
````yaml=
---
- name: Provision and configure web server
  hosts: web_servers
  become: true
  
  tasks:
    - name: Install Apache/Nginx
      package:
        name: "{{ web_server_package }}"
        state: present
        
    - name: Copy web server configuration file
      template:
        src: "{{ web_server_config_template }}"
        dest: "{{ web_server_config_path }}"
        
    - name: Start web server service
      service:
        name: "{{ web_server_service }}"
        state: started
        enabled: true
````

### Problem Statement 2: Deploy an Application

#### Description
:::info
Develop an Ansible playbook to deploy a sample application (e.g., a simple web application or a backend service) on a set of target hosts. The playbook should handle installing dependencies, copying application code, configuring any required settings, and starting the application.
:::
#### Sample Playbook
````yaml=
---
- name: Deploy application
  hosts: app_servers
  become: true
  
  tasks:
    - name: Install application dependencies
      package:
        name: "{{ app_dependencies }}"
        state: present
        
    - name: Copy application code
      copy:
        src: "{{ app_code_dir }}"
        dest: "{{ app_deploy_dir }}"
        
    - name: Configure application settings
      template:
        src: "{{ app_config_template }}"
        dest: "{{ app_config_path }}"
        
    - name: Start application service
      service:
        name: "{{ app_service }}"
        state: started
        enabled: true
````

### Problem Statement 3: Manage User Accounts

#### Description

:::info
Create an Ansible playbook that automates the creation and management of user accounts on multiple servers. The playbook should be able to create new user accounts, set passwords, manage SSH keys, and handle the granting or revocation of user access.
:::

#### Sample Playbook
````yaml=
---
- name: Manage user accounts
  hosts: all
  become: true
  
  vars:
    users:
      - username: user1
        password: "{{ 'pass1' | password_hash('sha512') }}"
        ssh_key: "{{ lookup('file', '~/.ssh/user1.pub') }}"
        state: present
      - username: user2
        password: "{{ 'pass2' | password_hash('sha512') }}"
        ssh_key: "{{ lookup('file', '~/.ssh/user2.pub') }}"
        state: present
  
  tasks:
    - name: Create or manage user accounts
      user:
        name: "{{ item.username }}"
        password: "{{ item.password }}"
        ssh_key: "{{ item.ssh_key }}"
        state: "{{ item.state }}"
      loop: "{{ users }}"
````


### Problem Statement 4: Configure Firewall Rules

#### Description
:::info
Write an Ansible playbook to manage firewall rules on target hosts. The playbook should enable or disable specific ports, define custom rules, and ensure that the firewall configurations are consistent across all servers.
:::

#### Sample Playbook
````yaml=
---
- name: Manage firewall rules
  hosts: all
  become: true
  
  tasks:
    - name: Enable required ports
      firewalld:
        port: "{{ item }}"
        state: enabled
        permanent: true
      loop:
        - 80
        - 443
        - 22
        
    - name: Add custom firewall rule
      firewalld:
        rich_rule: "rule family='ipv4' source address='192.168.0.0/24' accept"
        state: enabled
        permanent: true
````

### Problem Statement 5: Install and Configure Monitoring Agent
#### Description
:::info
Develop an Ansible playbook to automate the installation and configuration of a monitoring agent (e.g., Prometheus, Nagios, or Datadog) on multiple servers. The playbook should install the monitoring agent, configure appropriate monitoring targets, and ensure that the agent is running correctly.
:::

#### Sample Playbook
````yaml=
---
- name: Install and configure monitoring agent
  hosts: monitoring_servers
  become: true
  
  tasks:
    - name: Install monitoring agent
      package:
        name: "{{ monitoring_agent_package }}"
        state: present
        
    - name: Copy monitoring agent configuration file
      template:
        src: "{{ monitoring_agent_config_template }}"
        dest: "{{ monitoring_agent_config_path }}"
        
    - name: Start monitoring agent service
      service:
        name: "{{ monitoring_agent_service }}"
        state: started
        enabled: true
````

### Problem Statement 6: Manage Database Servers

#### Description
:::info
Develop an Ansible playbook to automate the provisioning and management of database servers (e.g., MySQL or PostgreSQL) on multiple hosts. The playbook should handle the installation of the database server, configure necessary settings, create databases, and manage user privileges.
:::

#### Sample Playbook
````yaml=
---
- name: Manage database servers
  hosts: database_servers
  become: true
  
  tasks:
    - name: Install Database Server
      package:
        name: "{{ database_server_package }}"
        state: present
        
    - name: Copy database configuration file
      template:
        src: "{{ database_config_template }}"
        dest: "{{ database_config_path }}"
        
    - name: Start database service
      service:
        name: "{{ database_service }}"
        state: started
        enabled: true
        
    - name: Create databases
      mysql_db:
        name: "{{ item }}"
        state: present
      loop:
        - db1
        - db2
        
    - name: Manage user privileges
      mysql_user:
        name: "{{ item.username }}"
        password: "{{ item.password }}"
        priv: "{{ item.privileges }}"
        state: present
      loop:
        - { username: user1, password: pass1, privileges: "*.*:ALL" }
        - { username: user2, password: pass2, privileges: "db1.*:ALL" }
````


### Problem Statement 7: Configure Load Balancer
  
#### Description
:::info
Write an Ansible playbook to provision and configure a load balancer (e.g., Nginx or HAProxy) for distributing incoming traffic across multiple web servers. Ensure that the load balancer software is installed, the necessary configuration files are set up, and the service is running.
:::
#### Sample Solution Playbook
````yaml=
---
- name: Provision and configure load balancer
  hosts: load_balancer
  become: true
  
  tasks:
    - name: Install Load Balancer software
      package:
        name: "{{ load_balancer_package }}"
        state: present
        
    - name: Copy load balancer configuration file
      template:
        src: "{{ load_balancer_config_template }}"
        dest: "{{ load_balancer_config_path }}"
        
    - name: Start load balancer service
      service:
        name: "{{ load_balancer_service }}"
        state: started
        enabled: true

````
