1

I have the below file module that touches a file on remote hosts.

 - name: create file
   file:
     path: "/oracle/{{ inventory_hostname }}/del.tmp"
     state: touch
   register: direxists

 - fail:
     msg: "Directory missing"
   when: direxists.changed == False

The issue is on the target hosts I may have /oracle or /Oracle folder. The letter 'o' could be case insensitive. Thus, I wanted the regex to work

 path: "/[Oo]racle/{{ inventory_hostname }}/del.tmp"

But unfortunately, such regex is not supported by the file module.

I will have to fall back to shell module & use Unix commands instead; which I was not wanting.

I wish to fail the play if both /Oracle or /oracle directories are missing. If anyone of the [Oo]racle directory exists my play should not fail.

How can I achieve this requirement?

Ashar
  • 2,942
  • 10
  • 58
  • 122
  • Rename `Oracle` to `oracle` on all machines once for all. Make sure that folder is created correctly on new machines through automation. And then you don't have to worry about that anymore. Unix is case sensitive, there is no easy way to tell ansible to create a file in either case without being very verbose (using the `stat` module and testing for example) or dropping to `shell`. – Zeitounator Apr 12 '20 at 13:48

1 Answers1

1

Find the directory. Fail if none found. Create the link with the case of the first letter switched. This way both /Oracle and /oracle would exist and point to the same directory.

    - find:
        paths: /
        patterns: '[oO]racle'
        file_type: directory
      register: result

    - fail:
        msg: Directory is missing. Play failed.
      when: result.matched == 0

    - set_fact:
        mylink: "{{ mydir[0] ~ myswitch[mydir[1]] ~  mydir[2:] }}"
      vars:
        mydir: "{{ result.files.0.path }}"
        myswitch:
          'o': 'O'
          'O': 'o'

    - file:
        state: link
        src: "{{ result.files.0.path }}"
        dest: "{{  mylink }}"
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63