1

How do I install an ansible role using ansible?

The manual way is using the command line:

ansible-galaxy install user.role

But how do I do it in ansible itself, like a module? I would guess something like this in a playbook:

- tasks:
  galaxy:
    user: username
    role: rolename
    state: installed

This seems to be a very trivial and elementary task, but I can't find how to do it.

There is a similar question here How to automatically install Ansible Galaxy roles? .But it doesn't answer it, as it just runs the manual command every time, and it is not idempotent (that is the main reason to use ansible to automate stuff).

Allan Deamon
  • 418
  • 6
  • 18

2 Answers2

2

Your conclusion "it just runs the manual command every time, and it is not idempotent" was too quick. Such a task is idempotent with the parameter creates and appropriate ansible_roles_list.

- name: Install roles from Ansible Galaxy
  command: "ansible-galaxy install {{ item.role_name }}"
  args:
    creates: "{{ item.role_path }}"
  loop: "{{ ansible_roles_list }}"
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
0

The main point to this question that is not so obvious to novices and not easily found in the introductory documentation is that the galaxy's roles just need to be installed in the client, not on the server.

So in a playbook for a server you don't have to install a galay role every time you setup a server, but only once in you client machine.

Allan Deamon
  • 418
  • 6
  • 18
  • Ansible is very flexible. The roles are installed quite often also on servers. For example to let the playbooks run with [ansible-pull](https://docs.ansible.com/ansible/2.4/ansible-pull.html#ansible-pull). – Vladimir Botka Jun 18 '19 at 22:32