set_fact array in loop

Hi,

I tried to construct an array with set_fact and I found this: http://stackoverflow.com/questions/23507589/is-it-possible-to-set-a-fact-of-an-array-in-ansible

So I tested:

  tasks:
    - set_fact: foo="[ 'one', 'two', 'three' ]"
    - debug: var=foo
    - set_fact: foo="{{foo}} + [ 'four' ]"
    - debug: var=foo

and it worked very well.

But when I try to add a ‘with_items’:

  tasks:
    - set_fact: foo="[ 'one' ]"
    - debug: var=foo
    - set_fact: foo="{{foo}} + [ '{{item}}' ]"
      with_items: [ "two", "three" ]
    - debug: var=foo

It doesn’t work, I get:

“foo”: [
“one”,
“three”
]

instead of:

“foo”: [
“one”,
“two”,
“three”
]

Whereas this works:

  tasks:
    - name: set foo fact to an array
      set_fact: foo="[ 'one' ]"
    - debug: var=foo
    - set_fact: foo="{{foo}} + [ 'two' ]"
    - set_fact: foo="{{foo}} + [ 'three' ]"
    - debug: var=foo

But is not suitable for my needs (It just proves that a fact can be overriden multiple times).

The behaviour of with_items is not what I expected, is something wrong with my approach?

I’m using ansible 1.8.2 on ubuntu 14.04

Regards,
Michaël

Hi Michaël, I’ve fixed this in the v2 code base, which also suffered from the same problem. We will look at potentially fixing this in any future 1.9.x releases as well.

Thanks!

Hi James,in Ansible version 1.9.2 the problem is still alive

`
[root@]# ansible --version
ansible 1.9.2
[root@]# date
Tue Dec 6 22:14:59 CET 2016
[root@]# ansible-playbook play03.yml

PLAY [localhost] **************************************************************

TASK: [set_fact foo=“[ ‘one’ ]”] **********************************************
ok: [localhost]

TASK: [debug var=foo] *********************************************************
ok: [localhost] => {
“var”: {
“foo”: [
“one”
]
}
}

TASK: [set_fact foo=“{{foo}} + [ ‘{{item}}’ ]”] *******************************
ok: [localhost] => (item=two)
ok: [localhost] => (item=three)

TASK: [debug var=foo] *********************************************************
ok: [localhost] => {
“var”: {
“foo”: [
“one”,
“three”
]
}
}

PLAY RECAP ********************************************************************

Playbook run UUID: 0edf4886-bbf9-11e6-9392-005056a3151a

`

News?

Thanks

Ferdinand

Given that this works just fine in the v2 release of Ansible:

`

(vagrant) [vagrant@atrebla:vagrant] ansible --version

ansible 2.2.0.0

config file =

configured module search path = Default w/o overrides

(vagrant) [vagrant@atrebla:vagrant] ansible-playbook loop.yml

PLAY [Test set_facts for loop.] ************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [set_fact] ****************************************************************

ok: [localhost]

TASK [debug] *******************************************************************

ok: [localhost] => {
“foo”: [

“one”

]

}

TASK [set_fact] ****************************************************************

ok: [localhost] => (item=two)

ok: [localhost] => (item=three)

TASK [debug] *******************************************************************

ok: [localhost] => {

“foo”: [

“one”,

“two”,

“three”

]

}

PLAY RECAP *********************************************************************

localhost : ok=5 changed=0 unreachable=0 failed=0

`

I’m curious to know: why not just upgrade?

Hi Alexander,

Unfortunately it’s not my choice. The upgrade policy are decided by company.

A common situation . That’s all

Thanks
Ferdinand