剧本看起来像:
- hosts: all
tasks:
- name: "run on all hosts,1"
shell: something1
- name: "run on all hosts,2"
shell: something2
- name: "run on one host, any host would do"
shell: this_command_should_run_on_one_host
- name: "run on all hosts,3"
shell: something3
我知道命令行选项
--limit
,我可以限制为一台主机,是否可以在剧本中做到这一点?
最佳答案
对于任何主机(默认情况下,它将匹配列表中的第一个):
- name: "run on first found host"
shell: this_command_should_run_on_one_host
run_once: true
对于特定主机:
- name: "run on that_one_host host"
shell: this_command_should_run_on_one_host
when: ansible_hostname == 'that_one_host'
或
inventory_hostname
(Ansible list 中定义的主机名)而不是 ansible_hostname
(在目标机器上定义的主机名),具体取决于您要使用的名称。
关于ansible - 如何让 Ansible 仅在一台主机上运行一项特定任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47342724/