Set different ORACLE_HOME and PATH environment var

2019-08-30 22:51发布

Im currently querying multiple databases and capturing the results of the query

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

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

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

1条回答
贼婆χ
2楼-- · 2019-08-30 23:52

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

similarly for the PATH variable

then your task:

sample playbook that demonstrates the results:

- 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

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"
    ]
}
查看更多
登录 后发表回答