Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically run "ansible-galaxy install -r requirement.yml -p roles/" as a prerequisite before running a role

Tags:

roles

ansible

I'm trying to get the following Ansible playbook to work but cannot figure out how to tell Ansible to either:

  • ignore a check for existence of a role
  • run a ansible-galaxy command to automatically load role before using it

Here's my code:

- name: Install prerequisite roles
  hosts: all
  become: no

  tasks:
    - name: run ansible galaxy
      local_action: command ansible-galaxy install -r requirements.yml -p roles/

- name: Install Oracle Java
  hosts: tomcat-servers:ss-servers:ns-servers:ld-servers
  become: yes

  roles:
    - raltd-jdk

I tried doing the above using pre_tasks as well and that didn't work either. My requirements.yml is simply this:

- src: [email protected]:ansible/raltd-jdk.git
  scm: git

I'm getting this error:

$ ansible-playbook -i inv/myinventory raltd-jdk.yml
 ERROR! the role 'raltd-jdk' was not found in /path/to/ansible/roles:/etc/ansible/roles:/path/to/ansible

 The error appears to have been in '/path/to/ansible/raltd-jdk.yml': line 16, column 7, but may
 be elsewhere in the file depending on the exact syntax problem.

 The offending line appears to be:

   roles:
       - raltd-jdk
             ^ here

References

  • https://github.com/ansible/ansible/issues/22508
like image 662
slm Avatar asked Oct 30 '25 20:10

slm


1 Answers

I received the following response from the Ansible dev team.

@slmingol this particular error is by design. Roles and tasks are preloaded before any of them are executed. We have ongoing proposals that tackle this problem from different angles, but none of them have yet to be accepted or written.

He also referenced this proposal: Proposal: Auto Install Ansible Roles

To use the latest (or even a specific) version of a playbook with the appropriate roles, the following steps are typically required:

git pull upstream branch
ansible-galaxy install -r path/to/rolesfile.yml -p path/to/rolesdir -f
ansible-playbook run-the-playbook.yml

The most likely step in this process to be forgotten is the middle step. While we can improve processes and documentation to try and ensure that this step is not skipped, we can improve ansible-playbook so that the step is not required.

So it would appear this isn't possible until this proposal gets implemented.

References

  • https://github.com/ansible/proposals/issues/7
like image 86
slm Avatar answered Nov 02 '25 11:11

slm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!