puravidaso
puravidaso

Reputation: 1223

"who am i" in ansible and over ssh outputs nothing

I have the following test code in ansible:

- shell: who am i
  register: w

- debug:
    msg:
      - "{{ w }}" 

which produces empty stdout as shown below:

ok: [10.20.30.40] => {
    "msg": [
        {
            "changed": true,
            "cmd": "who am i",
            "delta": "0:00:00.003400",
            "end": "2021-02-02 21:19:50.454729",
            "failed": false,
            "rc": 0,
            "start": "2021-02-02 21:19:50.451329",
            "stderr": "",
            "stderr_lines": [],
            "stdout": "",
            "stdout_lines": []
        }
    ]
}

The command works on some servers, but not work on some others. I could not figure out what the difference is. I can only guess it has to do with terminal info. When I type the "who am i" command interactively, I get what I expected.

I also noticed the other strange behavior in "who" command over ssh:

$ ssh myhost who       # who outputs nothing
$ ssh myhost who am i  # who am i outputs nothing
$ ssh myhost whoami    # whoami works
j.smith

Maybe they are related.

I need to use who am i in sudo command to find the user who issue the sudo. If there is another way to find the info, I would be glad to use that, but I still want to know what is happening with who am i. Thanks a lot.

Upvotes: 1

Views: 656

Answers (1)

that other guy
that other guy

Reputation: 123460

whoami shows the username of the current effective user.

who am i aka who -m shows the utmp record of whatever stdin is.

utmp records are generally added by login and various kinds of terminal emulators. If you don't create a login session, who am i will be blank:

$ ssh localhost who am i
(nothing)

$ ssh -t localhost who am i
me    pts/31       Feb  2 14:27 (127.0.0.1)

If you e.g. run screen with deflogin off, then who am i will always be blank. This is one reason why you should never use who am i to try to figure out who ran sudo (use $SUDO_USER instead).

Upvotes: 6

Related Questions