Saturday, 29 November 2025

Ansible - 2. A typical ansible project setup

 

Ansible Project Setup

A typical basic Ansible project structure looks like this:

ansible-project/
├── inventory/
│   └── hosts          # Your inventory file (hosts file)
└── playbooks/
    └── setup.yml      # Your main playbook

1. Project Directory

Start by creating the main project directory:

mkdir ansible-project
cd ansible-project

2. Inventory Directory

Create a directory for your inventory file:

mkdir inventory

3. Playbooks Directory

Create a directory for your playbooks:

mkdir playbooks

💻 Ansible Host File (Inventory) Example

The inventory file (often named hosts or similar) lists the servers (nodes) that Ansible will manage. It allows you to group them for easy targeting in your playbooks.

You will typically place this file at ansible-project/inventory/hosts.

The default location for the Ansible hosts file (inventory) is:

/etc/ansible/hosts

However, in professional or project-based setups, it is highly recommended to use a local inventory file specific to your project, which you specify when running the playbook using the -i flag, as shown in the previous example:

vi ansible-project/inventory/hosts

[web_servers]
web1.example.com
web2.example.com
192.168.1.10

[db_servers]
db1.example.com
db2.example.com

[all:vars]
ansible_user=your_ssh_user  # Default SSH user for all hosts
ansible_ssh_private_key_file=~/.ssh/id_rsa # Optional: specify a key file
  • Groups: Items in brackets (e.g., [web_servers]) define groups.

  • Hosts: Below each group, you list the hostnames or IP addresses of the servers.

  • Variables: The [all:vars] section defines variables that apply to all hosts in the inventory.


📜 Ansible Playbook Example

A playbook is a YAML file that contains a list of one or more "plays." Each play maps a set of hosts to a set of tasks to be executed.

You will typically place this file at ansible-project/playbooks/setup.yml.

This example playbook performs two simple tasks:

  1. Ensures the Apache web server (httpd on RedHat/Fedora, apache2 on Debian/Ubuntu) package is installed.

  2. Ensures the Apache service is running and set to start on boot.

#vi ansible-project/playbooks/setup.yml

---
- name: Install and Configure Apache Web Server # <--- This is the Play name
  hosts: web_servers                            # <--- Targets the 'web_servers' group from the Inventory
  become: true                                  # <--- Run tasks with root privileges

  tasks:                                        # <--- Start of the ordered list of tasks
    - name: Ensure Apache package is installed
      ansible.builtin.package:                  # <--- Using the 'package' module
        name: httpd
        state: present

    - name: Ensure web service is running and enabled
      ansible.builtin.service:                  # <--- Using the 'service' module
        name: httpd
        state: started
        enabled: true

Running the Playbook

To run this playbook, you use the ansible-playbook command, specifying the playbook file and the inventory file:

ansible-playbook -i inventory/hosts playbooks/setup.yml

No comments:

Post a Comment

Building a Safer PostgreSQL CI/CD Pipeline with GitHub Actions: Dev → PR Review → Test Promotion

In my previous post, we explored a simple push-to-main deployment strategy . While functional, that model is not considered an industry best...