运行 ansible playbook 时如何缩小范围? [英] 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"
如果您想改为(或作为组合)限制游戏运行的节点,那么您可以使用 --limit
选项.因此,如果您想将播放限制为仅网络服务器,那么您可以使用 ansible-playbook -i path/to/hosts playbook.yml --limit webservers
运行它,甚至将其限制为单个特定ansible-playbook -i path/to/hosts playbook.yml --limit web.example.org
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 playbook 时如何缩小范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!