ansible question

May be this question is so basic, frustrating me not in any document explains this scenario. advance thanks for your help .

Ansible not loading the become_user profile . this may look like small issue but i want to run some scripts in become_user only . (acutually become_user not loading its user profiles )

there is a difference for commands in linux
when you are in root user:
su : it will load only /root home directory
sudo su : it will load only /root home directory
su - : it will load /home/ directory. //do we have this option in ansible

but in ansible : if we do below, it never runs the profile
become: yes
become_user:
become_method: sudo

code example:


  • name: Este setup
    hosts: server
    gather_facts: no
    become: yes
    become_user: raju
    become_method: sudo // with this or without this same result

tasks:

  • name: executing script

shell: pwd
register: out

  • debug: var=out

TASK [debug] *******************************************************************
ok: [server] => {
“out”: {
“changed”: true,
“cmd”: “pwd”,
“delta”: “0:00:00.003830”,
“end”: “2018-01-15 13:19:40.438390”,
“failed”: false,
“rc”: 0,
“start”: “2018-01-15 13:19:40.434560”,
“stderr”: “”,
“stderr_lines”: ,
“stdout”: “/root”,
“stdout_lines”: [
“/root”
]
}
}

—versions info

ansible 2.4.1.0
config file = /etc/ansible/ansible.cfg
configured module search path = [u’/root/.ansible/plugins/modules’, u’/usr/share/ansible/plugins/modules’]
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Aug 4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]

CentOS Linux release 7.4.1708 (Core)

Most shell have to modes, interactive shell (when you log in and get a prompt) and non-interactive shell (when you run a script).

The two modes read different environment and profile files.
If you login to a server and execute "env" and if you run "ssh <username>@<hostname> env" you'll see that you get different result.

Ansible by default uses /bin/sh, and when you log in you probably use bash and they will probably differ.

To read more about interactive/non-interactive shell search the archives, read man bash or search the Internet.