問題描述
我目前正在查詢多個(gè)數(shù)據(jù)庫并捕獲查詢結(jié)果
Im currently querying multiple databases and capturing the results of the query
我這樣做的方式是,我編寫一個(gè)復(fù)制shell腳本的任務(wù),如下所示
The way Im doing it is, Im writing a task which copies a shell script, something like below
#!/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
在劇本中,我寫的任務(wù)如下:
In the playbook, Im writing the task as below:
- name: List Query [Host and DB]
shell: "/tmp/sqlscript/sql_select.sh {{item}} >> /tmp/sqlscript/output.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"
with_items: "{{ factor_dbs.split('\n') }}"
但是我注意到不同的主機(jī)有不同的 ORACLE_HOME 和 PATHS.如何在劇本中定義這些變量,以便任務(wù)選擇正確的 ORACLE_HOME 和 PATH 變量并成功執(zhí)行任務(wù)
However I have noticed that the different hosts have different ORACLE_HOME and PATHS. How can I define those variables in the playbook, so that the task picks the right ORACLE_HOME and PATH variables and execute the task successfully
推薦答案
您可以為每個(gè)主機(jī)定義特定于主機(jī)的變量.您可以編寫您的庫存文件,如:
you can define host specific variables for each of the hosts. You can write your inventory file like:
[is_hosts]
greenhat ORACLE_HOME=/tmp
localhost ORACLE_HOME=/sbin
類似于 PATH 變量
similarly for the PATH variable
那么你的任務(wù):
演示結(jié)果的示例劇本:
- hosts: is_hosts
gather_facts: false
vars:
tasks:
- name: task 1
shell: "env | grep -e PATH -e ORACLE_HOME"
environment:
# PATH: "{{ hostvars[inventory_hostname]['PATH']}}"
ORACLE_HOME: "{{ hostvars[inventory_hostname]['ORACLE_HOME'] }}"
register: shell_output
- name: print results
debug:
var: shell_output.stdout_lines
示例輸出,您可以看到 ORACLE_HOME 變量確實(shí)發(fā)生了變化,并且按照主機(jī)的定義:
sample output, you can see ORACLE_HOME variable was indeed changed, and as defined per host:
TASK [print results] ************************************************************************************************************************************************************************************************
ok: [greenhat] => {
"shell_output.stdout_lines": [
"ORACLE_HOME=/tmp",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
]
}
ok: [localhost] => {
"shell_output.stdout_lines": [
"ORACLE_HOME=/sbin",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
]
}
這篇關(guān)于使用 Ansible 設(shè)置不同的 ORACLE_HOME 和 PATH 環(huán)境變量的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!