# switch to all the users and run the same command

**URL:** <https://forum.ansible.com/t/switch-to-all-the-users-and-run-the-same-command/17418>\
**Category:** Ansible Project\
**Created:** [February 2, 2015, 4:49pm UTC](https://forum.ansible.com/t/switch-to-all-the-users-and-run-the-same-command/17418 "2015-02-02T16:49:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [February 2, 2015, 4:49pm UTC](https://forum.ansible.com/t/switch-to-all-the-users-and-run-the-same-command/17418/1 "2015-02-02T16:49:58Z")

</div>

Hi I am new to ansible and stuck here, where i want to run this same command but using the each user from the list.

for example:

in vars/main.yml  
vpc\_users:

- abc
- xyz
- qes

in tasks/main.yml

- name: VNC | vncserver command  
shell: vncserver \</tmp/file \>/tmp/vncpasswd.1 2\>/tmp/vncpasswd.2

remote\_user/sudo\_user or somethine else?

with\_items:  
vnc\_users

Thanks in advance

---

<div class="post-metadata">

**Author:** ![michael1](https://avatars.discourse-cdn.com/v4/letter/m/eada6e/32.png) [@michael1](https://forum.ansible.com/u/michael1)\
**Post date:** [February 3, 2015, 2:23pm UTC](https://forum.ansible.com/t/switch-to-all-the-users-and-run-the-same-command/17418/2 "2015-02-03T14:23:24Z")

</div>

Hi,

Are you saying to want to run the ‘vncserver’ command as each user? Does this work:

`

- name: VNC | vncserver command  
shell: sudo -u {{item}} vncserver \</tmp/file \>/tmp/vncpasswd.1 2\>/tmp/vncpasswd.2  
with\_items:  
vnc\_users  
`

What have you tried so far, and what were the results?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [February 3, 2015, 5:32pm UTC](https://forum.ansible.com/t/switch-to-all-the-users-and-run-the-same-command/17418/3 "2015-02-03T17:32:35Z")

</div>

Thanks, I have tried this and it worked partially. partially mean that for each user in the list, I need to setup the ssh key plus sudo access (I have tried it even without sudo but didn’t work) but for some user I don’t want to give sudo access but if I didn’t give sudo access then the task fail ☹

`

- name: vncserver command  
shell: vncserver \</tmp/file \>/tmp/vncpasswd.1 2\>/tmp/vncpasswd.2  
remote\_user: “{{ item }}”  
sudo: False  
with\_items:  
vnc\_users  
`

Thanks in advance

---

<div class="post-metadata">

**Author:** ![Mark\_McCoy](https://avatars.discourse-cdn.com/v4/letter/m/ebca7d/32.png) [@Mark\_McCoy](https://forum.ansible.com/u/Mark_McCoy)\
**Post date:** [February 3, 2015, 5:55pm UTC](https://forum.ansible.com/t/switch-to-all-the-users-and-run-the-same-command/17418/4 "2015-02-03T17:55:21Z")

</div>

I think if fails because the command runs as the first user (or as root) and subsequent users can write to the output files in /tmp. Try this:

- name: vncserver command  
shell: vncserver \</tmp/file \>“/tmp/vncpasswd.{{item}}.1” 2\>“/tmp/vncpasswd.{{item}}.2”  
remote\_user: “{{ item }}”  
sudo: False  
with\_items:  
vnc\_users

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [February 4, 2015, 10:50am UTC](https://forum.ansible.com/t/switch-to-all-the-users-and-run-the-same-command/17418/5 "2015-02-04T10:50:57Z")

</div>

Thanks Mark,

This trick worked for me:

`- name: vncserver command`  
`shell: sudo -u {{ item.name }} -i vncserver </tmp/file >/tmp/vncpasswd.1 2>/tmp/vncpasswd.2`  
`with_items:`  
`vnc_users`

The modification with your tick is only “i”:

`-i [command]`  
`The -i (simulate initial login) option runs the shell specified in the passwd(5)`  
`entry of the target user as a login shell. This means that login-specific resource`  
`files such as .profile or .login will be read by the shell. If a command is`  
`specified, it is passed to the shell for execution. Otherwise, an interactive shell`  
`is executed. sudo attempts to change to that user's home directory before running`  
`the shell. It also initializes the environment, leaving DISPLAY and TERM unchanged,`  
`setting HOME, MAIL, SHELL, USER, LOGNAME, and PATH, as well as the contents of`  
`/etc/environment on Linux and AIX systems. All other environment variables are`  
`removed.`
