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

查看:23
本文介绍了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 子句来确定它们应该在哪个操作系统上运行,例如:

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'标签的角色,而带有'桌面'.

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'.

当我在 OSX 上运行剧本时,根据当前操作系统设置一个默认的os"标签也非常好,以避免包括 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 2.5 以来,有一个新功能可以解决此类情况.

Since Ansible 2.5 there is a new feature which solves these kinds of situations.

另一个特殊标签是 never,它会阻止任务运行除非特别要求标记.

Another special tag is never, which will prevent a task from running unless a tag is specifically requested.

Example:

tasks:
  - debug: msg='{{ showmevar}}'
    tags: [ 'never', 'debug' ]

所以你的问题应该这样解决:

So your problem should be addressed like this:

- 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: [ 'never','desktop', 'homebrew_cask', osx' ]}
  - { role: gnome_terminator, tags: [ 'never','desktop', 'gnome_terminator', ubuntu' ]}

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

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