# Role uses module that another role installs - how to handle?

**URL:** <https://forum.ansible.com/t/role-uses-module-that-another-role-installs-how-to-handle/4526>\
**Category:** Get Help\
**Tags:** playbook, linux\
**Created:** [March 23, 2024, 6:14pm UTC](https://forum.ansible.com/t/role-uses-module-that-another-role-installs-how-to-handle/4526 "2024-03-23T18:14:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![crazzyfool](https://avatars.discourse-cdn.com/v4/letter/c/6f9a4e/32.png) [@crazzyfool](https://forum.ansible.com/u/crazzyfool)\
**Post date:** [March 23, 2024, 6:14pm UTC](https://forum.ansible.com/t/role-uses-module-that-another-role-installs-how-to-handle/4526/1 "2024-03-23T18:14:40Z")

</div>

Hi. I have an issue I don’t know how to handle; I’m creating a load of roles to configure my Arch Linux system - the main file looks like this:

```auto
---
- name: Configure Arch Linux on ThinkPad E16 Laptop
  hosts: control
  vars_files:
    - vars/main.yml 

  roles: 
    - roles/base_packages
    - ssh_client_config
    - roles/git
    - roles/vim
    - roles/networkmanager
...
    - roles/alsa
    - roles/aur_builder
    - roles/brave
  handlers:
    - include: handlers/main.yml

```

The `roles/aur_builder` creates a user with sudo privileges on pacman, checks if the package is already installed (shell) and git clones the ansible-aur repository to /tmp if not and then does a makepkg to install it.

That all works fine - so the next role is the first one to use the new **aur** module:

```auto
- name: Install the Brave browser
  become: true
  become_user: aur_builder
  aur:
    name: brave-bin
    use: makepkg
    aur_only: true
    state: present

```

This also works fine - the problem is when I run it from scratch on a unconfigured system, because the **aur** module is not installed.

```auto
[andy@work-pc archlinux]$ ansible-playbook configure.yml --ask-vault-pass 
Vault password: 
ERROR! couldn't resolve module/action 'aur'. This often indicates a misspelling, missing collection, or incorrect module path.

The error appears to be in '/home/andy/ansible/archlinux/roles/brave/tasks/main.yml': line 1, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

- name: Install the Brave browser
  ^ here

```

How can I handle this situation? I have no idea! Thanks.

---

<div class="post-metadata">

**Author:** ![brainmue](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/brainmue/32/2403_2.png) [@brainmue](https://forum.ansible.com/u/brainmue)\
**Post date:** [March 23, 2024, 6:42pm UTC](https://forum.ansible.com/t/role-uses-module-that-another-role-installs-how-to-handle/4526/2 "2024-03-23T18:42:45Z")

</div>

You have probably installed the [Ansible Collection - kewlfft.aur](https://github.com/kewlfft/ansible-aur).

Have you tried the FQCN (Fully Qualified Collection Name) name?  
As described in the example?

```auto
# This task uses the module's short name instead of its FQCN (Fully Qualified Collection Name).
# Use the short name if the module was installed as a local custom module.
# Otherwise, if it was installed through the `kewlfft.aur` collection, this task will fail.

# This task uses the `aur` module's FQCN.
- name: Install trizen using makepkg if it isn't installed already
  kewlfft.aur.aur:
    name: trizen
    use: makepkg
    state: present
  become: yes
  become_user: aur_builder

```

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [March 23, 2024, 7:39pm UTC](https://forum.ansible.com/t/role-uses-module-that-another-role-installs-how-to-handle/4526/3 "2024-03-23T19:39:34Z")

</div>

Ansible “compiles” a play before it starts to run it. All modules a play uses have to exist already.

So you’ll need to split your play into two plays. The first one can put everything in place that the second one needs to run.

```yaml
---
- name: Configure Arch Linux on ThinkPad E16 Laptop - Phase 1
  hosts: control
  vars_files:
    - vars/main.yml 
  roles: 
    - roles/base_packages
    - ssh_client_config
    - roles/git
    - roles/vim
    - roles/networkmanager
    # […]
    - roles/alsa
    - roles/aur_builder
  handlers:
    - include: handlers/main.yml

- name: Configure Arch Linux on ThinkPad E16 Laptop - Phase 2
  hosts: control
  vars_files:
    - vars/main.yml 
  roles: 
    - roles/brave
  handlers:
    - include: handlers/main.yml
```

---

<div class="post-metadata">

**Author:** ![crazzyfool](https://avatars.discourse-cdn.com/v4/letter/c/6f9a4e/32.png) [@crazzyfool](https://forum.ansible.com/u/crazzyfool)\
**Post date:** [March 23, 2024, 10:41pm UTC](https://forum.ansible.com/t/role-uses-module-that-another-role-installs-how-to-handle/4526/4 "2024-03-23T22:41:42Z")

</div>

Thanks - but when I try what you suggest and put all that in one file I seem to run into the same issue when running `configure.yml`. Or do I need to put phase 1 into its own file (`configure_phase1.yml`) and phase 2 into _its_ own file (`configure_phase2.yml`) and run them completely separately?

---

<div class="post-metadata">

**Author:** ![benjamin.schweizer](https://avatars.discourse-cdn.com/v4/letter/b/e47774/32.png) [@benjamin.schweizer](https://forum.ansible.com/u/benjamin.schweizer)\
**Post date:** [March 24, 2024, 1:31am UTC](https://forum.ansible.com/t/role-uses-module-that-another-role-installs-how-to-handle/4526/5 "2024-03-24T01:31:12Z")

</div>

Load your role with ansible.builtin.include\_role  
[https://docs.ansible.com/ansible/latest/collections/ansible/builtin/include\_role\_module.html](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/include_role_module.html)

Then the requirements of that role will not be checked at the beginning

---

<div class="post-metadata">

**Author:** ![flowerysong](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/flowerysong/32/2305_2.png) [@flowerysong](https://forum.ansible.com/u/flowerysong)\
**Post date:** [March 24, 2024, 1:39am UTC](https://forum.ansible.com/t/role-uses-module-that-another-role-installs-how-to-handle/4526/6 "2024-03-24T01:39:03Z")

</div>

Installing a module while `ansible-playbook` is running and then using it is not a supported scenario. You may be able to get it working by being clever, but there’s no guarantee that it will continue working in the future.

Instead, you should built your process so that all of the Ansible content that you depend on is installed before you run `ansible-playbook` against a playbook that requires it. There are many ways you can approach this, such as calling `ansible-galaxy` directly to install the dependencies, or moving your existing steps into a bootstrap playbook and using a separate invocation of `ansible-playbook`.

---

<div class="post-metadata">

**Author:** ![crazzyfool](https://avatars.discourse-cdn.com/v4/letter/c/6f9a4e/32.png) [@crazzyfool](https://forum.ansible.com/u/crazzyfool)\
**Post date:** [March 24, 2024, 6:49pm UTC](https://forum.ansible.com/t/role-uses-module-that-another-role-installs-how-to-handle/4526/7 "2024-03-24T18:49:13Z")

</div>

@benjamin.schweizer - thanks! I think this is the solution I’ll go with for now. As a test I created:

```auto
[andy@work-pc archlinux]$ cat site.yml -p
---
- name: Configure Arch Linux on ThinkPad E16
  hosts: control
  vars_files:
    - vars/main.yml 
  roles: 
    - roles/aur_builder
  handlers:
    - include: handlers/main.yml

  tasks:
    # tasks that use aur module:
    - name: Install Brave
      include_role:
        name: brave

```

I uninstalled `brave-bin` and `ansible-aur`; deleted the cloned git repo from `/tmp` and ran. And it worked!!

Although I do take your point @flowerysong - I think my longer term plan will be to create a play for the [initial installation that I run from a LiveUSB](https://wiki.archlinux.org/title/installation_guide) which includes the `aur_builder` role. That way when I reboot into my fresh OS I can run the configure part normally again.

Thanks for everyone’s help!

---

<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:** [April 23, 2024, 6:49pm UTC](https://forum.ansible.com/t/role-uses-module-that-another-role-installs-how-to-handle/4526/8 "2024-04-23T18:49:49Z")

</div>

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