中只运行一项任务

中只运行一项任务

本文介绍了如何在 ansible playbook 中只运行一项任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 ansible playbook 中只运行一个任务?

Is there a way to only run one task in ansible playbook?

例如,在roles/hadoop_primary/tasks/hadoop_master.yml 中.我有 启动 hadoop 作业跟踪器服务" 任务.我可以只运行那一项任务吗?

For example, in roles/hadoop_primary/tasks/hadoop_master.yml. I have "start hadoop job tracker services" task. Can I just run that one task?

hadoop_master.yml 文件:

hadoop_master.yml file:

# Playbook for  Hadoop master servers

- name: Install the namenode and jobtracker packages
  apt: name={{item}} force=yes state=latest
  with_items:
   - hadoop-0.20-mapreduce-jobtracker
   - hadoop-hdfs-namenode
   - hadoop-doc
   - hue-plugins

- name: start hadoop jobtracker services
  service: name=hadoop-0.20-mapreduce-jobtracker state=started
  tags:
   debug

推荐答案

您应该使用 tags:https://docs.ansible.com/ansible/latest/user_guide/playbooks_tags.html

如果您有一个很大的剧本,能够在不运行整个剧本的情况下运行配置的特定部分可能会很有用.

If you have a large playbook it may become useful to be able to run a specific part of the configuration without running the whole playbook.

出于这个原因,游戏和任务都支持标签:"属性.

Both plays and tasks support a "tags:" attribute for this reason.

示例:

tasks:

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

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

如果你只想运行一个很长的剧本的配置"和包"部分,你可以这样做:

If you wanted to just run the "configuration" and "packages" part of a very long playbook, you could do this:

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

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

On the other hand, if you want to run a playbook without certain tasks, you could do this:

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

您还可以将标签应用于角色:

You may also apply tags to roles:

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

你也可以标记基本的包含语句:

And you may also tag basic include statements:

- include: foo.yml tags=web,foo

这两个都具有在 include 语句中标记每个任务的功能.

Both of these have the function of tagging every single task inside the include statement.

这篇关于如何在 ansible playbook 中只运行一项任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 20:25