Ansible playbook执行Oracle脚本

时间:2017-01-22 21:41:44

标签: oracle ansible sqlplus

我正在尝试运行一个Playbook来执行Oracle Scripts。

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

我已经在vars中声明了所有变量。当我执行它时,我得到错误" set_fact不是播放的有效属性" 。 哪个是使用Ansible运行SQL脚本的最佳方法?我必须在变量中声明所有连接细节。

3 个答案:

答案 0 :(得分:2)

---
- 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 Host并执行sql脚本。如果默认情况下未定义Oracle环境变量,我们也可以在playbook任务本身中设置它。下面是一个例子:

---
- 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 >>

答案 1 :(得分:1)

您忘记声明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 }}"

P.S。而且我不知道任务的wait属性。

答案 2 :(得分:0)

您需要先导出ORACLE_HOME。然后,您可以从oracle的bin文件夹中调用sqlplus。

---
- hosts: localhost
- tasks:
    - name: Execute table.sql using sqlplus 
      shell: |
        export ORACLE_HOME={{oracle_home_path}} 
        $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