Ansible: Run command as different user but not with sudo

Solution 1:

Solution:

After several different approaches I got it working with the following configuration:

The variables in the inventory:

ansible_connection: ssh
ansible_user: myuser
ansible_ssh_pass: 1234
ansible_become: yes
ansible_become_user: root
ansible_become_pass: 1234

in the playbook:

- hosts: myhost
  connection: local

  roles:
    - role: another-role
    - { role: pulse-audio-config,
      ansible_become_user: nas }
    - role: another-other-role

in the role:

- name: restart pulse audio
  shell: '$([[ $(pulseaudio -k) -eq 0 ]] || exit 0; sleep 5; [[ $(pulseaudio -D --exit-idle-time=-1) -eq 0 ]] || exit 0)'
  args:
    executable: /bin/bash
  become: true
  become_method: sudo
  become_flags: "su - {{ ansible_become_user }} -c"