3

How do you become a user when using a role in ansible?

For example, how would I install nodejs as root using the geerlingguy.nodejs role from ansible galaxy? Here is the relevant portion of what I have:

roles:
  - geerlingguy.nodejs
    become: yes

But that produces ERROR! Syntax Error while loading YAML on the YAML file.

techraf
  • 64,883
  • 27
  • 193
  • 198
John Cummings
  • 1,949
  • 3
  • 22
  • 38

1 Answers1

7

I finally found a solution, which in retrospect is obvious from the documentation

roles:
    - role: geerlingguy.nodejs
      become: yes

This can also be written as:

roles:
    - { role: geerlingguy.nodejs, become: yes }

The part that was not obvious before to this ansible newbie was the alternate json-like syntax used in the documentation when it states "Also, should you wish to parameterize roles, by adding variables, you can do so, like this:"

- hosts: webservers
  roles:
    - common
    - { role: foo_app_instance, dir: '/opt/a',  app_port: 5000 }

I spent way too long figuring this out, so hopefully this answer will help someone else.

John Cummings
  • 1,949
  • 3
  • 22
  • 38
  • 1
    Warning: as noted [here](https://github.com/ansible/proposals/issues/126#issuecomment-402790323), setting become on a role is not limited to that role and bleeds over into subsiquent roles. Definitely not the intent that I wanted when I was trying to do the same thing as the original question. – bryon Jun 14 '20 at 00:04
  • 1
    In fact that comment in the Github issue relates to using become on tasks, not roles. If you set become: yes on one role, the following one doesn't automatically get it. Just tried it while looking at this SO question and thought I would clarify that. – Renaud Martinet Oct 07 '22 at 21:33