How to automatically install Ansible Galaxy roles?

All my downloads / roles in Ansible are marked in my git registry.

However, for the Ansible Galaxy role, I always have to explicitly download them, one on each machine with which I want to run Ansible.

It's even hard to know in advance which Ansible Galaxy roles are needed until Ansible complains about the missing role at runtime.

How can I manage the dependencies of the Ansible Galaxy role? I would like to either check them in my git registry along with the rest of my code, or automatically identify and download them when I run Ansible on a new computer.

+99
ansible ansible-playbook ansible-galaxy
Aug 10 '14 at
source share
6 answers

You should use the requirements.yml file for this use case. Describe the roles you need using any of the installation methods:

 # Install a role from the Ansible Galaxy - src: dfarrell07.opendaylight # Install a role from GitHub - name: opendaylight src: https://github.com/dfarrell07/ansible-opendaylight # Install a role from a specific git branch - name: opendaylight src: https://github.com/dfarrell07/ansible-opendaylight version: origin/master # Install a role at a specific tag from GitHub - name: opendaylight src: https://github.com/dfarrell07/ansible-opendaylight version: 1.0.0 # Install a role at a specific commit from GitHub - name: opendaylight src: https://github.com/dfarrell07/ansible-opendaylight version: <commit hash> 

Then install them:

 ansible-galaxy install -r requirements.yml 

Here's a working example (installing OpenDaylight using Ansible as a device for strollers). See the relevant Ansible documents for more information.

+124
May 11 '15 at 20:01
source share

As expected, you can use the ANSIBLE galaxy for this purpose.

Anzibl has a function where you can create a requirements.yml file that lists all your roles. You can find out about it here: http://docs.ansible.com/ansible/latest/galaxy.html#install-multiple-roles-from-a-file

For example (needs.yml):

 - src: yatesr.timezone 

Then you run ansible-galaxy install -r requirements.yml for this file to download all the roles listed there.

If you want to further automate it, you can create a simple shell script that will run two commands.

For example (ansible.sh):

./ansible.sh

 ansible-galaxy install -r requirements.yml ansible-playbook playbook.yml -i inventory 
+44
Jan 07 '16 at 23:30
source share

I often install the Java JDK installation. Using a role makes this touch easier. I tried several different ways (including many .gitmodules and submodules ... I need to use several git systems to work, and it all gets ugly). My biggest requirement is that I will not check the role code in my playbook project, basically, so I can keep everything in one place.

The contents of my requirements.yml file:

 - src: https://github.com/staylorx/ansible-role-wls-prep.git version: master name: staylorx.wls-prep - src: https://my-work-git-extravaganza.com version: 2.x name: coolplace.niftyrole #From Ansible Galaxy - src: staylorx.oracle-jdk 

I run a separate playbook, install-role.yml:

 --- - hosts: localhost tasks: - file: path: roles state: absent - local_action: command ansible-galaxy install -r requirements.yml --roles-path roles - lineinfile: dest: .gitignore regexp: '^\/roles$' line: '/roles' state: present 

I launch this first play, then I launch my roles in any textbook. For me, the secret is to ignore its git, so I am not checking roles by mistake. In addition, since I destroy the folder every time, I guarantee that I do not need to force or ignore errors.

+16
Feb 10 '16 at 15:27
source share

Another solution is to use git submodules. After all, Ansible Galaxy is just a github repository directory ...

I use this command to automatically add any Galaxy role as a submodule:

 ansible-galaxy info <package> | grep -A 1 github_repo | tr '\n' ' ' | sed -e "s/.*github_repo: \([^[:space:]]*\)[^\w]*github_user: \([^[:space:]]*\)[[:space:]]*/git submodule add git:\/\/github.com\/\2\/\1.git roles\/\2.\1/g" | sh 

Complete the changes, then to the git repository. When you clone your repo in the future, be sure to clone it with submodules, for example. git clone ... --recursive

The advantage of this is that the git submodule always refers to a specific version (git commit-hash). This will prevent unverified updates from starting in your production environment. The new version of the Galaxy role may have bugs or work completely different than before. With the git submodule, you decide when and when you will upgrade the role in the new version.

In addition, you don’t have to worry about blacklisting galaxies in your .gitignore to prevent their code from entering your repository.

+4
May 12 '15 at 9:07
source share

At the moment, as far as I know, there is no automatic way to load roles at runtime. It’s best to either transfer them to your own repo, or have proper documentation listing all the requirements. You can even create a pre-flight play that sets your roles. :)

+3
Aug 11 '14 at 4:38
source share

You can use the Ansible role to install other required roles using the command module .

Here is a very simple example that runs ansible-galaxy install :

 - name: Install roles from Ansible Galaxy command: ansible-galaxy install {{ item.item }} with_items: - "{{ ansible_roles_list }}" 

ansible_roles_list can be supplied as a variable or as a role parameter.

I have implemented a role for this, so you can use Ansible also to install (part) of the control machine: https://galaxy.ansible.com/ferrarimarco/install-roles/ .

This role must be applied before for any other roles that you want to install using it in a separate book. This is because Ansible checks to see if all roles are available before starting the tutorial where you reference them.

+3
Apr 04 '16 at 9:09
source share



All Articles