ubuntu - 使用 Ansible 安装 Dart 的包

标签 ubuntu vagrant dart ansible dart-pub

我有以下 Ansible 任务来安装 Dart:

---

# Install Dart

# Based on:
# - https://www.dartlang.org/install/linux#using-apt-get
# - https://github.com/jgrowl/ansible-dart

- name: install apt-transport-https required by Dart
  apt: pkg=apt-transport-https update_cache=yes state=latest

# Get the Google Linux package signing key.
- apt_key: url=https://dl-ssl.google.com/linux/linux_signing_key.pub state=present

# Set up the location of the stable repository.
- name: download dart source list
  get_url: url=https://storage.googleapis.com/download.dartlang.org/linux/debian/dart_stable.list dest=/etc/apt/sources.list.d/dart_stable.list mode=0644

# Set up for the dev channel
# Before running this command, follow the instructions in
# "Set up for the stable channel".
#$ sudo sh -c 'curl https://storage.googleapis.com/download.dartlang.org/linux/debian/dart_unstable.list > /etc/apt/sources.list.d/dart_unstable.list'
- apt: name=dart update_cache=yes state=present

- name: Add dart and tools to PATH
  template: src=dart.sh.j2 dest=/etc/profile.d/dart.sh owner=root group=root mode=0655
  notify:
     - install stagehand

似乎正确安装了 Dart。问题是当我最后通知 install stagehand 时。该处理程序如下
---

# Called after installing Dart to install Dart's package stagehand
- name: install stagehand
  shell: pub global activate stagehand

但我不断收到以下错误:
...

TASK [install stagehand] *******************************************************
fatal: [default]: FAILED! => {"changed": true, "cmd": "pub global activate stagehand", "delta": "0:00:00.001392", "end": "2017-02-13 02:03:06.679762", "failed": true, "rc": 127, "start": "2017-02-13 02:03:06.678370", "stderr": "/bin/sh: 1: pub: not found", "stdout": "", "stdout_lines": [], "warnings": []}
    to retry, use: --limit 

@/Users/X/Desktop/path_to_project/provision/ansible/playbook.retry

这很奇怪,因为如果我尝试使用 vagrant ssh 登录 Vagrant VM 并输入 pub ,则安装了 Dart 的打包管理器!

由于我对 Ansible 以及我正在使用的所有其他技术都很陌生,这也可能是一个问题,因为我仍然不知道 Ansible 是如何工作的。

编辑

这是理论上也应该导出的文件(即 dart.sh.j2 ) pub :
# Add vendor binaries to the path
PATH=$PATH:/usr/lib/dart/bin

编辑 2

这是 Vagrantfile(根据要求):
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/trusty64"

  config.vm.network :forwarded_port, guest: 80, host: 4567

  config.vm.provider "virtualbox" do |vb|
    vb.memory = "1024"  
  end

  # Run Ansible from the Vagrant Host
  config.vm.provision "ansible" do |ansible|
      ansible.playbook = "provision/ansible/playbook.yml"
  end

end

最佳答案

您的解决方案有两个问题。

第一个是,默认情况下 shell命令使用非交互式、非登录 shell,因此它将完全跳过运行 /etc/profile.d/dart.sh ,因此您的 PATH补充。

但是,如果您使用登录 shell,它会正常工作,因为登录 shell 会读取 /etc/profile.d :

- hosts: all
  tasks:
    - name: install stagehand
      shell: bash -lc "pub global activate stagehand"

第二个问题是pub默认会将所有包安装到~/.pub-cache/bin ,这也不在您的 PATH 中(另请注意,它只会为调用用户安装它:vagrant,因此其他用户无法使用)。更改 PATHvagrant用户的个人资料可以通过 lineinfile 来完成。 .

一个完整的工作手册示例:
- hosts: all
  roles:
    - { role: ansible-dart, when: ansible_os_family == "Debian", become: yes }

- hosts: all
  tasks:
    - name: "Add pub cache to PATH"
      lineinfile:
        dest: /home/vagrant/.profile
        line: 'PATH=$PATH:/home/vagrant/.pub-cache/bin'

    - name: install stagehand
      shell: bash -lc "pub global activate stagehand"

你可以把它放在 provision/ansible/playbook.yml 下并克隆 ansible-dart进入provision/ansible/roles目录。原文ansible-dart似乎有点过时了,所以我在 my fork 中添加了一些小修复,其中包括使用 become而不是 sudo , 并安装 unzip ,因为现在需要。

示例运行:
$ vagrant up
(...)
PLAY ***************************************************************************

TASK [ansible-dart : include] **************************************************
included: provision/ansible/roles/ansible-dart/tasks/Debian.yml for default
(...)
PLAY ***************************************************************************

TASK [Add pub cache to PATH] ***************************************************
changed: [default]

TASK [install stagehand] *******************************************************
changed: [default]

PLAY RECAP *********************************************************************
default                    : ok=12   changed=10   unreachable=0    failed=0
$ vagrant ssh -c stagehand
Welcome to Stagehand! We collect anonymous usage statistics and crash reports in
order to improve the tool. Would you like to opt-in to
additional analytics to help us improve Stagehand [y/yes/no]?

如果您不想使用 bash -lc一直,那么你必须设置PATH在你的剧本中手动赋值。如果您想主要通过 ansible 管理这台计算机,并且不需要交互式终端访问,这很有用:
- hosts: all
  environment:
    PATH: "{{ ansible_env.PATH }}:/usr/lib/dart/bin:/home/vagrant/.pub-cache/bin"
  tasks:   
    - name: install stagehand
      shell: pub global activate stagehand

显然,如果需要,您也可以混合使用这两种方法。

关于ubuntu - 使用 Ansible 安装 Dart 的包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42195653/

相关文章:

Apache 未遵循符号链接(symbolic link) (403) : Creating Symlinks Within Vagrant : Windows Host, Ubuntu guest

android - 安装 build/app/outputs/flutter-apk/app.apk... 在 flutter 运行后丢失/没有发生。设备上未保存任何应用

Flutter Provider 不会重建小部件

php 没有在输出中给出任何错误

ruby-on-rails - Rails - 设计安装(成功/现场工作,但文件不存在)

Bash:除了更新语言环境之外,通过导出设置的语言环境有什么不同吗?

flutter - 为什么我的onPressed回调不能使用Bloc调用我的Bloc?

Python3.6 ImportError : cannot import name 'main' after upgrading pip from 8. 1.1 到 19.0.1

具有多个同步文件夹的 Vagrant

ruby-on-rails - RubyMine 在 Vagrant 机器上列出的 gem 太少