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

问题描述

我的清单文件的内容-

[webservers]
x.x.x.x ansible_ssh_user=ubuntu

[dbservers]
x.x.x.x ansible_ssh_user=ubuntu

在具有共同作用的任务文件中,即它将在两个主机上运行,​​但是我想在清单文件中定义的dbservers而不是主机web服务器上运行以下任务

in my tasks file which is in common role i.e. it will run on both hosts but I want to run a following task on host webservers not in dbservers which is defined in inventory file

- name: Install required packages
  apt: name={{ item }} state=present
  with_items:
    - '{{ programs }}'
  become: yes
  tags: programs

当模块有用时还是有其他方法?我该怎么办?

is when module helpful or there is any other way? How could I do this ?

推荐答案

如果要在所有主机上运行角色,但只限于webservers组的单个任务,则-如您已经建议的- when 是您的朋友.

If you want to run your role on all hosts but only a single task limited to the webservers group, then - like you already suggested - when is your friend.

您可以定义以下条件:

when: inventory_hostname in groups['webservers']

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

10-10 18:35