I have two users under my remote host server : firas , michel
In my playbook , i'm connectiong as "firas" and am trying to execute some task with "michel" user
(therefore , to just assert it , i'm trying to display the user who is running the task in each time").
My host file looks like this :
[myHost]
server1
[myHost:vars]
ansible_user=firas
My tasks are the following :
- name: switch to user michel and run whoami
shell: whoami
remote_user: michel
register: username_michel
when:
- ansible_host in groups['myHost']
- debug:
msg: "user supposed to be michel is ; {{username_michel.stdout}}"
when:
- ansible_host in groups['myHost']
- name: switch to user firas and run whoami
shell: whoami
remote_user: firas
register: username_firas
when:
- ansible_host in groups['myHost']
- debug:
msg: "user supposed to be adi is ; {{username_firas.stdout}}"
when:
- ansible_host in groups['myHost']
-> but the strange that , like this : i obtain always the user "firas" as the active user
And seems that switching user is not running
any ideas?
The connection variable you defined in the inventory ansible_user=firas
has precedence over remote_user: michel
declaration in the task. Remove it.
Per explanation in Possible Misunderstanding:
As documented http://docs.ansible.com/ansible/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable, the connection variables have precedence over directives, this is done as host connection information is considered more 'specific' than play connection information. You can still override this from the play by setting the connection variable (as you would any variable).