---
- name: Check Brewfile exists
  stat:
    path: "{{ homebrew_brewfile_dest }}"
  register: brewfile_check

- name: Cleanup packages not found in manifest
  block:
    - name: Execute homebrew cleanup
      shell: |
        brew bundle cleanup --file={{ homebrew_brewfile_dest }} --force
      changed_when: "homebrew_cleanup.stdout_lines | length"
      register: homebrew_cleanup
    - name: Print homebrew cleanup stdout
      debug:
        var: homebrew_cleanup.stdout_lines
        verbosity: 2
  when: brewfile_check.stat.exists

- name: Tap external repositories
  homebrew_tap:
    name: "{{ homebrew['taps'] }}"
    state: present

- name: Install homebrew packages
  homebrew:
    name: "{{ homebrew['brews'] }}"
    state: present

- name: Install homebrew packages with options
  homebrew:
    name: "{{ package.key }}"
    install_options: "{{ package.value }}"
    state: present
  loop: "{{ homebrew['brews_with_options'] | dict2items }}"
  loop_control:
    loop_var: package

- name: Install homebrew casks
  homebrew_cask:
    name: "{{ homebrew['casks'] }}"
    greedy: yes
    state: present

- name: Update homebrew and upgrade all packages
  homebrew:
    update_homebrew: yes
    upgrade_all: yes

- name: Update homebrew cask and upgrade all packages
  homebrew_cask:
    upgrade_all: true

---
- name: Check Brewfile exists
  stat:
    path: "{{ homebrew_brewfile_dest }}"
  register: brewfile_check

- name: Cleanup packages not found in manifest
  block:
    - name: Execute homebrew cleanup
      shell: |
        brew bundle cleanup --file={{ homebrew_brewfile_dest }} --force
      changed_when: "homebrew_cleanup.stdout_lines | length"
      register: homebrew_cleanup
    - name: Print homebrew cleanup stdout
      debug:
        var: homebrew_cleanup.stdout_lines
        verbosity: 2
  when: brewfile_check.stat.exists

- name: Tap external repositories
  homebrew_tap:
    name: "{{ homebrew['taps'] }}"
    state: latest

- name: Install homebrew packages
  homebrew:
    name: "{{ homebrew['brews'] }}"
    state: latest

- name: Install homebrew packages with options
  homebrew:
    name: "{{ package.key }}"
    install_options: "{{ package.value }}"
    state: latest
  loop: "{{ homebrew['brews_with_options'] | dict2items }}"
  loop_control:
    loop_var: package

- name: Install homebrew casks
  homebrew_cask:
    name: "{{ homebrew['casks'] }}"
    greedy: yes
    state: latest

- name: Update homebrew and upgrade all packages
  homebrew:
    update_homebrew: yes
    upgrade_all: yes

- name: Update homebrew cask and upgrade all packages
  homebrew_cask:
    upgrade_all: true

Install homebrew packages at latest state
Install homebrew packages at latest state
