Score:0

Supplying variables to an Ansible script for upgrading Payara

be flag

I'm attempting to write an Ansible script to upgrade a Payara instance to a newer one, by taking a backup of the domains on the current installation, nuking it, installing the new version of Payara and then restoring the backup that was just made to it. This appears to be the recommended way to upgrade Payara.

I've made considerable progress laboring on this, partially from code I've already written to run a fresh install of Payara and partially from code I've written new for this script, but have run into a roadblock on the last part. Because it's possible that a given Payara instance may have more than one domain in it, I've had to figure out how to use loops in Ansible and I still don't fully grasp how they work. I've successfully done the first half of the process (extracting out backups of all the domains) by doing a find of the directory Payara stores the domains in and looping over it, but the restore has introduced a headache because the filename is unique every time - it will be the only file in that folder, but even after analyzing it for some time I can't see how you'd reference what'd effectively be two loops at once.

My code (cut down for relevancy) so far:

    - name: Get list of Payara domains
      find:
        paths: /opt/{{ payara_folder }}/glassfish/domains
        file_type: directory
      register: result_of_find

    - name: Run backup-domain on each of the Payara domains
      command: "/opt/{{ payara_folder }}/bin/asadmin backup-domain --backupDir /tmp/payara_backup {{ item.path | basename }}"
      with_items: "{{ result_of_find.files }}"

    - name: Run restore-domain on each of the Payara domains
      command: "/opt/{{ payara_folder }}/bin/asadmin restore-domain --fileName /tmp/payara_backup/{{ item.path | basename }}/**NAME OF BACKUP FILE HERE** --backupDir /tmp/payara_backup {{ item.path | basename }}"
      with_items: "{{ result_of_find.files }}"

As mentioned, tasks 1 & 2 work perfectly fine, but I'm struggling with task 3: Payara's backup extraction kicks out a name along the lines of domain_yyyy_mm_dd_v00001.zip. I considered adding a task that goes through and renames all the backups to something like backup.zip between steps 2 and 3 given that they're all in separate sub-directories but a) I don't know if Payara will accept a file with a different naming layout (I don't see why it wouldn't?) and b) this only moves the problem of identifying the filenames to a different task.

Any suggestions on how I go about this?

Score:1
pt flag

Why not replace your command task with a shell task and a wildcard?

- name: Run restore-domain on each of the Payara domains
  shell: >-
    /opt/{{ payara_folder }}/bin/asadmin restore-domain
    --fileName /tmp/payara_backup/{{ item.path | basename }}/*.zip
    --backupDir /tmp/payara_backup {{ item.path | basename }}
  with_items: "{{ result_of_find.files }}"

Since each directory only contains a single file, we can write /tmp/payara_backup/{{ item.path | basename }}/*.zip and the shell will replace our wildcard (*.zip) with the appropriate filename.

Note that I've reformatted the task for readability, but it remains syntactically identical to the original except for the change from command to shell and the use of the wildcard in the path.

Chris Douglas avatar
be flag
I have just tested this now and it works, thank you very much!
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.