# Python module API for Ansible Galaxy

**URL:** <https://forum.ansible.com/t/python-module-api-for-ansible-galaxy/44787>\
**Category:** Get Help\
**Tags:** ansible-galaxy, galaxy-ng\
**Created:** [November 4, 2025, 10:04pm UTC](https://forum.ansible.com/t/python-module-api-for-ansible-galaxy/44787 "2025-11-04T22:04:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Belal](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/belal/32/9793_2.png) [@Belal](https://forum.ansible.com/u/Belal)\
**Post date:** [November 4, 2025, 10:04pm UTC](https://forum.ansible.com/t/python-module-api-for-ansible-galaxy/44787/1 "2025-11-04T22:04:20Z")

</div>

Hi all. I was wondering if there is a way to use Ansible Galaxy from within Python.

I’m familiar with using Ansible Galaxy in the CLI:

```auto
ansible-galaxy collection install /path/to/ansible-galaxy-requirements.yml

```

I’d like to be able to do something like this:

```auto
from ansible import galaxy

galaxy.install({
    'collections': ['community.general', 'community.mysql'],
    'roles': ['geerlingguy.php'],
})

```

On that note, it would be really cool if I could specify versions as well, but I’d be happy with just the above as well.

Is something like this currently possible or supported in a future version? Thanks in advance.

Kind regards

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [November 4, 2025, 11:21pm UTC](https://forum.ansible.com/t/python-module-api-for-ansible-galaxy/44787/2 "2025-11-04T23:21:06Z")

</div>

TL;DR: No

Outside of the plugin APIs, none of the python code of ansible-core should be considered a public API. Effectively, in practice that means that the ability to run any of the functionality of the ansible CLIs is not supported by any public API.

---

<div class="post-metadata">

**Author:** ![mariolenz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mariolenz/32/108_2.png) [@mariolenz](https://forum.ansible.com/u/mariolenz)\
**Post date:** [November 5, 2025, 4:08pm UTC](https://forum.ansible.com/t/python-module-api-for-ansible-galaxy/44787/3 "2025-11-05T16:08:04Z")

</div>

Thanks for the clarification on ansible-core code @sivel!

But maybe there’s some other way, even if it’s a 3rd party lib. Or some quick’n’dirty hack.

Isn’t galaxy based on [pulp](https://pulpproject.org/)? I’m not sure there, but if I’m right: Maybe there’s a way to automatically build a client for pulp based repositories?

I’m just wildly guessing around, and possibly on completely wrong assumptions!

---

<div class="post-metadata">

**Author:** ![gundalow](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/gundalow/32/99_2.png) [@gundalow](https://forum.ansible.com/u/gundalow)\
**Post date:** [November 6, 2025, 8:51pm UTC](https://forum.ansible.com/t/python-module-api-for-ansible-galaxy/44787/4 "2025-11-06T20:51:39Z")

</div>

Could you help me understand why calling the command doesn’t work for you?

---

<div class="post-metadata">

**Author:** ![markstos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/markstos/32/9706_2.png) [@markstos](https://forum.ansible.com/u/markstos)\
**Post date:** [November 7, 2025, 12:54am UTC](https://forum.ansible.com/t/python-module-api-for-ansible-galaxy/44787/5 "2025-11-07T00:54:52Z")

</div>

@Belal A number of programmatic APIs for CLI tools are wrapping calls to the CLI tool, and the same approach could be used here if you’d like:

```python
import subprocess

def ansible_galaxy(*args: str) -> None:
    subprocess.run(
        ["ansible-galaxy", *args],
        check=True,
        text=True,
    )

ansible_galaxy("collection", "install", "community.general", "community.mysql")
ansible_galaxy("role", "install", "geerlingguy.php")

```

The result would look like this:

```auto
➤ python3 test.py
Starting galaxy collection install process
Nothing to do. All requested collections are already installed. If you want to reinstall them, consider using `--force`.
Starting galaxy role install process
- downloading role 'php', owned by geerlingguy
- downloading role from https://github.com/geerlingguy/ansible-role-php/archive/6.0.0.tar.gz
- extracting geerlingguy.php to /home/mark/.config/ansible/roles/geerlingguy.php
- geerlingguy.php (6.0.0) was installed successfully

```

---

<div class="post-metadata">

**Author:** ![Belal](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/belal/32/9793_2.png) [@Belal](https://forum.ansible.com/u/Belal)\
**Post date:** [November 8, 2025, 5:52pm UTC](https://forum.ansible.com/t/python-module-api-for-ansible-galaxy/44787/6 "2025-11-08T17:52:23Z")

</div>

Hi all,

Thanks for the quick feedback on this. I have some CLI tools based on Ansible Runner, with a use case where I wanted to fine tune some of the Galaxy dependency handling… read requirements from a file, detect what’s already installed, confirm user approval, installing the missing ones… but in the interest of time and simplicity, I decided to abandon this. There’s no reasons why those tools can’t just ship with a requirements file, install from that during runtime, and be done with it.

@gundalow Hope this answers your question. ansible-galaxy command works just fine 🙂

@markstos Nice! And could be very useful for other stuff too… I ended up doing this basically.

---

<div class="post-metadata">

**Author:** ![gundalow](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/gundalow/32/99_2.png) [@gundalow](https://forum.ansible.com/u/gundalow)\
**Post date:** [November 10, 2025, 7:27pm UTC](https://forum.ansible.com/t/python-module-api-for-ansible-galaxy/44787/7 "2025-11-10T19:27:59Z")

</div>

> [@Belal](#):
>
> @gundalow Hope this answers your question. ansible-galaxy command works just fine 🙂

Glad it works 😀

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [December 10, 2025, 7:28pm UTC](https://forum.ansible.com/t/python-module-api-for-ansible-galaxy/44787/8 "2025-12-10T19:28:32Z")

</div>

Closed on December 10, 2025, 7:28pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
