title: How to switch a user per task or set of tasks?
body: <p>A recurring theme that's in my ansible playbooks is that I often must execute a command with sudo privileges (<code>sudo: yes</code>) because I'd like to do it for a certain user. Ideally I'd much rather use sudo to switch to that user and execute the commands normally. Because then I won't have to do my usual post commands clean up such as chowning directories. Here's a snippet from one of my playbooks:</p>
  <pre class="lang-yaml prettyprint-override"><code>- name: checkout repo
    git: repo=https://github.com/some/repo.git version=master dest={{ dst }}
    sudo: yes
  - name: change perms
    file: dest={{ dst }} state=directory mode=0755 owner=some_user
    sudo: yes
  </code></pre>
  <p>Ideally I could run commands or sets of commands as a different user even if it requires sudo to su to that user.</p>