如何让 Ansible 仅在一台主机上运行一项特定任务? [英] How to make Ansible run one certain task only on one host?

查看:37
本文介绍了如何让 Ansible 仅在一台主机上运行一项特定任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

剧本看起来像:

- 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,我可以限制到一台主机,是否可以在剧本中做到这一点?

I know with command line option --limit, I can limit to one host, is it possible to do it in playbook?

推荐答案

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

For any host (with defaults it will match the first on the list):

- 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(在目标机器上定义的主机名),具体取决于您要使用的名称.

Or inventory_hostname (hostname as defined in the Ansible inventory) instead of ansible_hostname (hostname as defined on the target machine), depending on which name you want to use.

这篇关于如何让 Ansible 仅在一台主机上运行一项特定任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆