Ansible accumulating dictionaries in array fact via with_items

ansible

I have the following task (and a debug task):

- name: get list of error logs
  set_fact:
    centralised_logs: "{{ centralised_logs | default([]) | union([{'name': '{{ item }} error log'}])}}"
  with_items:
    - "one"
    - "two"
    - "three"

- debug: msg="{{ centralised_logs }}"

I'd expect the centralised_logs fact to accumulate an array of dictionaries that contain the interpolated strings in the union statement, but instead, I get the following (note that it works for the second and third items, but not the first):

TASK [nginx : get list of error logs] ******************************************
ok: [127.0.0.1] => (item=one)
ok: [127.0.0.1] => (item=two)
ok: [127.0.0.1] => (item=three)

TASK [nginx : debug] ***********************************************************
fatal: [127.0.0.1]: FAILED! => {"failed": true, "msg": "[{'name': u'two error log'}, {'name': u'three error log'}, {'name': u'{{ item }} error log'}]: 'item' is undefined"}

Why is the first value failing to be interpolated correctly?

Ansible version 2.1.0.0.

Best Answer

The problem is, that you try to use a the {{ variable }}-notation inside of another one.

A correct example would be

- name: get list of error logs
  set_fact:
    centralised_logs: "{{ centralised_logs | default([]) | union([{'name': item + ' error log'}])}}"
  with_items:
    - "one"
    - "two"
    - "three"

- debug: msg="{{ centralised_logs }}"

This produces the following output:

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": [
        {
            "name": "one error log"
        }, 
        {
            "name": "two error log"
        }, 
        {
            "name": "three error log"
        }
    ]
}