变量不会被正确过滤

问题描述:

我在做什么错? 我使用下面的任务来获取主机的所有已定义log_dirs.这些信息存储在一个事实中,这是一个命令,它就像一个咒语.

what am I doing wrong? I use below task to get all defined log_dirs of a host. Those information are stored in a fact, which is a dict and this works like a charm.

- 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"}

但是问题是,如果我现在想对e使用新的字典. g.:

But the problem is, if I now want to use the new 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及其解决方法,也不会起作用,因为未对变量进行过滤.

Why isn't {{ server_deployment_dir }} filtered correctly by Ansible? Even I change src to dest and the way around, it won't work, because the variable isn't being filtered.

{{server_deployment_dir}}的值当然是特定于主机的,类似于/opt/applicationXY/appDeployDir

The value of {{ server_deployment_dir }} is of course host specific and is sth like /opt/applicationXY/appDeployDir

不要使用vars对象.期间.

这是供内部使用的内部变量存储.

It is internal variable storage intended for under-the-hood usage.

当Ansible模板引擎检测到vars访问时,它将停止进一步的模板链!

When Ansible template engine detects vars access, it stops further templating chain!

示例:

---
- 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 }}"
}

更新:如果您完全需要访问变量槽vars对象,则Ansible 2.5中提供了vars查找;并照常模板化值:

Update: if you utterly need to access variable trough vars object, there's vars lookup available in Ansible 2.5; and it templates values as usual:

例如

- debug:
    msg: "{{ lookup('vars','myvar1') }} {{ lookup('vars','myvar3') }}"

在上一示例的上下文中,

结果为hello world.

results to hello world in the context of my previous example.