Menu

Managing Applications and Infrastructure with Ansible

Managing Applications and Infrastructure with Ansible

Automating Linux User, Package and Service Management

Use purpose-built modules for users, groups, packages, files and services rather than shell commands. Modules are usually more idempotent and provide clearer change reporting.

Configuring Web Servers and Deploying Applications Using Ansible

A web-server role can install Nginx, deploy a validated template, publish application files, set ownership and notify a handler to reload the service. Separate application deployment from infrastructure provisioning where possible.

Use health checks and rollback procedures. A successful Ansible task does not by itself prove that an application is healthy or reachable to users.

Managing Multiple Servers and Automating Multi-Tier Infrastructure Configuration

Use separate inventory groups for web, application and database tiers. Apply common baseline roles to all hosts and specialised roles to each tier. Control rollout order and use serial batches where service availability matters.

Store environment-specific values in group_vars and use clear host/group naming. Keep secrets encrypted or in an approved external secret manager.

Worked Intermediate Configuration

The following compact example shows a reusable Nginx role with a template and handler. A real project should include role metadata, validation, tests and environment-specific variables.

ansible-intermediate/

├── inventory/

│   ├── staging.ini

│   └── production.ini

├── group_vars/

│   └── webservers.yml

├── roles/

│   └── nginx/

│       ├── defaults/main.yml

│       ├── tasks/main.yml

│       ├── handlers/main.yml

│       └── templates/site.conf.j2

└── site.yml

GROUP_VARS/WEBSERVERS.YML

nginx_listen_port: 8080

nginx_server_name: app.example.internal

SITE.YML

---

- name: Configure Nginx web tier

hosts: webservers

become: true

roles:

      -nginx

ROLES/NGINX/TASKS/MAIN.YML

---

- name: Install Nginx

ansible.builtin.apt:

name: nginx

state: present

update_cache: true

- name: Deploy Nginx site configuration

ansible.builtin.template:

src: site.conf.j2

dest: /etc/nginx/sites-available/default

owner: root

group: root

mode: '0644'

notify: Validate and reload Nginx

ROLES/NGINX/HANDLERS/MAIN.YML

---

- name: Validate and reload Nginx

ansible.builtin.command: nginx -t

changed_when: false

listen: Validate and reload Nginx

- name: Reload Nginx

ansible.builtin.service:

name: nginx

state: reloaded

listen: Validate and reload Nginx

ROLES/NGINX/TEMPLATES/SITE.CONF.J2

server {

listen {{ nginx_listen_port }};

server_name {{ nginx_server_name }};

location / {

return 200 'Managed by Ansible';

add_header Content-Type text/plain;

}

}

Project: build a reusable multi-tier configuration project. Organise web and application hosts into inventory groups; create common baseline, web and application roles; use variables and facts to select platform settings; generate service configuration with Jinja2 templates; notify handlers only when configuration changes; and verify each tier with health checks.

WorkstreamDeliverable
Inventory and variablesSeparate staging and production inventories with group_vars and host_vars.
Reusable rolesCommon baseline role plus web and application roles with documented defaults.
Templates and handlersGenerated service configuration with validation and controlled reload.
Execution controlsTags, limits, serial rollout and check-mode review.
VerificationService state, endpoint checks and a clear failure/rollback procedure.

What You Should Be Able to Do

  • Use variables, facts, registered values and precedence intentionally.
  • Write conditionals, loops, blocks and error-handling workflows.
  • Generate configuration with Jinja2 templates and notify handlers.
  • Build, reuse and version roles and collections.
  • Configure multi-tier systems with controlled execution and verification.

Level Summary

You have moved from individual tasks to structured, reusable automation. The advanced level adds dynamic inventory, custom automation, security controls, testing and CI/CD integration.


Ansible Tutorial

HA

Harini A



Learn via Video Course

undefined Logo

NaN hrs



Get in Touch For Details