Eric
Eric

Reputation: 2705

Switching user per role

I am writing an Ansible playbook.

It has a series of commands that run as root, followed by a series of commands that run as another user (call it user1).

I know I can switch to user1 by attaching

become: yes
become_user: user1

at the end of every command that runs as user1, but this will make the playbook unnecessarily lengthy and ugly.

Can I switch to user1 and run all tasks as user1 afterwards?

Upvotes: 0

Views: 249

Answers (1)

Dave Konopka
Dave Konopka

Reputation: 1255

You can use a block in 2.x to run multiple tasks as a particular user:

- block:
    - task1
    - task2
    - task3
  become: yes
  become_user: user1

- task4

You can also set become_user for a role's tasks:

- hosts: localhost
  connection: local

  roles:
    - role: some-role
      become: true
      become_user: user1

Upvotes: 1

Related Questions