Pranjal Mittal
Pranjal Mittal

Reputation: 11422

ansible become_user for a set of tasks

Currently for a given task ansible-2.x provides an option called become_user that allows changing the user before the task in context is run. For example:

- name: Run Django database migrations
  django_manage:
    command: migrate
    app_path: "{{ project_path }}"
    settings: "{{ django_settings_file }}"
  become_user: "{{ project_user }}"

The become_user here only applies to the task in context and does not apply to all subsequent tasks that are run after this unless become_user is used in those tasks also explicitly. How do we become another user for a set of tasks/subsequent tasks following a given task in playbook?

Note: I can think of one way of doing this, which is; to refactor out these set of subsequent tasks into a new role and apply that new role on a single task with become_user set as desired. Is there any other alternative/simpler way of changing active user for subsequent tasks without putting these tasks into a separate role?

Upvotes: 7

Views: 6696

Answers (1)

udondan
udondan

Reputation: 59989

This can be done with the blocks feature, introduced in Ansible 2:

- block:
    - task 1
    - task 2
    - task N
  become_user: ...

Upvotes: 19

Related Questions