使用Ansible安装Dart的包

时间:2017-02-13 02:12:25

标签: ubuntu vagrant dart ansible dart-pub

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

---

# 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.j2pub

# 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

3 个答案:

答案 0 :(得分:1)

您的dart.sh不是非交互式非登录shell会话的来源。这就是为什么在以交互方式登录以及通过SSH运行脚本时会看到不同结果的原因。

考虑到文件内容,请使用pub可执行文件的完整路径(可能,您不需要shell模块,command就足够了):

# Called after installing Dart to install Dart's package stagehand
- name: install stagehand
  command: /usr/lib/dart/bin/pub global activate stagehand

答案 1 :(得分:1)

您的解决方案存在两个问题。

第一个是,默认情况下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中(另请注意,它只会为调用用户安装:{{1} },所以它不适用于其他用户)。可以使用vagrant更改PATH用户个人资料中的vagrant

完整的,有效的剧本示例:

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似乎有点过时,因此我添加了一些针对my fork的小修补程序,其中包括使用ansible-dart代替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]? ,则必须在Playbooks中手动设置bash -lc值。如果您想主要通过ansible管理此计算机,并且实际上不需要交互式终端访问,这非常有用:

PATH

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

答案 2 :(得分:0)

根据 SztupY 的回答,我设法在Vagrant VM stagehand激活 Dart的包ubuntu/trusty64

这些是我使用的相应版本的工具(如果有人因为类似的问题而在这篇文章中结束):

+=================+============================+=========================================================+
|      Tool       |            Version         |                         Comments                        |
+=================+============================+=========================================================+
| Ansible         |   2.2.1.0                  |  configured module search path = Default w/o overrides  |
+-----------------+----------------------------+---------------------------------------------------------+
| Vagrant         |  1.9.1                     |                                                         |
+-----------------+----------------------------+---------------------------------------------------------+
| ubuntu/trusty64 | (virtualbox, 20170202.1.0) |                                                         |
+-----------------+----------------------------+---------------------------------------------------------+
| ansible-dart    |                            |    Source: https://github.com/nbro/ansible-dart         |
+-----------------+----------------------------+---------------------------------------------------------+

我将Github项目ansible-dart的下载文件夹放在文件夹roles下,该文件夹位于配置文件夹的主文件夹下(在我的情况下,我称之为provision/ansible),其中包含playbook.yml文件。

- name: provisioning project...

  hosts: all

  roles:
    - { role: ansible-dart, when: ansible_os_family == "Debian", become: true }

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

    - name: activate Dart's package 'stagehand'
      shell: bash -lc "pub global activate stagehand"