When i execute the command "who am i" using ansible's shell module, i get an empty response, i can't figure out why ?

  hosts: server1
  remote_user: devops
  become: true
    - name: print current user
      shell: who am i
      register: userr
    - debug:
        msg: "{{ userr.stdout }}"

[root@ansible test]# ansible-playbook test.yml 

PLAY [test playbook] ********************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************
ok: [server1]

TASK [print current user] ***************************************************************************************************************************
changed: [server1]

TASK [debug] ****************************************************************************************************************************************
ok: [server1] => {
    "msg": ""

PLAY RECAP ******************************************************************************************************************************************
server1                    : ok=3    changed=1    unreachable=0    failed=0 
  • 2
    shell is a valid ansible module yes. i do not want to replace it with the command module because i plan to use piping afterwards. but even using the command module, i get the same empty output. – Hamdi Ghodbane Jan 7 '18 at 14:01
  • @Jesse_b Have a look here: docs.ansible.com/ansible/latest/shell_module.html I use the shell module a lot as I find it more flexible than command. – maulinglawns Jan 7 '18 at 14:30

The reason i think is that ansible is not using a login shell to execute the command. So the command is not displaying anything.

If you replace: 

who am i


su - anotheruser; who am i

You should see an output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.