Ansible-默认/显式标签 [英] Ansible - Default/Explicit Tags

查看:82
本文介绍了Ansible-默认/显式标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一部剧本,其中包括并标记了各种角色:

I've got a playbook that includes and tags various roles:

- name:  base
  hosts: "{{ host | default('localhost') }}"

roles: 

  - { role: apt,              tags: [ 'base', 'apt', 'ubuntu']}
  - { role: homebrew,         tags: [ 'base', 'homebrew', osx' ]}
  - { role: base16,           tags: [ 'base', 'base16', 'osx' ]}
  - { role: nodejs,           tags: [ 'base', 'nodejs' ]}
  - { role: tmux,             tags: [ 'base', 'tmux' ]}
  - { role: vim,              tags: [ 'base', 'vim' ]}
  - { role: virtualenv,       tags: [ 'base',  virtualenv', 'python' ]}
  - { role: homebrew_cask,    tags: [ 'desktop', 'homebrew_cask', osx' ]}
  - { role: gnome_terminator, tags: [ 'desktop', 'gnome_terminator', ubuntu' ]}

大多数任务都使用when子句来确定它们应在哪个OS上运行,例如:

Most of the tasks are using when clauses to determine which OS they should run on, for example:

- name: install base packages
  when: ansible_distribution == 'MacOSX'
  sudo: no
  homebrew:
    name: "{{ item.name }}"
    state: latest
    install_options: "{{ item.install_options|default() }}"
  with_items: homebrew_packages

如果我在未指定任何标签的情况下运行ansible-playbook base.yml,则所有任务都将运行.如果我指定标签,例如ansible-playbook base.yml --tags='base',则仅使用base run标签的角色.

If I run ansible-playbook base.yml without specifying any tags, all the tasks run. If I specify a tag, for example ansible-playbook base.yml --tags='base', only the roles tagged with base run.

默认情况下(如果未指定标签),我只想运行标有'base'的角色,而标有'desktop'的角色.

By default (if no tags are specified), I'd only like to run the roles tagged with 'base', and not the roles tagged with 'desktop'.

最好在当前操作系统的基础上设置默认的'os'标签,以避免在OSX上运行剧本时包含ubuntu的所有任务(反之亦然).

It would also be really nice to set a default 'os' tag, based on the current operating system, to avoid including all the tasks for the ubuntu when I'm running the playbook on OSX (and vice-versa).

有什么可能的想法,以及我该怎么做?

Any ideas if this is possible, and how I might do it?

推荐答案

不幸的是,没有这样的功能.当前,Ansible中的标签处理非常有限.默认情况下,您不能设置默认标签,也不能排除标签.

Unfortunately there is no such feature. Tag handling in Ansible currently is very limited. You can not set default tags and you can not exclude tags by default.

Google用户组中有一些主题,而github上有一些功能要求.但是还没有结果.到目前为止,常见的答案是,您应该创建一个shell脚本并将其放在您的剧本前面.然后,该脚本可以根据需要设置--tags--skip-tags.非常不愉快,但据我所知,目前唯一的选择.

There are some threads on the Google user group and feature requests on github regarding this. But no outcome yet. The common answer so far is, you should create a shell script and place it in front of your playbook. This script then can set the --tags and --skip-tags accordingly to your needs. Very unpleasant but as far as I know the only option right now.

这篇关于Ansible-默认/显式标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆