# become\_user and with\_items

**URL:** <https://forum.ansible.com/t/become-user-and-with-items/19111>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [August 25, 2015, 3:32pm UTC](https://forum.ansible.com/t/become-user-and-with-items/19111 "2015-08-25T15:32:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Serge\_Scevenels](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@Serge\_Scevenels](https://forum.ansible.com/u/Serge_Scevenels)\
**Post date:** [August 25, 2015, 3:32pm UTC](https://forum.ansible.com/t/become-user-and-with-items/19111/1 "2015-08-25T15:32:09Z")

</div>

Hi all,

I am trying to peform a series of tasks using with\_items at task level.

I would like to see the tasks executed using different sudo users, so that I define at task level “become\_user”.

As an example, this is my setup:

roles/test/vars/main.yml

user\_info:

- name: ubuntu
- name: vagrant

roles/test/tasks/main.yml

- debug: msg=“{{item}}”  
become\_user: “{{ item.name }}”  
with\_items: user\_info

- name: test  
become\_user: “{{ item.name }}”  
become: yes  
command: mkdir ~/testdir  
when: item.get(‘state’, ‘present’) == ‘present’  
with\_items: user\_info

playbooks/labs.yml

- name: Deploy Common Configuration

hosts: deploy

gather\_facts: false

roles:

- { role: test }

Unfortunately, this does not work.

The debug task receive correctly the two items of the user\_info variable :

ok: [fisher] =\> (item={u’name’: u’ubuntu’}) =\> {

“item”: {

“name”: “ubuntu”

},

“msg”: {

“name”: “ubuntu”

}

}

ok: [fisher] =\> (item={u’name’: u’vagrant’}) =\> {

“item”: {

“name”: “vagrant”

},

“msg”: {

“name”: “vagrant”

}

}

But unfortunately, the task “test” is run two times using only the first item as become\_user

failed: [fisher] =\> (item={u’name’: u’ubuntu’}) =\> {“changed”: true, “cmd”: [“mkdir”, “~/testdir”], “delta”: “0:00:00.002188”, “end”: “2015-08-25 15:14:26.175761”, “failed”: true, “item”: {“name”: “ubuntu”}, “rc”: 1, “start”: “2015-08-25 15:14:26.173573”, “stderr”: "mkdir: cannot create directory **‘/home/ubuntu/testdir’: File exists",** “stdout”: “”, “stdout\_lines”: , “warnings”: [“Consider using file module with state=directory rather than running mkdir”]}

[WARNING]: Consider using file module with state=directory rather than running mkdir

failed: [fisher] =\> (item={u’name’: u’vagrant’}) =\> {“changed”: true, “cmd”: [“mkdir”, “~/testdir”], “delta”: “0:00:00.002270”, “end”: “2015-08-25 15:14:26.436281”, “failed”: true, “item”: {“name”: “vagrant”}, “rc”: 1, “start”: “2015-08-25 15:14:26.434011”, “stderr”: "mkdir: cannot create directory **‘/home/ubuntu/testdir’: File exists",** “stdout”: “”, “stdout\_lines”: , “warnings”: [“Consider using file module with state=directory rather than running mkdir”]}

Is it normal behaviour ?

I a currently running on Ansible 2.0 devel version… commit:

---

<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:** [August 27, 2015, 11:23pm UTC](https://forum.ansible.com/t/become-user-and-with-items/19111/2 "2015-08-27T23:23:16Z")

</div>

run with -vvvv, i suspect the become\_user is happening correctly but  
that you have sudo conserving $HOME and both are trying to create the  
same dir.
