85 lines
4.0 KiB
YAML
85 lines
4.0 KiB
YAML
---
|
|
- name: Clone a Git repository and run an Ansible role from it on localhost
|
|
hosts: localhost
|
|
connection: local
|
|
|
|
vars:
|
|
# --- REQUIRED VARIABLES (Adjust these) ---
|
|
repo_url: "git@git.felcloud.io:felcloud/ansible_roles_mongodb.git" # The URL of the Git repository to clone
|
|
role_name: "ansible_roles_mongodb" # The NAME of the role directory *inside* the cloned repo.
|
|
# E.g., if your repo contains 'my_project/roles/nginx_role',
|
|
# then 'role_name' would be 'nginx_role'.
|
|
|
|
# --- DERIVED / CONFIGURABLE PATHS ---
|
|
# The directory where the entire Git repository will be cloned.
|
|
# Choose a location accessible by the Ansible user. Using /tmp for temporary execution.
|
|
repo_clone_dest: "/tmp/ansible_cloned_repo_for_dynamic_role"
|
|
|
|
# Assumes roles are typically located in a 'roles/' subdirectory within the repository root.
|
|
# If your roles are in a different structure (e.g., directly in the repo root or 'ansible/roles'),
|
|
# adjust 'role_base_dir_in_repo' accordingly.
|
|
# For example: If your role is directly in the repo root: `role_base_dir_in_repo: ""`
|
|
# If your role is in 'ansible/roles': `role_base_dir_in_repo: "ansible/roles"`
|
|
role_base_dir_in_repo: "ansible_roles_mongodb" # Directory containing your role(s) within the cloned repo
|
|
|
|
# The absolute path to the specific role directory within the cloned repository.
|
|
# Constructed from the above variables.
|
|
role_absolute_path: "{{ repo_clone_dest }}/{{ role_base_dir_in_repo }}/{{ role_name }}"
|
|
|
|
# --- PLAYBOOK BEHAVIOR FLAGS ---
|
|
# Set to 'true' to clean up the cloned repository after the role has run.
|
|
ansible_cleanup_repo: true # Set to 'false' if you want to keep the cloned repo for inspection/caching
|
|
|
|
tasks:
|
|
- name: Ensure the target directory for repository cloning exists
|
|
ansible.builtin.file:
|
|
path: "{{ repo_clone_dest }}"
|
|
state: directory
|
|
mode: '0755'
|
|
recurse: yes # Create parent directories if they don't exist
|
|
tags: setup
|
|
|
|
- name: Clone or update the Git repository
|
|
ansible.builtin.git:
|
|
repo: "{{ repo_url }}"
|
|
dest: "{{ repo_clone_dest }}"
|
|
version: main # Specify the branch, tag, or commit hash (e.g., 'develop', 'v1.0.0', 'abcdef123')
|
|
force: yes # Useful for re-running; ensures the dest is exactly what's in the repo
|
|
# (pulls changes, or even re-clones if the dest is messy).
|
|
register: git_clone_result
|
|
tags: git
|
|
|
|
- name: Debug - Repository cloning status
|
|
ansible.builtin.debug:
|
|
msg: "Repository cloned to '{{ repo_clone_dest }}'. Changed: {{ git_clone_result.changed }}"
|
|
tags: git
|
|
|
|
- name: Verify that the specified role directory exists within the cloned repository
|
|
ansible.builtin.stat:
|
|
path: "{{ role_absolute_path }}"
|
|
register: role_dir_stat
|
|
tags: validation
|
|
|
|
- name: Fail the playbook if the role directory was not found
|
|
ansible.builtin.fail:
|
|
msg: "ERROR: The specified role directory '{{ role_absolute_path }}' does not exist or is not a directory within the cloned repository. Please check 'role_name' and 'role_base_dir_in_repo' variables, and verify the repository structure."
|
|
when: not role_dir_stat.stat.exists or not role_dir_stat.stat.isdir
|
|
tags: validation
|
|
|
|
- name: Run the specified Ansible role from the cloned repository
|
|
ansible.builtin.include_role:
|
|
name: "{{ role_name }}"
|
|
_path: "{{ role_absolute_path }}"
|
|
# You can pass variables specific to the role here if needed:
|
|
# vars:
|
|
# nginx_port: 8080
|
|
# app_env: "production"
|
|
tags: run_role
|
|
|
|
- name: Clean up the cloned repository (optional)
|
|
ansible.builtin.file:
|
|
path: "{{ repo_clone_dest }}"
|
|
state: absent
|
|
when: ansible_cleanup_repo | default(false) | bool # Only run if ansible_cleanup_repo is true
|
|
tags: cleanup
|