这是一个棘手的理论问题,甚至解释本身。
我将在这里以Bacula (服务器备份软件)为例来说明这一点。
Bacula有一个服务器和一个客户端组件。添加新客户端需要服务器和客户端上的配置文件。所以我想做的是:
巴库拉服务器角色:
现在我遇到的问题是host_vars和group_vars。我希望能够让我的所有[debian]主机(这是一个组)使用这个角色。所以我的剧本是这样的:
- hosts: debian
roles:
- bacula
tags:
- bacula因此,当触发此角色时,应执行以下操作:
debian-client):将配置复制到debian-clientdebian-client):将配置复制到debian-server知道我怎么能做到吗?
对我来说,这是很难解释的,所以如果有什么不清楚的地方,请让我知道,这样我就可以说得更清楚了。
感谢@Konstantin Suvorov,delegate_to回答说:https://docs.ansible.com/ansible/playbooks_delegation.html#delegation
发布于 2017-02-10 19:49:23
例如,类似这样的事情:
盘存:
[debian]
host1
host2
host3 bacula_role=server
host4
host5演奏:
- hosts: debian
vars:
bacula_server: "{{ (ansible_play_hosts | map('extract',hostvars) | selectattr('bacula_role','defined') | selectattr('bacula_role','equalto','server') | first).inventory_hostname }}"
tasks:
- debug: msg="Install server"
when: inventory_hostname == bacula_server
# client block
- block:
- debug: msg="Template server-side client config"
delegate_to: bacula_server
- debug: msg="Template client config"
when: inventory_hostname != bacula_server
# end of block将debug语句替换为实际模块(例如,apt/template),如果不存在bacula_role=server主机,则添加一些错误处理。
如果您有许多任务要安装服务器/客户端,则可以将它们拆分为bacula_server.yml和bacula_client.yml,而不需要when语句,但可以将它们包括在以下内容中:
- include: "bacula_{{ bacula_role | default('client') }}.yml"https://serverfault.com/questions/831864
复制相似问题