- name: get all_log_dirs
set_fact:
all_log_dirs="{{ (all_log_dirs|default({})) | combine( { item.key:vars[item.key] } ) }}"
with_dict: "{{ vars }}"
when: item.key is search('^((?!splunk).)*_log_dir')
这里有适当的输出:
"ansible_facts": { "all_log_dirs": { "springboot_server_log_dir": "{{ server_deployment_dir }}/logs"}
但问题是,如果我现在想要使用新的dict for e。 G:
- name: create symlink for splunk if not present
file:
src: "{{ item.value }}"
dest: "{{ splunk_log_dir }}/{{ item.key | regex_replace('_server_log_dir|_log_dir') | regex_replace('eap','jboss-eap') }}"
state: link
with_dict: "{{ all_log_dirs }}"
我只得到:
failed: [...] (item={'value': u'{{ server_deployment_dir }}/logs', 'key': u'springboot_server_log_dir'}) => { "changed": false, "invocation": { "module_args": { "dest": "/somedir/springboot", "path": "/somedir/springboot", "src": "{{ server_deployment_dir }}/logs", "state": "link", } }, "msg": "src file does not exist, use \"force=yes\" if you really want to create the link: /somedir/{{ server_deployment_dir }}/logs", "path": "/somedir/springboot", "src": "{{ server_deployment_dir }}/logs", "state": "absent"
}
为什么Ansible没有正确过滤{{server_deployment_dir}}? 即使我将src更改为dest和方式,它也无法工作,因为该变量未被过滤。
{{server_deployment_dir}}的值当然是特定于主机的,就像/ opt / applicationXY / appDeployDir
答案 0 :(得分:2)
不要使用vars
个对象。周期。
内部变量存储空间用于引擎盖下使用。
当Ansible模板引擎检测到vars
访问时,它会停止进一步的模板链!
示例:
---
- hosts: localhost
connection: local
gather_facts: no
vars:
myvar1: hello
myvar2: world
myvar3: "{{ myvar2 }}"
tasks:
- debug:
msg: "{{ myvar1 }} {{ myvar3 }}"
- debug:
msg: "{{ vars['myvar1'] }} {{ vars['myvar3'] }}"
结果:
TASK [debug] ***************************
ok: [localhost] => {
"msg": "hello world"
}
TASK [debug] ***************************
ok: [localhost] => {
"msg": "hello {{ myvar2 }}"
}
更新:如果您完全需要访问变量trough vars对象,则Ansible 2.5中提供了vars
查找功能;和照片模板一样:
E.g。
- debug:
msg: "{{ lookup('vars','myvar1') }} {{ lookup('vars','myvar3') }}"
在前一个示例的上下文中显示hello world
。