2015-07-10 74 views
1

背景如何訪問Ansible中的其他主機的模板變量?

我部署到包含多個主機與監控主機一起的系統。監視主機需要使用有關其他主機的信息來配置其監視檢查。但是,通過監視主機上的hostvars訪問host_vars/group_vars中定義爲Jinja模板的變量不起作用。

inventory.ini

my_host ansible_connection=local 
monitoring_host ansible_connection=local 

host_vars/my_host轉變

--- 
my_var: "{{ inventory_hostname }}" 

playbook.yml

--- 
- hosts: my_host 
    tasks: 
    - debug: var=my_var 

- hosts: monitoring_host 
    tasks: 
    - debug: var="hostvars['my_host']['my_var']" 

期望的是兩個調試任務將輸出相同的值,"my_host"。在Ansible 1.7中,第二個調試任務輸出"{{ inventory_hostname }}"(即模板未展開)。在Ansible 1.8中,第二個調試任務輸出"monitoring_host"(即模板在錯誤的上下文中展開)。

這是一個已知的錯誤嗎?有沒有很好的解決方法?

回答

0

我很確定這是越野行爲。可能的解決方法:

--- 
- hosts: my_host 
    tasks: 
    - debug: var=my_var 
    - set_fact: my_var="{{ my_var }}" # Expanded in my_host context 

- hosts: monitoring_host 
    tasks: 
    - debug: var="hostvars['my_host']['my_var']" 
+0

我覺得這樣會有效,但... hngggg – augurar