Ansible Oracle

Stop Oracle on Linux with Ansible

This post describes how to stop Oracle using Ansible code. Nobody wants to login anymore, into a vm, using a prompt, bah… really. We want to use code, for example Ansible to stuff.

Stopping Oracle takes two steps. One, stop the listener, two stop the database. First we will check if the database is running. We do this by counting ora_ processes.

- name: "STATUS - Check: {{application}} - Are there running Oracle processes?"
  shell: "ps -ef | grep ora_ | grep -v grep "
  register: result_psefgreporacle
  failed_when: result_psefgreporacle.rc == 100

We now know how many processes are running. However it’s my experience that it’s difficult to create when statements with count that are not 1 or 0…. That is why we will use the return code of grep to see if Oracle is running or not. If it’s running, the return code will be 0 and if it’s not running it will be 1.

Next we create a shutdown script for oracle. I tried to do the shutdown immediate from the cli on the same line as sqlplus however I couldn’t make it happen. Here you see how we use the return code to match 1. So create a stop script when you are running.

- name: "Create sql file to start oracle "
    dest: "/tmp/startoracle.sql"
    content: |
  when: result_psefgreporacle.rc|int == 1

Now we have a file that we can implement using the cli using the @ sign. In our inventory we have declared the db_sid variable so we can use it in this task. I explicitly tell the shell module to use the /bin/csh because this will force the read of the environment variables.

- name: "START - ORACLE - Lets start Oracle with an input file. "
  shell: "/oracle/{{db_sid}}/122/bin/sqlplus / as sysdba @/tmp/startoracle.sql"
    executable: /bin/csh
  register: result_startoracle
  when: result_psefgreporacle.rc|int == 1

When this tasks fires, Oracle will be shut down. You could display the output, if you watch your playbook, using the result_startoracle.stdout_lines variable like this:

- name: "Resultaat start oracle"
    var: result_startoracle.stdout_lines
  when: result_psefgreporacle.rc|int == 1

Thats all for now. Hopefully this made sense and maybe even solved your problem.

Thanks for reading and happy coding.


By Stuart Eight

Love automation and virtualisation. Have a passion for programming.

Leave a Reply

Your email address will not be published.