# Run a handler as a different user

**URL:** <https://forum.ansible.com/t/run-a-handler-as-a-different-user/22626>\
**Category:** Ansible Project\
**Created:** [September 23, 2016, 1:57pm UTC](https://forum.ansible.com/t/run-a-handler-as-a-different-user/22626 "2016-09-23T13:57:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gordon\_Ross](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@Gordon\_Ross](https://forum.ansible.com/u/Gordon_Ross)\
**Post date:** [September 23, 2016, 1:57pm UTC](https://forum.ansible.com/t/run-a-handler-as-a-different-user/22626/1 "2016-09-23T13:57:46Z")

</div>

I have a handler that I wish to run as a different user to the user Ansible normal logs in as.

My playbooks execute with the “—become” flag so they run as root, but this particular task needs to run as the application user.

I’m my handlers/main.yaml , I’ve got the entry:

- name: Update Rancid RCS files  
command: /usr/lib/rancid/bin/rancid-cvs  
become\_user: rancid  
become: yes

But Ansible still tries to run the command as root. (The command refuses to run as root)

If I change the handler to:

- name: Update Rancid RCS files  
action: command /usr/lib/rancid/bin/rancid-cvs  
remote\_user: rancid  
become: no

And setup the necessary SSH keys, the command runs fine as the correct user.

What am I doing wrong that’s preventing the first method from working?

I’m running Ansible 2.1.1.0

Thank you,

GTG

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [September 23, 2016, 3:32pm UTC](https://forum.ansible.com/t/run-a-handler-as-a-different-user/22626/2 "2016-09-23T15:32:37Z")

</div>

The difference is that in the first case you are trying to login as your ‘normal’ user and then sudo/su to the rancid user and that does not seem to work.

The second will login as the rancid user and not use sudo/su at all.

Without any errors I can only speculate that why this works/doesn’t seems to be related to how your permissions are setup.

---

<div class="post-metadata">

**Author:** ![Gordon\_Ross](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@Gordon\_Ross](https://forum.ansible.com/u/Gordon_Ross)\
**Post date:** [September 23, 2016, 7:11pm UTC](https://forum.ansible.com/t/run-a-handler-as-a-different-user/22626/3 "2016-09-23T19:11:19Z")

</div>

Running Ansible with verbose:

\<mgmtsrv1\> ESTABLISH SSH CONNECTION FOR USER: ansible  
\<mgmtsrv1\> SSH: EXEC ssh -C -q -o ControlMaster=auto -o ControlPersist=60s -o 'IdentityFile="/etc/ansible/ansible\_rsa"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ansible -o ConnectTimeout=10 -o ControlPath=/home/gr306/.ansible/cp/ansible-ssh-%h-%p-%r mgmtsrv1 '/bin/sh -c '"'"'sudo -H -S -n -u root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-reldgrhxvqzvmhecqvgicikpadbwqkct; LANG=en\_GB.UTF-8 LC\_ALL=en\_GB.UTF-8 LC\_MESSAGES=en\_GB.UTF-8 /usr/bin/python'"'"'"'"'"'"'"'"' && sleep 0'"'”’'

GTG
