大网数据广告活动
服务器运维问答

How to use Ansible's homebrew module to assert a list of packages are installed

· 52CE 我爱测

I'm a relatively new user of Ansible. I use it to orchestrate a few groups of Linux servers. I'm now expanding that use to use Ansible's homebrew module . Using the homebrew module, I want to assert that a list of packages are in…

问题原文

原帖链接 · James T Snell · CC BY-SA 3.0

I'm a relatively new user of Ansible. I use it to orchestrate a few groups of Linux servers. I'm now expanding that use to use Ansible's homebrew module.

Using the homebrew module, I want to assert that a list of packages are installed on my macOS boxes. So far, the best I can do is to make separate roles for each package, with each role resembling:

- name: "htop"
  homebrew:
    name: htop
    state: present

It seems like there must be a way to give a list of packages in a single role, which I've seen before when using the Ansible apt module.

Any tips? Does the homebrew module itself expressly have to support for this sort of operation or perhaps there's something more fundamental to Ansible or even Python itself that provides what I'm looking for?

PS - You can safely assume that my macOS machines are all servers.

已采纳解答

原帖链接 · James T Snell · CC BY-SA 3.0

I've found a solution, more or less. My role now looks like this, and it works:

- name: "brew favourites"
  homebrew: name={{ item }} state=present
  with_items:
    - htop
    - nmap
    - telnet
    - wget
    - nginx

Extended Notes

I don't really know what mechanism is responsible for this working. I feel like it's outside of Ansible and is actually a Pythonism (Is this a Lamda?). Whatever it is, it could serve as a template for a general solution to this sort of thing.

I tried the above after seeing something similar described for defining a list of apt packages, that approach looks like:

- name: "Asserting apt packages"
  apt: pkg={{ item }} state=installed
  with_items:
    - aptitude
    - vim
    - htop
    - nmap
    - curl
    - screen
    - open-vm-tools

Thus note that in the apt module, I wanted to have many packages listed for the pkg parameter. In the homebrew modules' context, it wasn't pkg, but instead name.

Similarly, both Ansible modules have a state parameter, but for apt the appropriate value was installed, and with homebrew it's present.

So I think this sort of syntax applies to Ansible in a global context. And modules need have zero awareness or explicit support of this. Powerful.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-10-26)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。