问题描述
有没有办法只在ansible剧本中运行一项任务?
Is there a way to only run one task in ansible playbook?
例如,在roles/hadoop_primary/tasks/hadoop_master.yml
中.我有"start hadoop job tracker services"
任务.我可以只执行一项任务吗?
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
推荐答案
您应按照 http://docs.ansible.com/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.
因此,剧本和任务都支持"tags:"属性.
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' ] }
您还可以标记基本的include语句:
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剧本中仅执行一项任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!