1
votes

I'm trying to write a playbook that will rsync the folders from source to target after a database refresh. Our Peoplesoft HR application also requires a filesystem refresh along with database. I'm new to ansible and not an expert with python. I've written this but my playbook fails if any of the with_items doesn't exist. I'd like to use this playbook for all apps and the folders may differ between apps. How can I skip the folders that doesn't exist in source. I'm passing {{ target }} at command line.

---
- hosts: '<hostname>'
  remote_user: <user>

  tasks:
    - shell: ls -l /opt/custhome/prod/
      register: folders

    - name:  "Copy PROD filesystem to target"
      synchronize:
        src: "/opt/custhome/prod/{{ item }}"
        dest: "/opt/custhome/dev/"
        delete: yes
      when: "{{ folders == item }}"
      with_items:
        - 'src/cbl/'
        - 'sqr/'
        - 'bin/'
        - 'NVISION/'

In this case, NVISION doesn't exist in HR app but it does in FIN app. But the playbook is failing coz that folder doesn't exist in source.

1

1 Answers

1
votes

You can use find module to find and store paths to source folders and then to iterate over results. Example playbook:

- hosts: '<hostname>'
  remote_user: <user>

  tasks:
    - name: find all directories
      find:
        file_type: directory
        paths: /opt/custhome/prod/
        patterns:
          - "src"
          - "sqr"
          - "bin"
      register: folders

    #debug to understand contents of {{ folders }} variable
    # - debug: msg="{{ folders }}"

    - name:  "Copy PROD filesystem to target"
      synchronize:
        src: "{{ item.path }}"
        dest: "/opt/custhome/dev/"
        delete: yes
      with_items: "{{ folders.files }}"

You may want to use recurse to descend into subdirectories and use_regex to use the power of python regex instead of shell globbing.