DEV Community

Santhosh S
Santhosh S

Posted on

Automating Infrastructure with Semaphore: Ansible + Terraform Integration

Introduction

Automation is the backbone of modern DevOps. In my recent experiments, I combined Ansible and Terraform with Semaphore, a powerful UI-driven orchestration tool. This blog walks through how I set up playbooks, inventories, and Terraform workflows — all managed from Semaphore’s dashboard.

Section 1:

Setting Up Ansible in Semaphore
Repository: Store playbooks in /home/ec2-user/ansible-project.

Inventory: Define hosts in hosts.ini. For localhost testing:

[local]
127.0.0.1 ansible_connection=local

Enter fullscreen mode Exit fullscreen mode

Secrets: Upload SSH keys if targeting remote hosts.

Task Templates: Point directly to .yaml playbook files, not directories.

Section 2:

Running Playbooks on Localhost
Use ansible_connection=local to avoid SSH issues.

Example playbook:

- name: Install NGINX locally
  hosts: local
  become: yes
  tasks:
    - name: Ensure NGINX is installed
      ansible.builtin.yum:
        name: nginx
        state: present
Enter fullscreen mode Exit fullscreen mode

Section 3:

Introducing Terraform
Install Terraform via HashiCorp repo.

Create a simple project:


resource "local_file" "example" {
  content  = "Hello Semaphore + Terraform!"
  filename = "${path.module}/hello.txt"
}
Enter fullscreen mode Exit fullscreen mode

Section 4:

Integrating Terraform into Semaphore
Repository: Add /home/ec2-user/terraform-project.

Project: Link repository (no inventory needed).

GitHub Integration

Connect repository → Link your GitHub repo in Semaphore under Repositories.

Use GitHub Secrets → Store AWS keys, Vault tokens, or Terraform Cloud tokens in GitHub Secrets.

Inject secrets → Reference them in Semaphore Task Templates via environment variables.

OIDC authentication → Enable GitHub OIDC for secure, short‑lived credentials instead of static keys.

⏰ Scheduler Integration

Open Schedule menu → In Semaphore UI, go to Schedule.

Create schedule → Link it to a Task Template (Ansible or Terraform).

Set frequency → Choose hourly, daily, weekly, or cron‑style.

Automate jobs

Conclusion
By combining Ansible and Terraform under Semaphore, I’ve built a clean, UI-driven automation pipeline. It’s flexible enough for local testing and powerful enough for AWS infrastructure. This workflow is a solid foundation for scaling DevOps automation.

Top comments (0)