become_user and with_items

742 views
Skip to first unread message

Serge Scevenels

unread,
Aug 25, 2015, 11:36:05 AM8/25/15
to Ansible Project
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:  



Brian Coca

unread,
Aug 27, 2015, 7:23:22 PM8/27/15
to Ansible Project
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.



--
Brian Coca
Reply all
Reply to author
Forward
0 new messages