|
| 1 | +--- |
| 2 | +- name: "Install {{ item.name }} from DMG." |
| 3 | + loop: "{{ dmg_installed_apps }}" |
| 4 | + loop_control: |
| 5 | + label: "{{ item.name }}" |
| 6 | + block: |
| 7 | + - name: "Check if {{ item.name }} is already installed" |
| 8 | + ansible.builtin.stat: |
| 9 | + path: "/Applications/{{ item.app_name | default(item.name + '.app') }}" |
| 10 | + register: app |
| 11 | + |
| 12 | + - when: not app.stat.exists |
| 13 | + block: |
| 14 | + - name: "Download DMG for {{ item.name }}." |
| 15 | + ansible.builtin.get_url: |
| 16 | + url: "{{ item.url }}" |
| 17 | + dest: "/tmp/{{ item.name | urlencode }}.dmg" |
| 18 | + mode: '0644' |
| 19 | + |
| 20 | + - name: "Mount DMG for {{ item.name }}." |
| 21 | + ansible.builtin.command: hdiutil attach -nobrowse "/tmp/{{ item.name | urlencode }}.dmg" |
| 22 | + register: mount_result |
| 23 | + changed_when: true |
| 24 | + |
| 25 | + - name: "Find the mount point of {{ item.name }} DMG." |
| 26 | + ansible.builtin.set_fact: |
| 27 | + mount_point: "{{ mount_result.stdout_lines | select('match', '^/dev/disk.*') | map('split', '\t') | map('last') | first }}" |
| 28 | + |
| 29 | + - name: "Find the '.app' file in the mounted {{ item.name }} DMG volume." |
| 30 | + ansible.builtin.find: |
| 31 | + paths: "{{ mount_point }}" |
| 32 | + patterns: "*.app" |
| 33 | + file_type: directory |
| 34 | + recurse: no |
| 35 | + register: find_app_result |
| 36 | + |
| 37 | + - name: "Fail if no '.app' file is found." |
| 38 | + ansible.builtin.fail: |
| 39 | + msg: "Could not find an '.app' file in the DMG for {{ item.name }} at {{ mount_point }}." |
| 40 | + when: find_app_result.files | length == 0 |
| 41 | + |
| 42 | + - name: "Set the source application path for {{ item.name }}." |
| 43 | + ansible.builtin.set_fact: |
| 44 | + source_app_path: "{{ find_app_result.files[0].path }}" |
| 45 | + |
| 46 | + - name: "Copy {{ item.name }}.app to /Applications directory." |
| 47 | + ansible.builtin.command: "cp -R \"{{ source_app_path }}\" /Applications/" |
| 48 | + args: |
| 49 | + creates: "/Applications/{{ source_app_path | basename }}" |
| 50 | + changed_when: true |
| 51 | + |
| 52 | + always: |
| 53 | + - name: "Unmount volume for {{ item.name }}." |
| 54 | + ansible.builtin.command: "hdiutil detach \"{{ mount_point | default('') }}\"" |
| 55 | + when: mount_point is defined and mount_point |
| 56 | + changed_when: false |
| 57 | + ignore_errors: true |
| 58 | + |
| 59 | + - name: "Remove downloaded DMG for {{ item.name }}." |
| 60 | + ansible.builtin.file: |
| 61 | + path: "/tmp/{{ item.name | urlencode }}.dmg" |
| 62 | + state: absent |
| 63 | + when: not app.stat.exists |
0 commit comments