More fun with lists

So, I’ve found another hiccup with lists. I’m trying to set a value through a python “split” call, and then access the individual pieces. I’ve verified that set_fact does in fact, set a list, because I can use the result for with_items, and it correctly picks up the individual values, but syntax which works for accessing array elements for a list set as a var, does not work for accessing the array elements when set via set_fact and python split. Here’s a small playbook which demonstrates the issue:

  • hosts: configurator
    user: root
    gather_facts: False

vars:
some_list: [ “foo”, “bar”, “baz” ]
ldap_base_dn: “jrbhome.net

tasks:

  • name: splittest
    set_fact: ldap_domain_split=“{{ldap_base_dn.split(”.“)}}”

  • name: retrievetest
    debug: msg=“Item access {{ some_list[0] }}”

  • name: retrievetest
    debug: msg=“Domain part 1 {{ ldap_domain_split[0] }}”

  • name: retrieveloop
    debug: msg=“Domain part {{item}}”
    with_items: ldap_domain_split

The first debug statement works perfectly, shows ‘foo’ for the value. The with_items loop works correctly, but the value which comes back from “retrievetest” is ‘[’. I think it’s setting the fact to a string, and accessing characters of the string, rather than correctly picking up that this is an array… but it’s very odd that with_items works in that case.

Yep, I’d expect that.

Set fact is not really intended to store list facts at this time – or at least wasn’t written for them.

I think this should be filed as a feature request, or otherwise someone should take a stab at making this module do a little bit more with complex data types.

This may be a bug at some level. But looks like, from my testing, that this is fixed by using something like:

  • name: splittest
    set_fact:
    ldap_domain_split: "{{ ldap_base_dn.split(‘.’) }}”

when you do a single line set_fact:

  • name: splittest
    set_fact: ldap_domain_split=“{{ldap_base_dn.split(”.")}}”

…something doesn’t convey.