执行Oracle脚本的Ansible行动手册

3vpjnl9f  于 2022-12-22  发布在  Oracle
关注(0)|答案(4)|浏览(198)

我正在尝试运行行动手册以执行Oracle脚本。

---
- hosts: localhost
- tasks: 
   - set_fact:
        execute_command: "sqlplus {{ Oracle_Username }}/{{ Oracle_Password }} @{{ sqlfile.sql }}"

   - name: Get Object_details 
     shell: "echo exit | {{ execute_command }} >> ./Oracle_Output.csv"
     environment:
        ORACLE_HOME: "{{ Oracle_DBServer }}"
        ORACLE_SID: "{{ Oracle_SID }}"

我已经声明了变量中的所有变量。当我执行它的时候,我得到错误“set_fact是一个播放的无效属性”。使用Ansible运行SQL脚本的最好方法是什么?我必须声明变量中的所有连接细节。

idv4meu8

idv4meu81#

---
- hosts: localhost
- tasks:
   - name: Get Object_details 
     shell: echo exit |sqlplus "{{ oracle_username }}/ {{ oracle_pwd}} @(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host={{ oracle_hostname }} )(Port={{ oracle_port }}))(CONNECT_DATA=(SERVICE_NAME= {{ service_name }})))"@sqlfile.sql;

使用上面的代码,我们将能够直接连接到Oracle主机并执行sql脚本。如果Oracle环境变量未默认定义,我们也可以在行动手册任务本身中进行设置。下面是相应的示例:

---
- hosts: localhost
- tasks:
   - name: Get Object_details 
     shell: echo exit |sqlplus "{{ oracle_username }}/ {{ oracle_pwd}} @(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host={{ oracle_hostname }} )(Port={{ oracle_port }}))(CONNECT_DATA=(SERVICE_NAME= {{ service_name }})))"@sqlfile.sql;
     environment:
          ORACLE_HOME: <<Oracle Home path >>
          PATH: << bin path >>
          LD_LIBRARY_PATH: << lib path >>
avkwfej4

avkwfej42#

您忘记声明tasks节:

---
- hosts: localhost
- tasks:
    - set_fact:
        execute_command: "sqlplus {{ Oracle_Username }}/{{ Oracle_Password }} @{{ sqlfile.sql }}"

    - name: Get Object_details 
      shell: "echo exit | {{ execute_command }} >> ./Oracle_Output.csv"
      environment:
        ORACLE_HOME: "{{ Oracle_DBServer }}"
        ORACLE_SID: "{{ Oracle_SID }}"

另外,我不知道任务的wait属性。

kq0g1dla

kq0g1dla3#

首先需要导出ORACLE_HOME,然后才能从oracle的bin文件夹调用sqlplus。

---
- hosts: localhost
- tasks:
    - name: Execute table.sql using sqlplus 
      shell: $ORACLE_HOME/bin/sqlplus -s username/password@connect @table.sql
      environment:
        ORACLE_HOME: "{{oracle_home_path}}"
        LD_LIBRARY_PATH: "{{ld_library_path}}"
        PATH: "{{bin_path}}"
      args:
        chdir: "{{sql_path}}" 
      become: true
      become_method: su
      become_user: oracle
s4n0splo

s4n0splo4#

- hosts: "{{ hosts }}"
  become: true
  become_user: oracle
  gather_facts: false
  vars:
    oracle_home: /product/u01/app/oracle/product/db/12.1.0.2/
  tasks:
   - name : NAME TASK
     shell: "export ORACLE_SID={{ base }}\n\
          export ORACLE_HOME={{ oracle_home }}\n\
          export PATH=$PATH:/product/u01/app/oracle/product/db/12.1.0.2/bin\n\
          /home/oracle/scripts_neodt/CREATE_USER.sh "

我这样做,它的罚款!

相关问题