More fun with lists

644 views
Skip to first unread message

Ross Becker

unread,
Dec 16, 2013, 3:52:18 PM12/16/13
to ansible...@googlegroups.com
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.


Michael DeHaan

unread,
Dec 16, 2013, 3:58:53 PM12/16/13
to ansible...@googlegroups.com
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.



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Matt Martz

unread,
Dec 16, 2013, 3:59:23 PM12/16/13
to ansible...@googlegroups.com, Ross Becker
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.
    -- 
    Matt Martz
    ma...@sivel.net
    Reply all
    Reply to author
    Forward
    0 new messages