Skip to content

Instantly share code, notes, and snippets.

@dmccuk
Created April 25, 2025 08:11
Show Gist options
  • Save dmccuk/3824c5ebcc1368236296bb943cd82aed to your computer and use it in GitHub Desktop.
Save dmccuk/3824c5ebcc1368236296bb943cd82aed to your computer and use it in GitHub Desktop.

🧰 Morpheus + Ansible Integration (Standalone Mode) for VM Provisioning (On-Prem & Cloud)

This guide walks you through setting up Morpheus 8 to provision virtual machines both on-prem (vSphere, KVM, Hyper-V) and in the cloud (AWS, Azure, etc.), and to automatically configure them using Ansible (Standalone mode) running from the Morpheus appliance.


🧱 1. Prepare Base VM Template

Ensure your VM image (on-prem or cloud) includes:

βœ… Requirements:

  • OS: Ubuntu, CentOS, RHEL, etc.
  • User: A non-root user (e.g. ubuntu, morpheus, centos)
  • SSH Access:
    • Public key added to ~/.ssh/authorized_keys
    • sshd enabled
  • Sudo Access:
    • Passwordless sudo configured:
      echo "morpheus ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/morpheus

☁️ 2. Configure Morpheus for VM Provisioning

A. Add a Cloud Integration

Navigate to: Infrastructure > Clouds > + Add

  • For on-prem:
    • VMware vCenter
    • KVM (libvirt)
    • Hyper-V
  • For cloud:
    • AWS, Azure, GCP, etc.
  • Provide access credentials, resource pools, networks

B. Create Credentials

Navigate to: Infrastructure > Credentials > + Add

  • Type: SSH Key or Username/Password
  • Username: User in your base image
  • Private Key: Paste SSH private key
  • Save and assign to your Cloud or Instance Layout

C. Create Instance Type & Layout

Go to: Library > Instance Types > + Add

  • Name: Linux-Base (example)
  • Create a Layout:
    • Select Cloud, image/template, size
    • Assign credential and provisioning method (Cloud-Init if supported)

βš™οΈ 3. Add Ansible Standalone Integration

Navigate to: Admin > Integrations > + Add

  • Type: Ansible
  • Mode: Standalone
  • Executable Path: /usr/bin/ansible-playbook
  • Save the integration

πŸ“ 4. Upload or Link Ansible Playbook

Option A: Upload a Playbook

Navigate to: Library > Templates > Scripts > + Add

  • Name: Configure Webserver
  • Type: Ansible Playbook
  • Upload a .yml file or ZIP with site.yml
  • Choose your Ansible integration

Option B: Use Git

Navigate to: Admin > Integrations > Code Repositories > + Add

  • Add your Git repo (public or private)
  • Morpheus syncs your playbooks automatically

πŸ“œ 5. Ansible Playbook Structure

You can use a flat playbook or a role-based layout. Example below:

site.yml

---
- name: Configure VM post-provision
  hosts: all
  become: yes
  vars:
    ansible_python_interpreter: /usr/bin/python3
  roles:
    - webserver

roles/webserver/tasks/main.yml

---
- name: Install NGINX
  apt:
    name: nginx
    state: present
  when: ansible_os_family == "Debian"

- name: Start NGINX
  service:
    name: nginx
    state: started
    enabled: yes

Optional: roles/webserver/templates/nginx.conf.j2

server {
  listen 80;
  server_name {{ inventory_hostname }};
  root /var/www/html;
}

πŸ”„ 6. Create Automation Workflow

A. Create Task

Go to: Library > Automation > Tasks > + Add

  • Type: Ansible Playbook
  • Select uploaded .yml or synced playbook
  • Choose the correct Ansible integration

B. Create Workflow

Go to: Library > Automation > Workflows > + Add

  • Type: Provisioning Workflow
  • Add the Ansible Task you just created

πŸ”— 7. Attach Workflow to VM Build Process

Attach the workflow to:

  • Instance Layout
  • Blueprint
  • Or manually during provisioning

Go to: Library > Instance Types > [Layout] > Automation Tab
Attach the Provisioning Workflow


βœ… 8. Test the Workflow

Provision a new VM using your configured Instance Type. Then:

  • Navigate to: Instances > [Your VM] > History
  • View the Automation Logs to confirm Ansible executed successfully

πŸ§ͺ Optional: Use Cloud-Init to Inject SSH Key/User

Create a Cloud-Init script to inject keys and users at build time:

#cloud-config
users:
  - name: morpheus
    sudo: ALL=(ALL) NOPASSWD:ALL
    groups: sudo
    shell: /bin/bash
    ssh_authorized_keys:
      - ssh-rsa AAAAB3Nza...your_key_here...
disable_root: true
ssh_pwauth: false

Upload at: Library > Templates > Scripts > + Add
Type: Cloud-Init
Attach to your Layout’s Provisioning Scripts section


πŸ“Ž Summary Checklist

Step Action
βœ… Prepare image with SSH, user, sudo
βœ… Add Cloud (vSphere, AWS, etc.)
βœ… Create and assign Credentials
βœ… Set up Ansible Standalone Integration
βœ… Upload or sync Ansible Playbooks
βœ… Create Ansible Task & Workflow
βœ… Attach Workflow to Layout or Blueprint
βœ… Provision VM and confirm automation

🧰 Bonus: Debug Task Example

Add this task to print host info during provisioning:

- name: Debug host info
  debug:
    msg: "Hostname: {{ inventory_hostname }}, IP: {{ ansible_host }}, Cloud: {{ cloud_name | default('N/A') }}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment