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!
El lunes, 18 de enero de 2016, 19:25:58 (UTC+1), Brian Coca escribió:
>
> first thing, this is not needed:
>
> - set_fact: mylist={{["l10","j82","b83","8jk","j83"]|list}}
>
> this should be enough:
> - set_fact:
> 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!).
>
>
> On Mon, Jan 18, 2016 at 10:33 AM, Dan C <[email protected] <javascript:>>
> wrote:
> > 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?
> >
> >
> > --
> > 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 [email protected] <javascript:>.
> > To post to this group, send email to [email protected]
> <javascript:>.
> > To view this discussion on the web visit
> >
> https://groups.google.com/d/msgid/ansible-project/69a9793b-9a30-422d-aed8-504672876d78%40googlegroups.com.
>
>
> > For more options, visit https://groups.google.com/d/optout.
>
>
>
> --
> Brian Coca
>
--
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/f542b425-996c-4a87-bd7b-dabdb4477a45%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.