Ansible – become_user and include_tasks modification since 2.5
There is a modification since Ansible 2.5 with the way that include_tasks works with become_user
Before Ansible 2.5, your code like this one works fine, you will have the Sylvie user id
--- - hosts: localhost gather_facts: no tasks: - name: test include_tasks: test_include_id.yml become: yes become_user: sylvie
--- - shell: id
Since Ansible 2.5 a change in Dynamic includes and attribute inheritance was implemented causing this previous playbook to show your user id instead of Sylvie user id.
Since Ansible 2.5 use this syntax with a block to have the become_user works
--- - hosts: localhost gather_facts: no tasks: - block: - name: test include_tasks: test_include_id.yml become: yes become_user: sylvie
Please follow and like us:
Invalid in Ansible 2.7 🙁
Are you sure ?
I have just tested it and it works with Ansible 2.7.8, without block, my id, with block, the become user id.
test_include.yml:
test_include_id.yml:
Running the test :
Actually I’ve tried the same code but it keeps the user I’ve connected with, maybe it’s bound to the way I’ve done my whole project.
But I’ve found another solution using the new syntax of 2.7 (2.7.4 if I remember right), I’ve tried it with a loop :
– include_tasks:
file: myfile.yml
apply:
become: yes
become_user: “{{my_user}}”
loop: “{{ my_list }}”
loop_control:
loop_var: my_user
With this piece of code, I get the right user.
Sure, the apply syntax could help you to solve this problem as it apply the arguments inside the include yaml file, tag including.
https://docs.ansible.com/ansible/latest/modules/include_tasks_module.html