# set\_fact array in loop

**URL:** <https://forum.ansible.com/t/set-fact-array-in-loop/17870>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [March 30, 2015, 8:35am UTC](https://forum.ansible.com/t/set-fact-array-in-loop/17870 "2015-03-30T08:35:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Bitard](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@Michael\_Bitard](https://forum.ansible.com/u/Michael_Bitard)\
**Post date:** [March 30, 2015, 8:35am UTC](https://forum.ansible.com/t/set-fact-array-in-loop/17870/1 "2015-03-30T08:35:40Z")

</div>

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](http://stackoverflow.com/questions/23507589/is-it-possible-to-set-a-fact-of-an-array-in-ansible)

So I tested:

```auto
  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’:

```auto
  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:

```auto
  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

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [March 30, 2015, 2:32pm UTC](https://forum.ansible.com/t/set-fact-array-in-loop/17870/2 "2015-03-30T14:32:22Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Luigi\_Crozzoli](https://avatars.discourse-cdn.com/v4/letter/l/c77e96/32.png) [@Luigi\_Crozzoli](https://forum.ansible.com/u/Luigi_Crozzoli)\
**Post date:** [December 6, 2016, 9:19pm UTC](https://forum.ansible.com/t/set-fact-array-in-loop/17870/3 "2016-12-06T21:19:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Alexander\_H\_Laughlin](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@Alexander\_H\_Laughlin](https://forum.ansible.com/u/Alexander_H_Laughlin)\
**Post date:** [December 6, 2016, 11:07pm UTC](https://forum.ansible.com/t/set-fact-array-in-loop/17870/4 "2016-12-06T23:07:42Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Luigi\_Crozzoli](https://avatars.discourse-cdn.com/v4/letter/l/c77e96/32.png) [@Luigi\_Crozzoli](https://forum.ansible.com/u/Luigi_Crozzoli)\
**Post date:** [December 6, 2016, 11:28pm UTC](https://forum.ansible.com/t/set-fact-array-in-loop/17870/5 "2016-12-06T23:28:10Z")

</div>

Hi Alexander,

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

A common situation . That’s all

Thanks  
Ferdinand
