ansible - 如何让 Ansible 仅在一台主机上运行一项特定任务?

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

最佳答案

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

- 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/

相关文章:

ansible - 结合 json 寄存器结果生成 ansible

variables - Ansible 获取其他组变量

shell - 如何在 @ 中使用密码! # ansible shell ssh 中的字符?

ansible - 如何从环境变量安装多个 ssh 公钥?

使用过滤器表达式时 Ansible json_query 输出列表

linux - 在ansible中找到最后一分钟修改的最新文件?

linux - Ansible amazon linux 权限被拒绝(公钥)

Ansible,如果剧本包含角色则运行任务

ansible - 如何通过 Ansible 创建嵌套目录结构

.htaccess - 你如何确保一行存在并且被 Ansible LineinFile 取消注释?