Run multiple tasks with different Ansible users for Windows hosts

Hi all,

I have role that contains several tasks that should be run by different Ansible users. The playbook that uses this role should run on Windows hosts and I do this using a command similar to the following:


 ansible-playbook --limit windows -i hosts --extra-vars "ansible_user=my_username ansible_password=my_password" site.yaml


Most of the tasks in the role should be done through the user specified in the command above while others should use another Windows user. For these other tasks, I try to override the user credential variables as shown below:

`

  • include: task_for_other_windows_user.yml
    vars:
    ansible_user: “{{other_windows_user_name}}”
    ansible_password: “{{other_windows_user_pw}}”

`



This unfortunately doesn’t work as the playbook appears to use the same user initially defined, on all the plays. How do I implement what I’m trying to achieve here?

The problem is that vars set on the command-line have the highest possible precedence. If you were to set the initial value in inventory, or someplace else with a lower precedence than a play/task var, it’d work fine (verified).

See http://docs.ansible.com/ansible/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable for more details.

-Matt

(ie, vars set by --extra-vars on the command-line cannot be overridden)