【发布时间】:2014-11-14 03:02:14
【问题描述】:
应用程序需要以下主机:
[foo-servers]
foo-server ansible_ssh_host=192.168.50.2
[bar-servers]
bar-server ansible_ssh_host=192.168.50.3
[mysql-servers]
mysql-server ansible_ssh_host=192.168.50.4
[mongodb-servers]
mongodb-server ansible_ssh_host=192.168.50.5
我需要在 foo 服务器和 bar 服务器上配置主机,因为它们需要访问 mysql 和 mongodb。为此,我引入了一个名为 hosts 的角色:
# roles/hosts/tasks/main.yml
---
- name: change hosts
template: src=hosts.j2 dest=/etc/hosts
# roles/hosts/templates/hosts.j2
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
{% for item in hostvars %}
{{ hostvars[item]['ansible_ssh_host'] }} {{ item }}
{% endfor %}
问题是当我运行时
ansible-playbook foo.yml
目标上的 /etc/hosts 只包含当前主机的 ip 和主机名,在本例中为 foo-server。
我的问题是:
当我只针对其中一个运行 playbook 时,如何获取清单中的所有主机?
或者你能建议一些替代方案吗,因为我一开始就有错误的想法。
我想到的替代方案是:
使主机配置成为针对所有主机的单独剧本,例如
---
- name: Configuring hosts
hosts: all
user: root
roles:
- hosts
缺点是我需要在其他人之前运行这个剧本,这似乎不是使用角色的正确方法。
【问题讨论】: