尽管在 Ansible 中提升了权限,但仍无法运行脚本

2021-11-11 00:00:00 shell ansible oracle sqlplus ansible-2.x

我尝试在使用 ansible 将它复制到主机上后在主机上运行它.该脚本有 777 个权限.

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

使用 Ansible 设置不同的 ORACLE_HOME 和 PATH 环境变量

- 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') }}"

下面是shell脚本

#!/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') }}"

然而,这个剧本需要在具有不同路径和 oracle_home 变量的不同主机上运行.

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

我的问题是,为什么尽管升级了权限,任务仍然运行.当我尝试通过登录服务器并在执行sudo su oracle"后手动运行相同的脚本时,它似乎运行良好.

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.

推荐答案

这取决于您实际设置环境变量的位置.在远程机器上登录时执行脚本与通过 ssh 像 Ansible 一样运行脚本是有区别的(参见例如 区分交互式登录和非交互式非登录外壳).根据 shell 的类型和您的系统,加载不同的 bash 配置文件.

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.

相关文章