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?
This can be done with the blocks feature, introduced in Ansible 2:
- block:
- task 1
- task 2
- task N
become_user: ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With