运行Ansible剧本时,如何缩小范围? [英] How do I narrow down scope when running an ansible playbook?
问题描述
我有一部剧本需要花费很多时间来执行,部分原因是有很多必须在其上运行的节点(我浪费时间在检查所有节点的状态上),我需要在其中的某个地方进行一些更改.
I have a playbook that takes a lot of time to execute, partly due to having a lot of nodes on which it has to run on (I am wasting time with ansible checking the status of all the nodes), and I need to make some changes somewhere in the middle of it.
缩小范围的最佳方法是什么?我已经考虑过隔离所需的更改和/或仅在单个节点上运行修改过的零件?
What would be the best way in which I could narrow down the scope of the playbook? I've considered isolating the required change and/or just running the modified part on a single node?
推荐答案
这是标签的用途.
您可以使用标签的任何组合为任何任务添加标签,然后指定要运行的标签组合(或使用--skip-tags
跳过),然后仅运行那些指定的任务.
You can tag any tasks with any combination of tags and then specify that combination of tags to run (or, alternatively, to skip with --skip-tags
) which will then only run those specified tasks.
因此示例剧本可能看起来像这样:
So an example playbook may look like this:
- hosts: all
tasks:
- name: copy foo
copy:
src: path/to/foo
dest: path/to/foo
tags:
- copy_foo
- copy
- foo
- name: copy bar
copy:
src: path/to/bar
dest: path/to/bar
tags:
- copy_bar
- copy
- bar
- name: restart foo
service:
name: foo
state: restarted
tags:
- restart_foo
- restart
- foo
- name: restart bar
service:
name: bar
state: restarted
tags:
- restart_bar
- restart
- bar
然后我可以使用ansible-playbook -i path/to/hosts playbook.yml --tags "restart_foo"
仅运行"restart foo"任务.或者,我可以运行所有与bar相关的标签,还可以使用以下任意组合重新启动foo(但不能复制foo):
I can then run just the "restart foo" task with ansible-playbook -i path/to/hosts playbook.yml --tags "restart_foo"
. Alternatively I could run all the bar related tags and also restart foo (but not copying foo) with any combination of the following:
-
ansible-playbook -i path/to/hosts playbook.yml --tags "bar,restart_foo"
-
ansible-playbook -i path/to/hosts playbook.yml --tags "restart,copy_bar"
-
ansible-playbook -i path/to/hosts playbook.yml --skip-tags "copy_foo"
ansible-playbook -i path/to/hosts playbook.yml --tags "bar,restart_foo"
ansible-playbook -i path/to/hosts playbook.yml --tags "restart,copy_bar"
ansible-playbook -i path/to/hosts playbook.yml --skip-tags "copy_foo"
If you want to instead (or as a combination) limit the nodes that a play is ran against then you can do that with --limit
option. So if you want to limit the play to just web servers then you could run it with ansible-playbook -i path/to/hosts playbook.yml --limit webservers
or even limit it to a single specific host with ansible-playbook -i path/to/hosts playbook.yml --limit web.example.org
这篇关于运行Ansible剧本时,如何缩小范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!