app git repo with ansible role

Hello,

I have different git repo of application (code, doc, …), and a repo for each with the role ansible.

Is it possible to merge and put the ansible role in the code repo ?

I’ve tried to make a ansible folder like this :

drwxr-xr-x    - myuser 19 févr. 11:24 ansible
drwxr-xr-x    - myuser 15 sept. 10:02 ├── defaults
.rw-r--r--  831 myuser 15 sept. 10:02 │   └── main.yml
drwxr-xr-x    - myuser 19 févr. 11:24 ├── handlers
.rw-r--r--  824 myuser 19 févr. 11:24 │   └── main.yml
drwxr-xr-x    - myuser 21 août  14:15 ├── meta
.rw-r--r--  295 myuser 19 févr. 11:24 │   └── main.yml
drwxr-xr-x    - myuser 15 sept. 10:02 ├── tasks
.rw-r--r-- 2,8k myuser  9 juil. 15:33 │   └── main.yml
drwxr-xr-x    - myuser 15 sept. 10:02 └── templates
.rw-r--r--  506 myuser 15 sept. 10:02     └── config.yaml.j2
.rw-r--r-- 2,0k myuser 19 févr. 11:24 justfile
.rw-r--r-- 3,3k myuser  9 juil. 15:33 README.md
.rw-r--r--  148 myuser 22 sept.  2025 renovate.json
drwxr-xr-x    - myuser 15 sept. 10:02 src
.rw-r--r-- 2,5k myuser 15 sept. 10:02 ├── conf.rs
.rw-r--r--  298 myuser 22 sept.  2025 ├── errors.rs
.rw-r--r--  10k myuser 15 sept. 10:02 └── main.rs

In my playbooks repo, this is my requirements.yml

[...]
  - name: 'my-rust-project'
    version: '0.0.1'
    scm: git
    src: 'https://gitlab.corp/my-group-app/my-rust-project'
[...]

It works when I launch ansible-galaxy role install -r requirements.yml my-rust-project

But when I look in the role folder, the whole repo is cloned, and it looks like the ansible folder is moved up :

l -T /home/myuser/.ansible/roles/my-rust-project                
drwxr-xr-x    - myuser 28 sept. 16:01 /home/myuser/.ansible/roles/my-rust-project
drwxr-xr-x    - myuser 28 sept. 16:01 ├── defaults
.rw-r--r--  831 myuser 28 sept. 16:01 │   └── main.yml
drwxr-xr-x    - myuser 28 sept. 16:01 ├── handlers
.rw-r--r--  824 myuser 28 sept. 16:01 │   └── main.yml
drwxr-xr-x    - myuser 28 sept. 16:01 ├── meta
.rw-r--r--  295 myuser 28 sept. 16:01 │   └── main.yml
drwxr-xr-x    - myuser 28 sept. 16:01 ├── tasks
.rw-r--r-- 2,8k myuser 28 sept. 16:01 │   └── main.yml
drwxr-xr-x    - myuser 28 sept. 16:01 └── templates
.rw-r--r--  506 myuser 28 sept. 16:01     └── config.yaml.j2
.rw-r--r-- 2,0k myuser 28 sept. 16:01 my-rust-project             # 👈 see this ?!
.rw-r--r-- 2,0k myuser 28 sept. 16:01     └──justfile
.rw-r--r-- 3,3k myuser 28 sept. 16:01     └──README.md
.rw-r--r--  148 myuser 28 sept. 16:01     └──renovate.json
.rw-r--r-- 2,0k myuser 28 sept. 16:01     └──src
.rw-r--r-- 2,5k myuser 28 sept. 16:01         ├── conf.rs
.rw-r--r--  298 myuser 28 sept. 16:01         ├── errors.rs
.rw-r--r--  10k myuser 28 sept. 16:01         └── main.rs

In this tiny project, it is not a big deal, but I use projects with huge git repo size (with “vendor” subfolder for externals libraries) and it is not pretty :sweat_smile:

Is there any solution to “just clone ansible folder” ?

Thanks !

Hi and welcome to the forum.

You can do that, but installing the role with Ansible Galaxy will break, as you have discovered (Ansible Galaxy expects the role to be at the root of the git repo).

I believe you should either point to requirements.yml or specify one or more role names, not both at the same time.

To my knowledge, you cannot install/extract Ansible roles from various directories from within a git repo with Ansible Galaxy. However, if you want to include the role in the code repo, you can look into Git Submodules. This will still require you to keep the Ansible role repository and to point your requirements.yml file to that repo for installation with Ansible Galaxy.

Yes I feared the Git gubmodules solution :sweat_smile:

Thank you !