Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 641
Pranjal Mittal Avatar asked May 22 '16 09:05

Pranjal Mittal


1 Answers

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

- block:
    - task 1
    - task 2
    - task N
  become_user: ...
like image 150
udondan Avatar answered Sep 28 '22 07:09

udondan