用于安装 Android SDK 的 Ansible-playbook

NaS*_*aSt 1 android jenkins vagrant android-sdk-tools ansible

如何使用ansible-playbook安装Android SDK?

我需要配置 Jenkins 安装 playbook 并让 Ansible 配置 Jenkins 以与 Android 集成

所以,我有 Vagrantfile:

# -*- mode: ruby -*-
# vi: set ft=ruby :

IP = "192.168.33.55"
VM_NAME = "jenkins"

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "geerlingguy/ubuntu1604" #target OS: Ubuntu 16.04
  config.ssh.insert_key = false
  config.vm.synced_folder ".", "/vagrant", disabled: true
  config.ssh.forward_agent = true

  config.vm.provider :virtualbox do |v|
    v.name = VM_NAME
    v.memory = 1024
    v.cpus = 2
    v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
    v.customize ["modifyvm", :id, "--ioapic", "on"]
  end

  config.vm.hostname = VM_NAME
  config.vm.network :private_network, ip: IP
  config.vm.network "forwarded_port", guest: 80, host: 8080

  # Set the name of the VM. See: http://stackoverflow.com/a/17864388/100134
  config.vm.define :jenkins do |jenkins|
  end

  # Ansible provisioner.
  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "jenkins/playbook.yml"
    ansible.inventory_path = "jenkins/inventory"
    ansible.sudo = true
  end
end
Run Code Online (Sandbox Code Playgroud)

和 Jenkins 安装手册:

---

- name: Install Jenkins
  hosts: jenkins
  gather_facts: yes

  vars_files:
    - vars/main.yml

  pre_tasks:
    - name: Install Python for Ansible
      raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal)
  #   changed_when: False
  # - setup: # aka gather_facts

  become: yes
  become_user: root
  remote_user: vagrant
  vars:
     - update_apt_cache: yes

  roles:
    - base
    - geerlingguy.jenkins
    - android-sdk
Run Code Online (Sandbox Code Playgroud)

android-sdk角色包含文件 main.yml 和任务:

---

- name: Download Android SDK
  action: get_url url=https://dl.google.com/android/repository/sdk-tools-linux-3859397.zip dest=/tmp/android.tgz

- name: Make opt dir for user
  action: file path=/opt/adt/ state=directory mode=0777

- name: Unpack Android SDK
  action: unarchive copy=no src=/tmp/android.tgz dest=/opt/adt/ creates=/opt/adt//android-sdk-linux

- name: Chown files
  action: file path=/opt/adt/android-sdk-linux recurse=yes state=directory

- name: Install Android SDK
  action: shell creates=/opt/adt/android-sdk-linux/tools echo y | /opt/adt/android-sdk-linux/tools/android

- name: Configure Android SDK paths
  action: lineinfile dest=/home/vagrant/.bashrc line="{{ item }}"
  with_items:
  - 'export ANDROID_HOME=/opt/adt/android-sdk-linux'
  - 'export ANDROID_TOOLS=$ANDROID_HOME/tools/'
  - 'export ANDROID_PLATFORM_TOOLS=$ANDROID_HOME/platform-tools/'
  - 'export PATH=$PATH:$ANDROID_TOOLS:$ANDROID_PLATFORM_TOOLS'
Run Code Online (Sandbox Code Playgroud)

所以,我运行我的 Vargrantfile:

vagrant up
Run Code Online (Sandbox Code Playgroud)

角色basegeerlingguy.jenkins作品,Jenkins VM成功起来了。我可以在浏览器中打开 Jenkins 页面。

但随后android-sdk角色开始工作,我看到以下内容:

< TASK [android-sdk : Download Android SDK] >
 changed: [jenkins]

< TASK [android-sdk : Make opt dir for user] >
 changed: [jenkins]

< TASK [android-sdk : Unpack Android SDK] >
 changed: [jenkins]

< TASK [android-sdk : Chown files] >
 changed: [jenkins]

< TASK [android-sdk : Install Android SDK] >

fatal: [jenkins]: FAILED! => {"changed": true, "cmd": "echo y | 
/opt/adt/android-sdk-linux/tools/android", "delta": 
"0:00:00.004105", "end": "2017-07-28 17:17:24.446018", "failed": 
true, "rc": 127, "start": "2017-07-28 17:17:24.441913", "stderr": 
"/bin/sh: 1: /opt/adt/android-sdk-linux/tools/android: not found", 
"stderr_lines": ["/bin/sh: 1: /opt/adt/android-sdk-linux/tools/android: not found"],
"stdout": "", "stdout_lines": []}

   jenkins : ok=41   changed=2    unreachable=0    
   failed=1   

   Ansible failed to complete successfully. Any error output should 
   be visible above. Please fix these errors and try again.
Run Code Online (Sandbox Code Playgroud)

但我可以看到 sdk-tools-linux-3859397.zip 包含“tools”目录,其中包含“android”shell 脚本

我的系统:Linux Mint 18.2 Sonya、VirtualBox 5.0.40_Ubuntur115130、Ansible 2.3.1.0 和 Vagrant 1.9.7。

kfr*_*ezy 5

两个主要问题是该unarchive任务不会创建/opt/adt/android-sdk-linux目录,并且我认为您安装 SDK 的命令不正确。

附带说明一下,您不需要action为每个任务使用该模块。您只需将其替换为操作内的模块即可。

- name: Make opt dir for user
  action: file path=/opt/adt/ state=directory mode=0777
Run Code Online (Sandbox Code Playgroud)

会成为

- name: Make opt dir for user
  file:
    path: /opt/adt/
    state: directory
    mode: 0777
Run Code Online (Sandbox Code Playgroud)

因此,要解决您的主要问题,您首先需要在解压 android sdk 之前创建目标目录。

 - name: Create Android SDK directory
   file:
     path: /opt/adt/android-sdk-linux
     state: directory

 - name: Unpack Android SDK
   unarchive:
     copy: no
     src: /tmp/android.tgz
     dest: /opt/adt/android-sdk-linux
     creates: /opt/adt/android-sdk-linux/tools
Run Code Online (Sandbox Code Playgroud)

我提取了安装https://gist.github.com/rikyperdana/61b1a5008b757da35a745185bfed7374的 SDK 的命令。

  - name: Install Android SDK
    shell: yes | ./android update sdk
    args:
      chdir: /opt/adt/android-sdk-linux/tools 
Run Code Online (Sandbox Code Playgroud)