ansibel---tag模块

如果你有一个大的剧本,你可以在不运行整个剧本的情况下运行一个特定的部分。

 由于这个原因,两个游戏和任务都支持一个“标记:”属性。您只能根据命令行中的标记(标记或- skip- tags)对任务进行筛选。添加“标记”:在一个游戏的任何部分(包括角色)中添加这些标记到包含的任务中。
 

例子:Example:

tasks:

    - yum: name={{ item }} state=installed
      with_items:
         - httpd
         - memcached
      tags:
         - packages

    - template: src=templates/src.j2 dest=/etc/foo.conf
      tags:
         - configuration

  如果您想要运行“配置”和“包”部分的很长的脚本,您可以这样做:

ansible-playbook example.yml --tags "configuration,packages"

另一方面,如果你想在没有特定任务的情况下运行剧本,你可以这样做:

ansible-playbook example.yml --skip-tags "notification"

Tag Reuse

您可以在同一个文件或包含文件中对多个任务应用相同的标记名。这将用该标记运行所有任务。Example:

---
# file: roles/common/tasks/main.yml

- name: be sure ntp is installed
  yum: name=ntp state=installed
  tags: ntp

- name: be sure ntp is configured
  template: src=ntp.conf.j2 dest=/etc/ntp.conf
  notify:
    - restart ntpd
  tags: ntp

- name: be sure ntpd is running and enabled
  service: name=ntpd state=started enabled=yes
  tags: ntp

Tag Inheritance

You can apply tags to more than tasks, but they ONLY affect the tasks themselves. Applying tags anywhere else is just a convenience so you don’t have to write it on every task:

- hosts: all
  tags:
    - bar
  tasks:
    ...

- hosts: all
  tags: ['foo']
  tasks:
    ...

You may also apply tags to roles:

roles:
  - { role: webserver, port: 5000, tags: [ 'web', 'foo' ] }

And import/include statements:

- import_tasks: foo.yml
  tags: [web,foo]

or:

- include_tasks: foo.yml
  tags: [web,foo]

All of these apply the specified tags to EACH task inside the play, included file, or role, so that these tasks can be selectively run when the playbook is invoked with the corresponding tags.

Tags are inherited down the dependency chain. In order for tags to be applied to a role and all its dependencies, the tag should be applied to the role, not to all the tasks within a role.

You can see which tags are applied to tasks by running ansible-playbook with the --list-tasks option. You can display all tags using the --list-tags option.

Special Tags

有一个特殊的always标记,它将始终运行一个任务,除非特别跳过(- -skip- tags总是)

Example:

tasks:

    - debug: msg="Always runs"
      tags:
        - always

    - debug: msg="runs when you use tag1"
      tags:
        - tag1
原文地址:https://www.cnblogs.com/gzxbkk/p/7601772.html