問題描述
我嘗試在使用 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.
這篇關于盡管在 Ansible 中提升了權限,但仍無法運行腳本的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!