Am I understanding correctly, that you want to execute only some tasks with sudo and the rest without elevating privileges?
In that case, you can just put become: yes
into the tasks you want to execute with privileges. Remove the become: yes
at the top of your playbook.
Something like this.
- name: Check if a reboot is required.
ansible.builtin.stat:
path: /var/run/reboot-required
get_md5: no
register: reboot_required_file
- name: Reboot the server (if required).
ansible.builtin.reboot:
when: reboot_required_file.stat.exists == true
become: yes