# Getting pieces from a list with ansible

**URL:** <https://forum.ansible.com/t/getting-pieces-from-a-list-with-ansible/20254>\
**Category:** Ansible Project\
**Created:** [January 18, 2016, 3:33pm UTC](https://forum.ansible.com/t/getting-pieces-from-a-list-with-ansible/20254 "2016-01-18T15:33:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dcodix](https://avatars.discourse-cdn.com/v4/letter/d/f6c823/32.png) [@dcodix](https://forum.ansible.com/u/dcodix)\
**Post date:** [January 18, 2016, 3:33pm UTC](https://forum.ansible.com/t/getting-pieces-from-a-list-with-ansible/20254/1 "2016-01-18T15:33:56Z")

</div>

Hi, I know I am provably misunderstanding somthing, but I am not able to find a solution to my (rather simple) problem.

What I want is quite easy. I have a list and I want to get the last (or first) X elements of it:

Example:

`

- set\_fact: mylist={{[“l10”,“j82”,“b83”,“8jk”,“j83”]|list}}
- debug: var=mylist
- set\_fact: max\_n={{1|int}}
- debug: var=max\_n
- debug: var=mylist[-{{max\_n}}:]  
`

Since here everything seems to work ok with the output:

`  
PLAY [deploy] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

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

TASK [cleanup\_s3\_folder : set\_fact] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

TASK [cleanup\_s3\_folder : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“mylist”: [  
“l10”,  
“j82”,  
“b83”,  
“8jk”,  
“j83”  
]  
}

TASK [cleanup\_s3\_folder : set\_fact] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

TASK [cleanup\_s3\_folder : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“max\_n”: “1”  
}

TASK [cleanup\_s3\_folder : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“mylist[-1:]”: [  
“j83”  
]  
}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=6 changed=0 unreachable=0 failed=0  
`

Now, this last list, containing in this case only the value “j83”, I want it on a variable to use further on, so I try with:

`

- set\_fact: elements=mylist[-{{max\_n}}:]

- debug: var=elements

- set\_fact: elements2={{ mylist[-{{max\_n}}:] }}

- debug: var=elements2  
`

And I am not getting what I want:

`  
PLAY [deploy] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

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

TASK [cleanup\_s3\_folder : set\_fact] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

TASK [cleanup\_s3\_folder : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“mylist”: [  
“l10”,  
“j82”,  
“b83”,  
“8jk”,  
“j83”  
]  
}

TASK [cleanup\_s3\_folder : set\_fact] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

TASK [cleanup\_s3\_folder : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“max\_n”: “1”  
}

TASK [cleanup\_s3\_folder : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“mylist[-1:]”: [  
“j83”  
]  
}

TASK [cleanup\_s3\_folder : set\_fact] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

TASK [cleanup\_s3\_folder : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“elements”: “mylist[-1:]”  
}

TASK [cleanup\_s3\_folder : set\_fact] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [localhost]: FAILED! =\> {“failed”: true, “msg”: “ERROR! template error while templating string: expected token ‘:’, got ‘}’”}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=8 changed=0 unreachable=0 failed=1  
`

Is there an easy way to get a VARIABLE piece of a list into a new variable?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [January 18, 2016, 6:25pm UTC](https://forum.ansible.com/t/getting-pieces-from-a-list-with-ansible/20254/2 "2016-01-18T18:25:48Z")

</div>

first thing, this is not needed:

- set\_fact: mylist={{["l10","j82","b83","8jk","j83"]|list}}

this should be enough:  
- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mylist: ["l10","j82","b83","8jk","j83"]

another issue is that moustaches do not stack! and you need moustaches  
for variables

- set\_fact: elements={{mylist[-max\_n:]}}

the first way you did it you had a string mylist, .not a variable, the  
second you were putting {{ }} inside of {{}} which does not work  
(moustaches do not stack!).

---

<div class="post-metadata">

**Author:** ![dcodix](https://avatars.discourse-cdn.com/v4/letter/d/f6c823/32.png) [@dcodix](https://forum.ansible.com/u/dcodix)\
**Post date:** [January 19, 2016, 8:17am UTC](https://forum.ansible.com/t/getting-pieces-from-a-list-with-ansible/20254/3 "2016-01-19T08:17:07Z")

</div>

Thanks Brian,

You are right about not needing the “|list”, it works perfectly without it.

But I am yet not able to assign the variable, using what you suggested I am now getting a different error:

`

- set\_fact: mylist={{[“l10”,“j82”,“b83”,“8jk”,“j83”]}}

- debug: var=mylist

- set\_fact: max\_n={{1|int}}

- debug: var=max\_n

- debug: var=mylist[-{{max\_n}}:]

- set\_fact: elements={{mylist[-max\_n:]}}

- debug: var=elements  
`

`  
[…]

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“mylist[-1:]”: [  
“j83”  
]  
}

TASK [set\_fact] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [localhost]: FAILED! =\> {“failed”: true, “msg”: “ERROR! an unexpected type error occurred. Error was bad operand type for unary -: ‘unicode’”}  
`

Thanks!

---

<div class="post-metadata">

**Author:** ![dcodix](https://avatars.discourse-cdn.com/v4/letter/d/f6c823/32.png) [@dcodix](https://forum.ansible.com/u/dcodix)\
**Post date:** [January 21, 2016, 12:28pm UTC](https://forum.ansible.com/t/getting-pieces-from-a-list-with-ansible/20254/4 "2016-01-21T12:28:51Z")

</div>

Hi again,

So, it is not possible to get a “variable” piece of a “variable” list?

What I need is to maintain a fixed number of objects in a storage, I want to have always only the “last X” objects and delete all the rest.  
Is there any other way to do so?
