假设:
android-23
extra-android-support
,sys-img-armeabi-v7a-android-19
,android update sdk -u -a -t some-ids
等)安装了一些组件
问题:
如果使用命令行检查是否安装了这些组件,基于与我安装它们完全相同的ID?
加成: 它可以在没有互联网连接的情况下完成
我将在我的Ansible Playbook脚本中使用它。我需要它来避免运行安装命令,如果它们已存在为幂等性。虽然android update sdk
不会安装已安装的项目,但它总是事先查询远程android存储库,这将是不必要的开销。
答案 0 :(得分:2)
我知道这是一个非常古老的问题,但我有一段时间遇到了同样的问题而无法找到答案。
所以我已经通过使用当地事实在我自己的ansible角色(这里:“ android_sdk ”)中完成了它。
您需要做的步骤是:
从本地事实中收集一些变量(例如,defaults / main.yml):
android_sdk_installed: "{{ ansible_local.android_sdk.sdks_installed
if (ansible_local|d() and ansible_local.android_sdk|d() and
ansible_local.android_sdk.sdks_installed|d([]))
else [] }}"
安装指定的SDK / build-tools / etc
- name: Install Android SDKs for applicable releases
shell: /bin/bash -l -c 'echo y | android -s update sdk -u -a -t "{{ item }}"'
with_items:
- "{{ android_sdk_tools }}"
when: item not in android_sdk_installed
register: sdk_install_res
例如 android_sdk_tools 设置为:
android_sdk_tools:
- "build-tools-23.0.2"
- "build-tools-19.1.0"
修改本地变量 android_sdk_installed (我知道它非常难看 - 它只是用于过滤上一步的输出以获得干净的视图)
- name: Set local android_sdk_installed fact
set_fact: android_sdk_installed="{{ android_sdk_installed|d([]) + [item.item|d()] }}"
with_items: |
{% set items=[] %}
{% for item in sdk_install_res.results %}
{% if item.item is defined %}{% if item.rc is defined %}
{% if items.append({'item':item.item,'rc':item.rc}) %}{% endif %}
{% endif %}{% endif %}
{% endfor %}
{{ items | to_nice_json }}
when: sdk_install_res is defined and item.rc == 0
保存当地事实(基于DebOps Ansible project的代码)
- name: Make sure that Ansible local facts directory is present
file:
path: '/etc/ansible/facts.d'
state: 'directory'
owner: 'root'
group: 'root'
mode: '0755'
- name: Save android_sdk local facts
template:
src: 'etc/ansible/facts.d/android_sdk.fact.j2'
dest: '/etc/ansible/facts.d/android_sdk.fact'
owner: 'root'
group: 'root'
mode: '0644'
register: android_sdk_register_local_facts
- name: Gather facts if they were modified
action: setup
when: android_sdk_register_local_facts.changed
模板文件“ android_sdk.fact.j2 ”的内容为:
{% set android_sdk_tpl_sdks_installed = [] %}
{% if (ansible_local|d() and ansible_local.android_sdk|d() and
ansible_local.android_sdk.sdks_installed|d()) %}
{% for element in ansible_local.android_sdk.sdks_installed %}
{% set _ = android_sdk_tpl_sdks_installed.append(element) %}
{% endfor %}
{% endif %}
{% for element in android_sdk_installed or [] %}
{% set _ = android_sdk_tpl_sdks_installed.append(element) %}
{% endfor %}
{
"sdks_installed": {{ android_sdk_tpl_sdks_installed | unique | sort | to_nice_json }}
}
希望它有所帮助,欢呼! :)强>