kcrawford / dockutil

command line tool for managing dock items
http://patternbuffer.wordpress.com
Apache License 2.0
1.36k stars 131 forks source link

When using with Ansible not all items added to the dock #138

Closed wonderphil closed 2 years ago

wonderphil commented 2 years ago

Firstly, not sure how to troubleshoot, so if you need more details please let me know.

Basically I have a Ansible playbook that is adding 20 odd items, it was working with an older version of dockutil although it was giving an error about python but still had the exit code 0, only upgraded because I wanted to clean up and not have that error.

My play book looks like this:

---
- name: "See if Dock item {{ item.name | default(item) }} exists."
  ansible.builtin.command: "dockutil --find '{{ item.name }}'"
  register: dockitem_exists
  failed_when: >
    "No such file or directory" in dockitem_exists.stdout
    or "command not found" in dockitem_exists.stdout
  changed_when: false
  loop: "{{ dockitems_persist }}"
  tags: ['dock']

- name: Ensure Dock item {{ item.name | default(item) }} exists.
  ansible.builtin.command: "dockutil --add '{{ item.path }}'"
  when: dockitem_exists.rc >0
  loop: "{{ dockitems_persist }}"
  tags: ['dock']

Then I have the list of dock items in dockitems_persist :

dockitems_persist:
  - name: "Calendar"
    path: "/System/Applications/Calendar.app/"
  - name: "System Preferences"
    path: "/System/Applications/System Preferences.app/"
  - name: "Safari"
    path: "/Applications/Safari.app/"
  - name: "Google Chrome"
    path: "/Applications/Google Chrome.app/"
  - name: "Firefox"
    path: "/Applications/Firefox.app/"
  - name: "Tor Browser"
    path: "/Applications/Tor Browser.app/"
  - name: "Beyond Compare"
 .....

So when I run Ansible with verbose logging I can see the command is run and exits with 0 but still item is not in the dock, for example the first step check if item is in dock, in this example we look at tor broswer

TASK [See if Dock item Tor Browser exists.] *************************************************************************
task path: /Users/pdavies/repos/devops-mac-ansible/tasks/dock/add.yml:2
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: pdavies
<127.0.0.1> EXEC /bin/sh -c 'echo ~pdavies && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /Users/pdavies/.ansible/tmp `"&& mkdir "` echo /Users/pdavies/.ansible/tmp/ansible-tmp-1658305494.5841348-22429-278887691827483 `" && echo ansible-tmp-1658305494.5841348-22429-278887691827483="` echo /Users/pdavies/.ansible/tmp/ansible-tmp-1658305494.5841348-22429-278887691827483 `" ) && sleep 0'
Using module file /opt/homebrew/Cellar/ansible/6.1.0/libexec/lib/python3.10/site-packages/ansible/modules/command.py
<127.0.0.1> PUT /Users/pdavies/.ansible/tmp/ansible-local-24655e5wwvht/tmptbfe92yr TO /Users/pdavies/.ansible/tmp/ansible-tmp-1658305494.5841348-22429-278887691827483/AnsiballZ_command.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /Users/pdavies/.ansible/tmp/ansible-tmp-1658305494.5841348-22429-278887691827483/ /Users/pdavies/.ansible/tmp/ansible-tmp-1658305494.5841348-22429-278887691827483/AnsiballZ_command.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/opt/homebrew/bin/python3.9 /Users/pdavies/.ansible/tmp/ansible-tmp-1658305494.5841348-22429-278887691827483/AnsiballZ_command.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /Users/pdavies/.ansible/tmp/ansible-tmp-1658305494.5841348-22429-278887691827483/ > /dev/null 2>&1 && sleep 0'
ok: [127.0.0.1] => {
    "changed": false,
    "cmd": [
        "dockutil",
        "--find",
        "Tor Browser"
    ],
    "delta": "0:00:00.010966",
    "end": "2022-07-20 09:24:54.698085",
    "failed_when_result": false,
    "invocation": {
        "module_args": {
            "_raw_params": "dockutil --find 'Tor Browser'",
            "_uses_shell": false,
            "argv": null,
            "chdir": null,
            "creates": null,
            "executable": null,
            "removes": null,
            "stdin": null,
            "stdin_add_newline": true,
            "strip_empty_ends": true,
            "warn": false
        }
    },
    "msg": "non-zero return code",
    "rc": 1,
    "start": "2022-07-20 09:24:54.687119",
    "stderr": "Find failed for Tor Browser in /Users/pdavies/Library/Preferences/com.apple.dock.plist",
    "stderr_lines": [
        "Find failed for Tor Browser in /Users/pdavies/Library/Preferences/com.apple.dock.plist"
    ],
    "stdout": "Tor Browser was not found in /Users/pdavies/Library/Preferences/com.apple.dock.plist",
    "stdout_lines": [
        "Tor Browser was not found in /Users/pdavies/Library/Preferences/com.apple.dock.plist"
    ]
}

This is good, as its not in the dock yet and it should move to the next task of adding it, the output of adding it is:

TASK [Ensure Dock item Tor Browser exists.] *************************************************************************
task path: /Users/pdavies/repos/devops-mac-ansible/tasks/dock/add.yml:11
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: pdavies
<127.0.0.1> EXEC /bin/sh -c 'echo ~pdavies && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /Users/pdavies/.ansible/tmp `"&& mkdir "` echo /Users/pdavies/.ansible/tmp/ansible-tmp-1658305494.740566-22450-42513838004992 `" && echo ansible-tmp-1658305494.740566-22450-42513838004992="` echo /Users/pdavies/.ansible/tmp/ansible-tmp-1658305494.740566-22450-42513838004992 `" ) && sleep 0'
Using module file /opt/homebrew/Cellar/ansible/6.1.0/libexec/lib/python3.10/site-packages/ansible/modules/command.py
<127.0.0.1> PUT /Users/pdavies/.ansible/tmp/ansible-local-24655e5wwvht/tmpfbs4w9o9 TO /Users/pdavies/.ansible/tmp/ansible-tmp-1658305494.740566-22450-42513838004992/AnsiballZ_command.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /Users/pdavies/.ansible/tmp/ansible-tmp-1658305494.740566-22450-42513838004992/ /Users/pdavies/.ansible/tmp/ansible-tmp-1658305494.740566-22450-42513838004992/AnsiballZ_command.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/opt/homebrew/bin/python3.9 /Users/pdavies/.ansible/tmp/ansible-tmp-1658305494.740566-22450-42513838004992/AnsiballZ_command.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /Users/pdavies/.ansible/tmp/ansible-tmp-1658305494.740566-22450-42513838004992/ > /dev/null 2>&1 && sleep 0'
changed: [127.0.0.1] => {
    "changed": true,
    "cmd": [
        "dockutil",
        "--add",
        "/Applications/Tor Browser.app/"
    ],
    "delta": "0:00:01.062190",
    "end": "2022-07-20 09:24:55.897390",
    "invocation": {
        "module_args": {
            "_raw_params": "dockutil --add '/Applications/Tor Browser.app/'",
            "_uses_shell": false,
            "argv": null,
            "chdir": null,
            "creates": null,
            "executable": null,
            "removes": null,
            "stdin": null,
            "stdin_add_newline": true,
            "strip_empty_ends": true,
            "warn": false
        }
    },
    "msg": "",
    "rc": 0,
    "start": "2022-07-20 09:24:54.835200",
    "stderr": "",
    "stderr_lines": [],
    "stdout": "adding /Applications/Tor Browser.app",
    "stdout_lines": [
        "adding /Applications/Tor Browser.app"
    ]
}
Read vars_file 'default.config.yml'

Now from that I gather it added the tor browser app to the dock, when it runs the screen flash and the dock disappears for a moment but when it re-appears, tor browser is not there :(

Now if I take the raw params and run that dockutil --add '/Applications/Tor Browser.app/' it works!

image

Any help would be great. Also wanted to say awesome util, so hopefuly this is just user error and get going with it again.

wonderphil commented 2 years ago

Also should add, if i add the --allhomes to the command it still doesn't work, also if I run multiple times it doesn't add the app

mvught commented 2 years ago

Please look here this is all you need: https://appleshare.it/posts/use-dockutil-in-a-script/ ;)

wonderphil commented 2 years ago

unfortunately, nothing in that blog post helped :(

wonderphil commented 2 years ago

Will close this as I have found the issue and its ansible not docutil. Basically ansible is trying to run through a list of dockutil commands and overriding itself

adamstirk-ct commented 2 years ago

Will close this as I have found the issue and its ansible not docutil. Basically ansible is trying to run through a list of dockutil commands and overriding itself

@wonderphil would you mind sharing how you fixed this? As I'm having the same issue using Ansible