我使用Vagrant和Ansible 1.9创建了一个带有haproxy前端(在负载均衡器组中)和两个jetty后端服务器(在Web服务器组中)的QA环境。 haproxy.cfg是一个模板,其中包含以下行以显示Ansible正在缓存的内容:
{% for host in groups['webservers'] %}
server {{ host }} {{ hostvars[host] }}:8080 maxconn 1024
{% endfor %}
最终配置将使用类似:
server {{ host }} {{ hostvars[host]['ansible_eth1']['ipv4']['address'] }}:8080 maxconn 1024
配置每个后端服务的IP地址。
基于此:http://docs.ansible.com/guide_rolling_upgrade.html我使用以下内容尝试激活Ansible来缓存每个网络服务器的IP地址:
- hosts: webservers
user: vagrant
name: Gather facts from webservers
tasks: []
首先构建Web服务器,然后使用负载均衡器。但是,网络服务器' IP地址未被缓存。就是这就是:
server qa01 {'ansible_ssh_host': '127.0.0.1', 'group_names': ['webservers'], 'inventory_hostname': 'qa01', 'ansible_ssh_port': 2222, 'inventory_hostname_short': 'qa01'}:8080 maxconn 1024
对于要缓存的值,我需要做什么?似乎Ansible知道价值观,因为:
- hosts: all
user: vagrant
sudo: yes
tasks:
- name: Display all variables/facts known for a host
debug: var=hostvars[inventory_hostname]
将显示完整的值集合,包括我需要的IP地址。
答案 0 :(得分:1)
我在http://jpmens.net/2015/01/29/caching-facts-in-ansible/了解到,默认情况下,缓存的事实会在组之间刷新。但是,通过配置ansible以将缓存丢弃在磁盘上来配置负载均衡器时,可以使所有事实(包括Web服务器所需的信息)可用。在与Vagrantfile相同的文件夹中创建此ansible.cfg可以解决问题:
[defaults]
fact_caching = jsonfile
fact_caching_connection = /tmp/mycachedir
此块是不必要的,我将其删除:
- hosts: webservers
user: vagrant
name: Gather facts from webservers
tasks: []