roles with different users

Hello community,

I tried to change my setup to use roles now :slight_smile:

But I have a problem and can’t help myself.

My main.yml Playbook is like this:

  • hosts: foobar
    roles:
  • common
  • add_basic_software

Now the playbook is executed by my current logged in. That’s logical and not the point.

What I want to do is to execute the common role by user “foo” and the “add_basic_software” role by user “bar”. Is there any possibility to do that? I expect that in the roles/common/tasks/main.yml should be no user mentioned.

Do you have any suggestions how to realize this ?

Thank you in advance.

You’ll want two plays here. In the first one, add “user: foo” right under hosts. In the second, add “user: bar”.

  • hosts: blarg
    user: foo
    roles:

  • common

  • hosts: blarg
    user: bar
    roles:

  • add_basic_software

You can of course also just use one user and set sudo_user on a per task basis, if you wanted.

Awesome thanks - should imagine that by myself ^^

but blarg was just a placeholder and is in my playbook “{{ blarg }}”. I assign it via vars_prompt.

Is there a possibility to just do that one time in the first playbook? :slight_smile:

Thanks for your work and ansible.

"but blarg was just a placeholder and is in my playbook “{{ blarg }}”. I assign it via vars_prompt. "

IIRC, hosts gets evaluated before vars_prompt.

you can definitely use -e on the command line.