Using Ansible to install the Oracle Client

Ansible is a great tool for administering machines, and I love it since it allows me to define recipes that will ensure the machines will always be (and behave) the same. One need I seldomly have, is to install the Oracle client on a remote machine. There are a few roles out there to the rescue, but for my own needs, I can do it with simple tasks, and in this post I describe how I do it.

Define what it means to “install Oracle client”

Installing the Oracle client on a Linux machine is actually the task of decompressing a bunch of archives that need to be downloaded from the Oracle official website. Therefore, what I need to do is to provide a place where to extract the files, extract them and (optionally) set the ORACLE_HOME variable so that I can interactively use the installed artifacts.

Setting up Ansible

First of all, you need the Oracle stuff (archives) on your local (orchestrator) machine. I place the files into the files/oracle folder, so that Ansible will automatically go searching into files.

% ls -1 files/oracle
instantclient-basic-linux.x64-21.12.0.0.0dbru.zip
instantclient-sdk-linux.x64-21.12.0.0.0dbru.zip
instantclient-sqlplus-linux.x64-21.12.0.0.0dbru.zip



Then I define a bunch of variables in my playbook:

---
- hosts: myoraclehost
  user: luca
  become: yes
  become_method: sudo
  vars:
    oracle_base: /opt/oracle
    oracle_client_major: 21
    oracle_client_minor: 12
    oracle_home: "/instantclient__"
    user_login: luca


The variables should be quite straighforward to understand, but in the following I describe them:
  • oracle_base is where the archvies will be extracted. Note that I haven’t named such directory like oracle_home because that is not the final ORACLE_HOME;
  • oracle_client_major and oracle_client_minor are the version numbers for the Oracle client I’m going to install;
  • oracle_home is the effective ORACLE_HOME that will be created on the remote machine. Note that the variable is set to a concatenation of the previous variables;
  • user_login is the name of a user that is going to be configured to have ORACLE_HOME into its enviroment, and could be different than the user used to play the playbook remotely.

The tasks

Having defined the previous variables, it is now possible to define the tasks. The first task is to create the oracle_base directory:

    - name: Create Oracle directory
      file:
        path: ""
        state: directory



Then, it is time to extract the local Oracle stuff into the remote machine. Note that I use the oracle/ relative path that Ansible will resolve as files/oracle/. Note also that I use the Oracle major and minor number variables to find exactly the version of the archives I need.

    - name: Place Oracle libraries in  (ORACLE_HOME = )
      unarchive:
        src: "oracle/"
        dest: ""
      with_items:
        - instantclient-basic-linux.x64-..0.0.0dbru.zip
        - instantclient-sdk-linux.x64-..0.0.0dbru.zip
        - instantclient-sqlplus-linux.x64-..0.0.0dbru.zip



In the above, a fileglob could have been a better idea to list all the items. Last, and optionally, I can set up the user profile environment:

    - name: Ensure ORACLE_HOME is loaded into the user's profile
      lineinfile:
        dest: "/home//.profile"
        state: present
        regexp: '^ORACLE_HOME'
        line: 'ORACLE_HOME='

    - name: Ensure ORACLE_HOME is into the user's PATH
      lineinfile:
        dest: "/home//.profile"
        state: present
        regexp: '^export PATH=$ORACLE_HOME:$PATH'
        line: 'export PATH=$ORACLE_HOME:$PATH'




Clearly, if the user needs another shell profile file, you need to adjust the dest accordingly.

Conclusions

Ansible is a robust way to configure automatically machines, even if it can be a little verbose and noisy.

The article Using Ansible to install the Oracle Client has been posted by Luca Ferrari on November 6, 2023