使用Ansible来管理Homebrew,Cask,Atom编辑器的软件包

首先

在中国,Homebrew和Homebrew Cask的软件包也可以使用Brewfile进行管理,但是在Brewfile中添加软件包并在第二次或以后的执行时会出现很多”already installed”之类的警告,不太令人愉快。此外,Atom编辑器的软件包可以使用apm命令进行管理,但是把它们和其他软件包一起使用Ansible进行管理可以使整个过程更加简洁明了,所以我尝试了一下。

我创建了三个用于Ansible角色的模块和一个用于apm的模块,并按照@znz blog的参考将其注册到了Ansible Galaxy,因此你可以轻松地使用。

使用步骤

安装Homebrew、Python和Ansible。

xcode-select --install

请在这里启动Xcode并同意许可证。

ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
brew install python
brew install ansible

下载用于Homebrew、Cask和Atom软件包管理的Ansible角色。

在这里,将~/ansible-work作为工作目录。

mkdir ~/ansible-works
cd ~/ansible-works
ansible-galaxy install --roles-path=. hnakamur.homebrew-packages
ansible-galaxy install --roles-path=. hnakamur.homebrew-cask-packages
ansible-galaxy install --roles-path=. hnakamur.atom-packages 

创建库存文件

如果使用Homebrew进行安装,则/usr/local/etc/ansible/hosts将成为清单文件。

我们按照以下的方式创建文件。

mkdir -p /usr/local/etc/ansible
echo localhost > /usr/local/etc/ansible/hosts

创建 localhost.yaml 文件

请根据您想使用的软件包进行适当调整,以以下方式创建localhost.yaml文件。

- hosts: localhost
  connection: local
  gather_facts: no           
  sudo: no
  vars:
    homebrew_packages_taps:
      - homebrew/binary
      - homebrew/dupes
    homebrew_packages_packages:
      - { name: readline }
      - { name: openssl, state: linked }
      - { name: ansible }
      - { name: cmake }
      - { name: ctags }
      - { name: direnv }
      - { name: docker }
      - { name: git }
      - { name: jq }
      - { name: hub }
      - { name: imagemagick }
      - { name: mercurial }
      - { name: mysql }
      - { name: packer }
      - { name: pkg-config }
      - { name: pwgen }
      - { name: python }
      - { name: reattach-to-user-namespace }
      - { name: redis }
      - { name: subversion }
      - { name: the_silver_searcher }
      - { name: tig }
      - { name: tmux }
      - { name: whois }
      - { name: xz }
      - { name: vim }
      - { name: wget }
      - { name: zsh, install_options: disable-etcdir }
    homebrew_cask_packages_packages:
      - atom
      - calibre
      - firefox
      - google-chrome
      - google-japanese-ime
      - grandperspective
      - hipchat
      - iterm2
      - java
      - keepassx
      - kindle
      - mysqlworkbench
      - skype
      - spark
      - vagrant
      - virtualbox
      - zeroxed
    atom_packages_packages:
      - project-manager
      - recent-files
  roles:
    - hnakamur.homebrew-packages
    - hnakamur.homebrew-cask-packages
    - hnakamur.atom-packages

执行Ansible

ansible-playbook localhost.yml

从第二次开始的执行结果

% ansible-playbook localhost.yml

PLAY [localhost] **************************************************************

TASK: [hnakamur.homebrew-packages | homebrew_tap tap= state=present] **********
ok: [localhost] => (item=homebrew/binary)
ok: [localhost] => (item=homebrew/dupes)

TASK: [hnakamur.homebrew-packages | homebrew update_homebrew=yes] *************
ok: [localhost]

TASK: [hnakamur.homebrew-packages | homebrew name={{item.name}} state={{item.state|default("latest")}} install_options={{item.install_options|default()}}] ***
ok: [localhost] => (item={'name': 'readline'})
changed: [localhost] => (item={'state': 'linked', 'name': 'openssl'})
ok: [localhost] => (item={'name': 'ansible'})
ok: [localhost] => (item={'name': 'cmake'})
ok: [localhost] => (item={'name': 'ctags'})
ok: [localhost] => (item={'name': 'direnv'})
ok: [localhost] => (item={'name': 'docker'})
ok: [localhost] => (item={'name': 'git'})
ok: [localhost] => (item={'name': 'jq'})
ok: [localhost] => (item={'name': 'hub'})
ok: [localhost] => (item={'name': 'imagemagick'})
ok: [localhost] => (item={'name': 'mercurial'})
ok: [localhost] => (item={'name': 'mysql'})
ok: [localhost] => (item={'name': 'packer'})
ok: [localhost] => (item={'name': 'pkg-config'})
ok: [localhost] => (item={'name': 'pwgen'})
ok: [localhost] => (item={'name': 'python'})
ok: [localhost] => (item={'name': 'reattach-to-user-namespace'})
ok: [localhost] => (item={'name': 'redis'})
ok: [localhost] => (item={'name': 'subversion'})
ok: [localhost] => (item={'name': 'the_silver_searcher'})
ok: [localhost] => (item={'name': 'tig'})
ok: [localhost] => (item={'name': 'tmux'})
ok: [localhost] => (item={'name': 'whois'})
ok: [localhost] => (item={'name': 'xz'})
ok: [localhost] => (item={'name': 'vim'})
ok: [localhost] => (item={'name': 'wget'})
ok: [localhost] => (item={'install_options': 'disable-etcdir', 'name': 'zsh'})

TASK: [hnakamur.homebrew-cask-packages | homebrew_tap tap=caskroom/cask state=present] ***
ok: [localhost]

TASK: [hnakamur.homebrew-cask-packages | homebrew name=brew-cask state=latest] ***
ok: [localhost]

TASK: [hnakamur.homebrew-cask-packages | homebrew_cask name= state=installed] ***
ok: [localhost] => (item=atom)
ok: [localhost] => (item=calibre)
ok: [localhost] => (item=firefox)
ok: [localhost] => (item=google-chrome)
ok: [localhost] => (item=google-japanese-ime)
ok: [localhost] => (item=grandperspective)
ok: [localhost] => (item=hipchat)
ok: [localhost] => (item=iterm2)
ok: [localhost] => (item=java)
ok: [localhost] => (item=keepassx)
ok: [localhost] => (item=kindle)
ok: [localhost] => (item=mysqlworkbench)
ok: [localhost] => (item=skype)
ok: [localhost] => (item=spark)
ok: [localhost] => (item=vagrant)
ok: [localhost] => (item=virtualbox)
ok: [localhost] => (item=zeroxed)

TASK: [hnakamur.atom-packages | apm name= state=latest] ***********************
ok: [localhost] => (item=project-manager)
ok: [localhost] => (item=recent-files)

PLAY RECAP ********************************************************************
localhost                  : ok=7    changed=1    unreachable=0    failed=0

如果在homebrew模块中指定了state=link,那么它总是会变为changed=1,这有点不理想,但除此之外,不会变为changed而是按顺序展示ok的感觉是可以的。

广告
将在 10 秒后关闭
bannerAds