我希望有一个角色,既可以部署ntp客户端(主机指向内部ntp服务器的时间),也可以部署ntp服务器(指出时间的主机)。
对于如何使用多个角色来完成此操作,有一个很好的链接。,但由于两者之间的差别很小(服务器与客户端),我想知道是否有一种方法可以简化和使用一个角色,并提供一些智能来根据需要配置主机。
在我的主机目录中,我有一个组ntp,它是服务器,其他的都是客户机。
[web]
10.1.1.1
10.1.1.2
[app]
10.1.2.1
10.1.2.2
[db]
10.1.3.1
10.1.3.2
[ntp]
10.1.4.1
10.1.4.2
除了复制到/etc/ntp.conf
的模板之外,部署服务器或客户端的角色/任务基本上是相同的。对于所有主机,请在ntp-client.j2
模板上复制,除非主机是[ntp]
组的一部分,然后通过ntp-server.j2
模板复制。
思想1:在剧本中设置标签?
初始剧本可以为角色设置标记变量吗?我不是指基于标记的过滤器,而是将其设置为它将调用的角色。类似于:
- hosts:all,!ntp
tags: client (set the server tag, don’t filter against the argument)
roles:
- ntp
- hosts:ntp
tags: server (set the server tag, don’t filter against the argument)
roles:
- ntp
然后在角色/ntp/template/main.yml
文件中定义
- name: stuff all host do
module: modulename attribute=value
tags:
- client
- server
- name: stuff just for ntp server
module: modulename attribute=value
tags:
- server
- name: stuff for all hosts (other then ntp servers)
module: modulename attribute=value
tags:
- client
思想2:在任务文件中使用主机
或者在/ntp/tasks/main.yml
文件中,我们可以使用hosts
过滤器来控制什么运行在什么主机上吗?
- hosts: all
- name: stuff all host do
module: modulename attribute=value
- hosts: ntp
- name: stuff just for ntp server
module: modulename attribute=value
- hosts: all,!ntp
- name: stuff for all hosts (other then ntp servers)
module: modulename attribute=value
应该用条件来代替某种方式吗?
发布于 2020-05-01 19:40:06
如果它们仅通过这一个模板不同,那么您可以将分配转移到库存中,或者使用group_vars
(一个等价的结果,但不需要污染库存文件)
[all:vars]
ntp_conf_template=ntp-client.j2
; ...etc...
[ntp]
10.1.4.1
10.1.4.2
[ntp:vars]
ntp_conf_template=ntp-server.j2
然后,在任务中:
- name: generate ntp.conf
template:
src: '{{ ntp_conf_template }}'
dest: /etc/ntp.conf
https://stackoverflow.com/questions/61548843
复制相似问题