Unable to run script despite escalating privilege in Ansible

天大地大妈咪最大 提交于 2019-12-25 00:12:37

问题


Im trying to run a shell script on the host machine after copying it over there using ansible. The script has 777 permissions.

Please read the below question as it gives the full scope of the actual issue that we are trying to deal with

Set different ORACLE_HOME and PATH environment variable using Ansible

- name: Run the Script [List]
  shell: "/tmp/sqlscript/sql_select.sh {{item}} >> /tmp/sqlscript/output.out"
  become: yes
  become_method: sudo
  become_user: oracle
  register: orh
  with_items: "{{ factor_dbs.split('\n') }}"

Below is the shell script

#!/bin/bash
source $HOME/bin/gsd_xenv $1 &> /dev/null

sqlplus -s <<EOF
/ as sysdba
set heading off


select d.name||','||i.instance_name||','||i.host_name||';' from v\$database d,v\$instance i;

EOF

Despite escalating the privileges, I observed that the task is not executing unless I add environment variables like below

- name: Run the script [List]
  shell: "/tmp/sqlscript/oracle_home.sh {{item}} >> /tmp/sqlscript/orahome.out"
  become: yes
  become_method: sudo
  become_user: oracle
  environment:
     PATH: "/home/oracle/bin:/usr/orasys/12.1.0.2r10/bin:/usr/bin:/bin:/usr/ucb:/sbin:/usr/sbin:/etc:/usr/local/bin:/oradata/epdmat/goldengate/config/sys"
     ORACLE_HOME: "/usr/orasys/12.1.0.2r10"
  register: orh
  with_items: "{{ factor_dbs.split('\n') }}"

However this playbook needs to be run across different hosts which have different path and oracle_home variables.

My question is, why doest the task run despite escalating the permissions. When I try to run the same script manually by logging into the server and after doing "sudo su oracle", it seems to be running fine.


回答1:


It depends on where you actually set your environment variables. There is a difference in executing a script when you are logged in at a remote machine, and running a script over ssh as Ansible does (see e.g., Differentiate Interactive login and non-interactive non-login shell). Depending on the type of shell and your system, different bash profiles are loaded.



来源:https://stackoverflow.com/questions/50299194/unable-to-run-script-despite-escalating-privilege-in-ansible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!