如何让Ansible只在一台主机上运行一个特定的任务?

问题描述 投票:10回答:1

剧本看起来像:

- 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,我可以限制为一个主机,是否可以在playbook中执行此操作?

ansible
1个回答
15
投票

对于任何(默认情况下它将匹配列表中的第一个)主机:

- 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清单中定义的主机名)而不是ansible_hostname(目标计算机上定义的主机名),具体取决于您要使用的名称。

© www.soinside.com 2019 - 2024. All rights reserved.