gpt4 book ai didi

ansible - 如何在ansible playbook中只运行一项任务?

转载 作者:行者123 更新时间:2023-12-02 11:38:50 25 4
gpt4 key购买 nike

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

例如,在roles/hadoop_primary/tasks/hadoop_master.yml中。我有"start hadoop job tracker services"任务。我可以只运行一项任务吗?

hadoop_master.yml 文件:

# 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 中所述

<小时/>

如果您有一个很大的剧本,那么能够运行配置的特定部分而不运行整个剧本可能会很有用。

出于这个原因,游戏和任务都支持“tags:”属性。

示例:

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"

另一方面,如果您想运行没有某些任务的 playbook,您可以这样做:

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

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

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

您还可以标记基本的包含语句:

- include: foo.yml tags=web,foo

两者都具有标记 include 语句内每个任务的功能。

关于ansible - 如何在ansible playbook中只运行一项任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23945201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com