Set different ORACLE_HOME and PATH environment variable using Ansible

余生颓废 提交于 2020-02-06 08:23:30

问题


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:


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"
    ]
}


来源:https://stackoverflow.com/questions/50224408/set-different-oracle-home-and-path-environment-variable-using-ansible

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