如何只使用多个标签运行ansible任务?

问题描述 投票:29回答:4

想象一下这个ansible剧本:

- name: debug foo
  debug: msg=foo
  tags:
     - foo

- name: debug bar
  debug: msg=bar
  tags:
     - bar

- name: debug baz
  debug: msg=baz
  tags:
     - foo
     - bar

我怎么才能只运行debug baz任务?我想说只运行用foobar标记的任务。那可能吗?

我尝试了这个,但它将运行所有3个任务:

ansible-playbook foo.yml -t foo,bar
ansible ansible-playbook
4个回答
23
投票

Ansible标签使用“或”而非“和”作为比较。您创建另一个标记的解决方案是合适的。


1
投票

试试when指令:

- name: debug foo
  debug: msg=foo
  tags:
     - foo

- name: debug bar
  debug: msg=bar
  tags:
     - bar

- name: debug baz
  debug: msg=baz
  when:
    - '"foo" in ansible_run_tags'
    - '"bar" in ansible_run_tags'

0
投票

我相信正确的语法是:

- name: debug baz
  debug: msg=baz
  tags: foo, bar

0
投票

如果你这样使用:

- name: debug baz
  debug: msg=baz
  tags:
    - foo
    - bar

你做了一个操作或。所以,如果你使用命令:

ansible-playbook -i inventory test.yml --tags foo

要么

ansible-playbook -i inventory test.yml --tags bar

将执行此任务。

如果您使用:

- name: debug baz
  debug: msg=baz
  tags:
    - foo, bar

你做了一个操作AND。所以只有命令:

ansible-playbook -i inventory test.yml --tags foo, bar

将执行此任务。

© www.soinside.com 2019 - 2024. All rights reserved.