issue: ansible ad-hoc command -u root and -u oracle with -a id giving same output

I am expecting “id” command to show the user details passed to “-u <user name”

“id” command to return details of root, oracle, and applmgr users on each run

but in below output is showing only one user for all, why ?

$ ansible -i hosts.ini db -u root -m shell -a id
host1 | SUCCESS | rc=0 >>
uid=500(oracle) gid=500(dba) groups=500(dba)

$ ansible -i hosts.ini db -u oracle -m shell -a id
host1 | SUCCESS | rc=0 >>
uid=500(oracle) gid=500(dba) groups=500(dba)

$ ansible -i hosts.ini db -u applmgr -m shell -a id
host1 | SUCCESS | rc=0 >>
uid=500(oracle) gid=500(dba) groups=500(dba)

$

Do you have ansible_user defined in your hosts.ini file? If so, the inventory var will win over the CLI value. See https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable

(attachments)

Thanks so much, it worked

however, still having issue with a specific group, let me re-review and see…

Thanks again and Regards,Naren

Provided my inventory file at the end

Below 3 commands are returning same output, that is, “applmgr” user’s information rather than their corresponding “ansible_user” info

  1. ansible -i test.ini db -m shell -a ‘id;pwd;ls;hostname’

  2. ansible -i test.ini mt -m shell -a ‘id;pwd;ls;hostname’

  3. ansible -i test.ini root -m shell -a ‘id;pwd;ls;hostname’

please see, what am i missing here ?

Below is my inventory file:

$ cat test.ini

[db]
host1 ansible_user=oracle

[root]
host1 ansible_user=root

[mt]
host1 ansible_user=applmgr

Continuation to previous updated issue…

What I observed is - ansible is always picking “last” group in the test.ini file

I have changed the order of groups, below is the complete contents of .ini file

$ cat test.ini

[root]
host1 ansible_user=root

[mt]
host1 ansible_user=applmgr

[db]
host1 ansible_user=oracle

all 3 commands show “oracle” user’s information rather than their corresponding user’s info

  1. ansible -i test.ini db -m shell -a ‘id;pwd;ls;hostname’

  2. ansible -i test.ini mt -m shell -a ‘id;pwd;ls;hostname’

  3. ansible -i test.ini root -m shell -a ‘id;pwd;ls;hostname’

Continuation to previous updated issue...

What I observed is - ansible is always picking "last" group in the test.ini
file

If a single host is in multiple groups, the last definition of a variable wins. When you specify a group to target, ansible internally converts it to a list of hosts. Once any groups have been resolved to hosts, ansible has no concept of which group any host was requested from.

I have changed the order of groups, below is the complete contents of .ini
file

$ cat test.ini
[root]
host1 ansible_user=root

root-host1 ansible_host=host1 ansible_user=root

[mt]
host1 ansible_user=applmgr

mt-host1 ansible_host=host1 ansible_user=applmgr

[db]
host1 ansible_user=oracle

Etc.

You could use such a workaround.

V/r,
James Cassell

Great, thank you for the explanation and the solution…

Thanks again and Regards,
Naren