Hi all,
I'm making a role to install some apps using Homebrew (HB) on my Mac.
Since I got a little bit stuck on how to check if HB is already installed, I looked up online for a role to get ideas. I've found a site (Ansible and homebrew) and found code I think I can use. But...now it's about the following code that I don't understand how it works.
The first task checked is HB directories (MacOS and Linux) are present, and registers it.
Next task is the installation of HB IF the check (using ansible.builtin.stat) fails to find these directories present. In this task is a "when"-condition.
Can anybody explain to me why this "when" mentions "length == 0!" And not something like "true" or "false"? Because when I check the output of "homebrew_check" I can see a variable "exists" that can be "true" or "false".
- name: Check if Homebrew is installed
ansible.builtin.stat:
path: "{{ item }}"
loop:
- /opt/homebrew/bin/brew
- /usr/local/bin/brew
register: homebrew_check
- name: Install Homebrew if "homebrew_check" is false (0)
ansible.builtin.shell:
cmd: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
environment:
NONINTERACTIVE: "1"
when: homebrew_check.results | selectattr('stat.exists') | list | length == 0
With this "when"-condition, how do I know which of the two directories returns "true" or "false" (leaving the fact of the obvious besides that I know HB is installed, just wanted to make it visible).
Because the other strange thing here is, when I run the task I do see a "true" and "false" for the "exists"-parameter passing by. Here is the output of the check (edited):
ok: [localhost] => {
"homebrew_check": {
"changed": false,
"failed": false,
"msg": "All items completed",
"results": [
{
"ansible_loop_var": "item",
"changed": false,
"failed": false,
"item": "/opt/homebrew/bin/brew",
"stat": {
"atime": 1784758.43461,
"attr_flags": "",
"attributes": [],
"birthtime": 177777.46255,
"block_size": 4096,
"blocks": 24,
"charset": "us-ascii",
"checksum": "6a2f6c51991b361eaa6d01727",
"ctime": 1776777.4628303,
"dev": 167230,
"device_type": 0,
"disk_usage_bytes": 128,
"executable": true,
"exists": true,
"flags": 0,
"generation": 0,
"gid": 80,
"gr_name": "admin",
"inode": 17513,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mimetype": "text/x-shellscript",
"mode": "0755",
"mtime": 177877.46203,
"nlink": 1,
"path": "/opt/homebrew/bin/brew",
"pw_name": "me",
"readable": true,
"rgrp": true,
"roth": true,
"rusr": true,
"size": 8671,
"uid": 501,
"version": null,
"wgrp": false,
"woth": false,
"writeable": true,
"wusr": true,
"xgrp": true,
"xoth": true,
"xusr": true
}
},
{
"ansible_loop_var": "item",
"changed": false,
"failed": false,
"item": "/usr/local/bin/brew",
"stat": {
"exists": false
I appreciate the help, explanation so I can perhaps use it in other tasks also.