--- - name: Clone a Git repository and run an Ansible role from it on localhost hosts: vm connection: local vars: repo_clone_dest: "{{ playbook_dir }}/roles/{{ role_name }}" role_base_dir_in_repo: "" role_absolute_path: "{{ repo_clone_dest }}" ansible_cleanup_repo: true tasks: - name: Check that repo_url is provided via Survey Variable ansible.builtin.fail: msg: "Missing required variable 'repo_url'. Define it in the Survey tab in Semaphore UI." when: repo_url is not defined - name: Check that role_name is provided via Survey Variable ansible.builtin.fail: msg: "Missing required variable 'role_name'. Define it in the Survey tab in Semaphore UI." when: role_name is not defined - name: Ensure the target directory for repository cloning exists ansible.builtin.file: path: "{{ repo_clone_dest }}" state: directory mode: '0755' recurse: yes tags: setup - name: Clone or update the Git repository ansible.builtin.git: repo: "{{ repo_url }}" dest: "{{ repo_clone_dest }}" version: main force: yes 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 if the role directory does not exist ansible.builtin.fail: msg: "ERROR: Role directory '{{ role_absolute_path }}' does not exist. Check 'role_name' and the repo structure." when: not role_dir_stat.stat.exists or not role_dir_stat.stat.isdir tags: validation - name: Add the cloned role path to roles_path set_fact: ansible_roles_path: "{{ role_absolute_path }}" tags: run_role - block: - name: Run the specified Ansible role from the cloned repository ansible.builtin.include_role: name: "{{ role_name }}" vars: delegate_to: localhost 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 tags: cleanup