Lab Guide: Deploying and Load Balancing a Web Application

A guide to using Ansible Automation Platform to deploy a web application to both Windows and RHEL servers and configure an HAProxy load balancer.


1. Scenario: Standardized Consistency

A ticket update has just arrived!

Support ticket for web app deployment

With the core infrastructure configured, you are finally ready to deploy the application and configure the load balancer before handing the environment over to the development team.


2. Deploying the Web Application

Your first task is to deploy the web application to both the Windows (IIS) and RHEL (Apache) servers.

  1. Deploy the web app to the Windows server.

    Navigate to your aap tab. Go to Automation Execution → Templates and launch the Windows Deploy WebApp job template. This will configure the Windows server to host the web application on IIS.

  2. Verify the Windows web app.

    Once the job is complete, navigate to the webapp-windows tab in your lab environment to view the running application.

    Web application running on the Windows server
  3. Deploy the web app to the RHEL server.

    Return to the aap tab. Go to Automation Execution → Templates and launch the RHEL Deploy WebApp job template. This will configure the RHEL server to host the same web application on Apache.

  4. Verify the RHEL web app.

    Once the job is complete, navigate to the webapp-rhel tab to view the running application.

    Web application running on the RHEL server

Great work! Both application servers are now live.


3. Configuring the Load Balancer

Lastly, you need to configure the HAProxy load balancer to distribute traffic between your two new web servers.

  1. Check the initial load balancer state.

    Navigate to the loadbalancer tab in your lab environment and refresh the page. You’ll see that it is not yet configured.

    Default HAProxy statistics page
  2. Launch the load balancer configuration template.

    Return to the aap tab. Go to Automation Execution → Templates and launch the Configure Loadbalancer job template.

  3. Add the RHEL server to the load balancer.

    You will be prompted by a survey for a hostname. Enter the FQDN for your RHEL system:

    node01.lab.{dns_domain}

    Entering a hostname in the survey prompt

    Launch the job.

  4. Add the Windows server to the load balancer.

    Re-run the Configure Loadbalancer job template a second time. When prompted by the survey, enter the FQDN for your Windows system:

    dbserver.lab.{dns_domain}

    Launch the job again.

  5. Verify the final configuration.

    After both jobs have completed, you can refresh the loadbalancer tab. You should now see traffic being successfully distributed between both the Windows and RHEL servers. The environment is now ready to be handed over!


4. Appendix: Code Snippets

If you’re interested, here are some key code snippets from the playbooks used in this lab.

4.1. Modifying the HAProxy Configuration

- name: Resolve DNS server hostname to IP
  set_fact:
    host_ip: "{{ lookup('dig', host) }}"

- name: Add static hosts to load balancer
  ansible.builtin.lineinfile:
    path: /etc/haproxy/haproxy.cfg
    line: "server static-{{ host}} {{ host_ip }}:80 check"
    insertafter: '^## STATIC CONFIG ANSIBLE'
    state: present

- name: Add webapp hosts to load balancer
  ansible.builtin.lineinfile:
    path: /etc/haproxy/haproxy.cfg
    line: "server webapp-{{ host }} {{ host_ip }}:80 check"
    insertafter: '^## APP CONFIG ANSIBLE'
    state: present
  notify:
    - restart haproxy

handlers:
  - name: restart haproxy
    service:
      name: haproxy
      state: restarted

4.2. Deploying the Webapp on RHEL

- name: clone a git repo
  ansible.builtin.git:
    repo: https://github.com/nmartins0611/aap25-roadshow-content.git
    dest: /tmp/repo

- name: copy all files from one directory to another
  ansible.builtin.copy:
    src: /tmp/repo/lab-resources/
    dest: /var/www/html
    remote_src: true

- name: Tag the page
  ansible.builtin.lineinfile:
    path: /var/www/html/index.html
    line: "This is running on the RHEL Host"
    insertafter: "<p>&copy; 2024 TMM - Where comedy lives. All rights reserved.</p>"

- name: Start httpd service
  ansible.builtin.service:
    name: httpd
    state: started

5. Troubleshooting

If you have encountered an issue or have noticed something not quite right, please open an issue on GitHub.