Menu

Building Your First Ansible Automation Project

Building Your First Ansible Automation Project

Creating an Ansible Inventory for Multiple Managed Servers

INVENTORY.INI

[webservers]

web1 ansible_host=WEB_SERVER_1 ansible_user=automation

web2 ansible_host=WEB_SERVER_2 ansible_user=automation

Replace placeholders with authorised host addresses and configure SSH access before running the project.

Writing Playbooks to Install Packages and Manage Services

SITE.YML — UBUNTU/DEBIAN EXAMPLE

---

- name: Configure web servers

hosts: webservers

become: true

tasks:

  -name: Install Nginx

ansible.builtin.apt:

name: nginx

state: present

update_cache: true

 -name: Ensure Nginx is enabled and running

ansible.builtin.service:

name: nginx

state: started

enabled: true

This example targets Ubuntu/Debian systems. For Red Hat-family systems, use the appropriate package module and verify service naming.

Automating Basic Server Configuration and Verifying Results

RUN AND VERIFY

ansible webservers -i inventory.ini -m ansible.builtin.ping

ansible-playbook -i inventory.ini site.yml --syntax-check

ansible-playbook -i inventory.ini site.yml

ansible webservers -i inventory.ini -b -m ansible.builtin.command -a 'systemctl is-active nginx'

Project completion means both hosts are reachable, the playbook completes without failures and Nginx is enabled and active on each target.

Worked Beginner Configuration

A simple project layout keeps inventory, playbook and configuration files together. For a team repository, keep environment-specific inventories and encrypted secrets separate.

ansible-beginner/

├── ansible.cfg

├── inventory.ini

└── site.yml

ANSIBLE.CFG

[defaults]

inventory = ./inventory.ini

host_key_checking = True

interpreter_python = auto_silent

INVENTORY.INI

[webservers]

web1 ansible_host=WEB_SERVER_1 ansible_user=automation

web2 ansible_host=WEB_SERVER_2 ansible_user=automation

SITE.YML

---

- name: Configure web servers

hosts: webservers

become: true

tasks:

-name: Install Nginx

ansible.builtin.apt:

name: nginx

state: present

update_cache: true

 -name: Ensure Nginx is enabled and running

ansible.builtin.service:

name: nginx

state: started

enabled: true

Project: configure two disposable Ubuntu/Debian Linux servers as web servers. Create an inventory, verify SSH connectivity, install Nginx through a playbook, ensure the service is enabled and running, and verify the service state on both hosts.

What You Should Be Able to Do

  • Explain the control-node and managed-node model and the agentless SSH workflow.
  • Install Ansible, configure SSH and verify remote connectivity.
  • Write valid YAML inventories and beginner playbooks.
  • Use ad hoc commands and common built-in modules.
  • Automate package and service configuration across multiple Linux hosts.

You can also explore this interactive lab: https://illustrious-cobbler-e3f249.netlify.app

Level Summary

You have completed the fundamentals: Ansible architecture, installation, YAML, inventory, ad hoc commands, modules and your first multi-host playbook. The next level introduces reusable configuration patterns.


Ansible Tutorial

HA

Harini A



Learn via Video Course

undefined Logo

NaN hrs



Get in Touch For Details