如何在ansible中在特定主机上运行特定任务 [英] how to run a particular task on specific host in ansible
本文介绍了如何在ansible中在特定主机上运行特定任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的库存文件的内容 -
my inventory file's contents -
[webservers]
x.x.x.x ansible_ssh_user=ubuntu
[dbservers]
x.x.x.x ansible_ssh_user=ubuntu
在我的任务文件中,它具有共同的角色,即它将在两台主机上运行,但我想在主机网络服务器上运行以下任务,而不是在清单文件中定义的 dbservers 中
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中在特定主机上运行特定任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文