# shell module with become\_user does not function correctly

**URL:** <https://forum.ansible.com/t/shell-module-with-become-user-does-not-function-correctly/38475>\
**Category:** Ansible Project\
**Created:** [September 27, 2023, 8:52pm UTC](https://forum.ansible.com/t/shell-module-with-become-user-does-not-function-correctly/38475 "2023-09-27T20:52:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tlemons](https://avatars.discourse-cdn.com/v4/letter/t/edb3f5/32.png) [@tlemons](https://forum.ansible.com/u/tlemons)\
**Post date:** [September 27, 2023, 8:52pm UTC](https://forum.ansible.com/t/shell-module-with-become-user-does-not-function-correctly/38475/1 "2023-09-27T20:52:09Z")

</div>

Hi - I’m having trouble using the shell module with become\_user. As a basic test, I created this playbook:

- name: Retrieve environment variables for root  
shell: printenv

- name: Retrieve environment variables for oracle  
shell: printenv  
become: yes  
become\_user: oracle

When I ran this with ansible-playbook and ‘-vvv’, I noticed:

- the output received from the first task was as expected:

“stdout”: “HOSTTYPE=x86\_64\nSSH\_CONNECTION=10.247.229.46 35330 10.247.229.191 22\nLESSCLOSE=lessclose.sh %s %s\nXKEYSYMDB=/usr/X11R6/lib/X11/XKeysymDB\n\_=/usr/bin/printenv\nLANG=POSIX\nWINDOWMANAGER=xterm\nLESS=-M -I -R\nHOSTNAME=ldpdd191\nCSHEDIT=emacs\nGPG\_TTY=/dev/pts/0\nLESS\_ADVANCED\_PREPROCESSOR=no\nCOLORTERM=1\nMACHTYPE=x86\_64-suse-linux\nMINICOM=-c on\nOSTYPE=linux\nXDG\_SESSION\_ID=69\nUSER=root\nPAGER=less\nMORE=-sl\nPWD=/root\nHOME=/root\nLC\_CTYPE=C.UTF-8\nHOST=ldpdd191\nSSH\_CLIENT=10.247.229.46 35330 22\nXNLSPATH=/usr/X11R6/lib/X11/nls\nXDG\_SESSION\_TYPE=tty\nXDG\_DATA\_DIRS=/usr/share\nLIBGL\_DEBUG=quiet\nPROFILEREAD=true\nSSH\_TTY=/dev/pts/0\nFROM\_HEADER=\nMAIL=/var/spool/mail/root\nLESSKEY=/etc/lesskey.bin\nTERM=xterm\nSHELL=/bin/bash\nXDG\_SESSION\_CLASS=user\nPYTHONSTARTUP=/etc/pythonstart\nSHLVL=3\nMANPATH=/usr/share/man:/usr/local/man\nLOGNAME=root\nDBUS\_SESSION\_BUS\_ADDRESS=unix:path=/run/user/0/bus\nXDG\_RUNTIME\_DIR=/run/user/0\nXDG\_CONFIG\_DIRS=/etc/xdg\nPATH=/sbin:/usr/sbin:/usr/local/sbin:/root/bin:/usr/local/bin:/usr/bin:/bin\nG\_BROKEN\_FILENAMES=1\nHISTSIZE=1000\nCPU=x86\_64\nSSH\_SENDS\_LOCALE=yes\nLESSOPEN=lessopen.sh %s”,

but the output received from the second task (which uses ‘become\_user’) was not correct:

“stdout”: “\_=/usr/bin/printenv\nLANG=POSIX\nSUDO\_GID=0\nCOLORTERM=1\nSUDO\_COMMAND=/bin/sh -c echo BECOME-SUCCESS-ukrwuqlueafnghzqqoabhpfcwxwpieyw ; /usr/bin/python3.6 /var/tmp/ansible-tmp-1695847065.2341652-30706-3263662880779/AnsiballZ\_command.py\nUSER=oracle\nPWD=/home/oracle/.ansible/tmp/ansible-moduletmp-1695847065.5976799-u8hbo4o2\nHOME=/home/oracle\nLC\_CTYPE=C.UTF-8\nSUDO\_USER=root\nSUDO\_UID=0\nMAIL=/var/mail/oracle\nTERM=xterm\nSHELL=/bin/bash\nSHLVL=2\nLOGNAME=oracle\nPATH=/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/bin:/usr/local/sbin”,

so, it seem that using ‘become\_user’ perverts the function of the ‘shell’ module, and the command provided is not executed as expected. Is this a known bug/limitation?

Thanks!  
tl

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [September 28, 2023, 2:38am UTC](https://forum.ansible.com/t/shell-module-with-become-user-does-not-function-correctly/38475/2 "2023-09-28T02:38:56Z")

</div>

> Hi - I’m having trouble using the shell module with become\_user. As a basic test, I created this playbook:
> 
> - name: Retrieve environment variables for root  
> shell: printenv
> 
> - name: Retrieve environment variables for oracle  
> shell: printenv  
> become: yes  
> become\_user: oracle
> 
> When I ran this with ansible-playbook and ‘-vvv’, I noticed:
> 
> - the output received from the first task was as expected:
> 
> “stdout”: “HOSTTYPE=x86\_64\nSSH\_CONNECTION=10.247.229.46 35330 10.247.229.191 22\nLESSCLOSE=lessclose.sh %s %s\nXKEYSYMDB=/usr/X11R6/lib/X11/XKeysymDB\n\_=/usr/bin/printenv\nLANG=POSIX\nWINDOWMANAGER=xterm\nLESS=-M -I -R\nHOSTNAME=ldpdd191\nCSHEDIT=emacs\nGPG\_TTY=/dev/pts/0\nLESS\_ADVANCED\_PREPROCESSOR=no\nCOLORTERM=1\nMACHTYPE=x86\_64-suse-linux\nMINICOM=-c on\nOSTYPE=linux\nXDG\_SESSION\_ID=69\nUSER=root\nPAGER=less\nMORE=-sl\nPWD=/root\nHOME=/root\nLC\_CTYPE=C.UTF-8\nHOST=ldpdd191\nSSH\_CLIENT=10.247.229.46 35330 22\nXNLSPATH=/usr/X11R6/lib/X11/nls\nXDG\_SESSION\_TYPE=tty\nXDG\_DATA\_DIRS=/usr/share\nLIBGL\_DEBUG=quiet\nPROFILEREAD=true\nSSH\_TTY=/dev/pts/0\nFROM\_HEADER=\nMAIL=/var/spool/mail/root\nLESSKEY=/etc/lesskey.bin\nTERM=xterm\nSHELL=/bin/bash\nXDG\_SESSION\_CLASS=user\nPYTHONSTARTUP=/etc/pythonstart\nSHLVL=3\nMANPATH=/usr/share/man:/usr/local/man\nLOGNAME=root\nDBUS\_SESSION\_BUS\_ADDRESS=unix:path=/run/user/0/bus\nXDG\_RUNTIME\_DIR=/run/user/0\nXDG\_CONFIG\_DIRS=/etc/xdg\nPATH=/sbin:/usr/sbin:/usr/local/sbin:/root/bin:/usr/local/bin:/usr/bin:/bin\nG\_BROKEN\_FILENAMES=1\nHISTSIZE=1000\nCPU=x86\_64\nSSH\_SENDS\_LOCALE=yes\nLESSOPEN=lessopen.sh %s”,
> 
> but the output received from the second task (which uses ‘become\_user’) was not correct:
> 
> “stdout”: “\_=/usr/bin/printenv\nLANG=POSIX\nSUDO\_GID=0\nCOLORTERM=1\nSUDO\_COMMAND=/bin/sh -c echo BECOME-SUCCESS-ukrwuqlueafnghzqqoabhpfcwxwpieyw ; /usr/bin/python3.6 /var/tmp/ansible-tmp-1695847065.2341652-30706-3263662880779/AnsiballZ\_command.py\nUSER=oracle\nPWD=/home/oracle/.ansible/tmp/ansible-moduletmp-1695847065.5976799-u8hbo4o2\nHOME=/home/oracle\nLC\_CTYPE=C.UTF-8\nSUDO\_USER=root\nSUDO\_UID=0\nMAIL=/var/mail/oracle\nTERM=xterm\nSHELL=/bin/bash\nSHLVL=2\nLOGNAME=oracle\nPATH=/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/bin:/usr/local/sbin”,
> 
> so, it seem that using ‘become\_user’ perverts the function of the ‘shell’ module, and the command provided is not executed as expected.

What do you expect? And what do you mean by “perverts the function”?

Because it seems to work fine to me.

---

<div class="post-metadata">

**Author:** ![tlemons](https://avatars.discourse-cdn.com/v4/letter/t/edb3f5/32.png) [@tlemons](https://forum.ansible.com/u/tlemons)\
**Post date:** [September 28, 2023, 1:12pm UTC](https://forum.ansible.com/t/shell-module-with-become-user-does-not-function-correctly/38475/3 "2023-09-28T13:12:48Z")

</div>

Hi Dick

Sorry I was vague. The issue is that the ‘printenv’ output returned by the second task was incorrect. It should have been:

oracle@ldpdd191:~\> printenv  
LS\_COLORS=no=00:fi=00:di=01;34:ln=00;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=41;33;01:ex=00;32:_.cmd=00;32:_.exe=01;32:_.com=01;32:_.bat=01;32:_.btm=01;32:_.dll=01;32:_.tar=00;31:_.tbz=00;31:_.tgz=00;31:_.rpm=00;31:_.deb=00;31:_.arj=00;31:_.taz=00;31:_.lzh=00;31:_.lzma=00;31:_.zip=00;31:_.zoo=00;31:_.z=00;31:_.Z=00;31:_.gz=00;31:_.bz2=00;31:_.tb2=00;31:_.tz2=00;31:_.tbz2=00;31:_.xz=00;31:_.avi=01;35:_.bmp=01;35:_.dl=01;35:_.fli=01;35:_.gif=01;35:_.gl=01;35:_.jpg=01;35:_.jpeg=01;35:_.mkv=01;35:_.mng=01;35:_.mov=01;35:_.mp4=01;35:_.mpg=01;35:_.pcx=01;35:_.pbm=01;35:_.pgm=01;35:_.png=01;35:_.ppm=01;35:_.svg=01;35:_.tga=01;35:_.tif=01;35:_.webm=01;35:_.webp=01;35:_.wmv=01;35:_.xbm=01;35:_.xcf=01;35:_.xpm=01;35:_.aiff=00;32:_.ape=00;32:_.au=00;32:_.flac=00;32:_.m4a=00;32:_.mid=00;32:_.mp3=00;32:_.mpc=00;32:_.ogg=00;32:_.voc=00;32:_.wav=00;32:_.wma=00;32:\*.wv=00;32:  
HOSTTYPE=x86\_64  
LESSCLOSE=lessclose.sh %s %s  
XKEYSYMDB=/usr/X11R6/lib/X11/XKeysymDB  
ORACLE\_SID=orcl  
ORACLE\_BASE=/u01/app/oracle  
LANG=en\_US.UTF-8  
WINDOWMANAGER=xterm  
LESS=-M -I -R  
ORACLE\_HOME=/u01/app/oracle/product/21.0.0/dbhome\_1  
HOSTNAME=ldpdd191  
CSHEDIT=emacs  
GPG\_TTY=/dev/pts/1  
LESS\_ADVANCED\_PREPROCESSOR=no  
COLORTERM=1  
MACHTYPE=x86\_64-suse-linux  
MINICOM=-c on  
OSTYPE=linux  
USER=oracle  
PAGER=less  
MORE=-sl  
PWD=/home/oracle  
HOME=/home/oracle  
HOST=ldpdd191  
XNLSPATH=/usr/X11R6/lib/X11/nls  
XDG\_DATA\_DIRS=/usr/share  
PROFILEREAD=true  
ORA\_INVENTORY=/u01/app/oraInventory  
FROM\_HEADER=  
MAIL=/var/spool/mail/oracle  
LESSKEY=/etc/lesskey.bin  
TERM=xterm  
SHELL=/bin/bash  
LS\_OPTIONS=-N --color=tty -T 0  
PYTHONSTARTUP=/etc/pythonstart  
SHLVL=1  
G\_FILENAME\_ENCODING=@locale,UTF-8,ISO-8859-15,CP1252  
MANPATH=/usr/local/man:/usr/share/man  
LOGNAME=oracle  
XDG\_CONFIG\_DIRS=/etc/xdg  
PATH=/u01/app/oracle/product/21.0.0/dbhome\_1/bin:/u01/app/oracle/product/21.0.0/dbhome\_1/bin:/home/oracle/bin:/usr/local/bin:/usr/bin:/bin  
G\_BROKEN\_FILENAMES=1  
HISTSIZE=1000  
CPU=x86\_64  
LESSOPEN=lessopen.sh %s  
\_=/usr/bin/printenv  
oracle@ldpdd191:~\>

The STDOUT value for the second task does not show this output; instead, it show some information that is NOT the output of ‘printenv’. Is this expected?

Thanks  
tl

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [September 28, 2023, 6:39pm UTC](https://forum.ansible.com/t/shell-module-with-become-user-does-not-function-correctly/38475/4 "2023-09-28T18:39:11Z")

</div>

become does not always imply a full login nor sourcing .shell files,  
some of it depends on flags (`-i` for sudo or `-` for su), other times  
it depends on shell used.

---

<div class="post-metadata">

**Author:** ![tlemons](https://avatars.discourse-cdn.com/v4/letter/t/edb3f5/32.png) [@tlemons](https://forum.ansible.com/u/tlemons)\
**Post date:** [September 28, 2023, 9:16pm UTC](https://forum.ansible.com/t/shell-module-with-become-user-does-not-function-correctly/38475/5 "2023-09-28T21:16:47Z")

</div>

Hi Brian

Thanks very much for these hints. I did some more reading in [https://docs.ansible.com/ansible/latest/playbook\_guide/playbooks\_privilege\_escalation.html](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_privilege_escalation.html) and its linked pages. I found that adding:

become\_method: su

allowed ‘printenv’ to run correctly in the non-root account, and the further addition of:

become\_flags: ‘-’

allowed execution of .bash\_profile, allowing the environment variables to be set.

Thanks again  
tl
