Vagrant Ansible不缓存事实供以后使用
我正在使用Vagrant和Ansible 1.9创建一个QA环境,其中包含一个haproxy前端(在loadbalancer组中)和两个jetty后端服务器(在webservers组中)。haproxy.cfg是一个具有以下行的模板,用于显示Ansible正在缓存的内容:Vagrant Ansible不缓存事实供以后使用,vagrant,ansible,Vagrant,Ansible,我正在使用Vagrant和Ansible 1.9创建一个QA环境,其中包含一个haproxy前端(在loadbalancer组中)和两个jetty后端服务器(在webservers组中)。haproxy.cfg是一个具有以下行的模板,用于显示Ansible正在缓存的内容: {% for host in groups['webservers'] %} server {{ host }} {{ hostvars[host] }}:8080 maxconn 1024 {% endfor %}
{% 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地址
基于此:我使用以下方法尝试激发Ansible缓存每个Web服务器的ip地址:
- hosts: webservers
user: vagrant
name: Gather facts from webservers
tasks: []
- hosts: webservers
user: vagrant
name: Gather facts from webservers
tasks: []
首先构建Web服务器,最后构建负载平衡器。但是,未缓存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地址。我从中了解到,默认情况下在组之间刷新缓存的事实。但是,在配置负载平衡器时,通过配置ansible将缓存放到磁盘上,可以获得所有事实,包括我需要从Web服务器获得的信息。在与我的Vagrantfile相同的文件夹中创建此ansible.cfg可修复此问题:
[defaults]
fact_caching = jsonfile
fact_caching_connection = /tmp/mycachedir
此块是不必要的,我将其删除: