set facts with loops

1,275 views
Skip to first unread message

DomaNitro

unread,
Sep 16, 2014, 3:41:35 AM9/16/14
to ansible...@googlegroups.com
Hey,

I am looking to concatenate variables in an array from a with_fileglob but i am not sure that set_fact works with loops

- hosts        : all
  vars
:
    myfiles
: ["old"]
  tasks
:
   
- name: "facts"
     set_fact
:
       myfiles
: "{{ [item] | union(myfiles) }}"
     with_items
:
     
- "new1"
     
- "new2"      
     
- "new3"            
   
- name: "Debug"
     debug
:
       
var=myfiles

The output

    "myfiles": [
       
"new3",
       
"old"
   
]

Only the last item is added. any ideas ?

Michael DeHaan

unread,
Sep 16, 2014, 10:48:51 AM9/16/14
to ansible...@googlegroups.com
Just for starters, with version of ansible are you using?

I'd probably be tempted to just declare "my_files" as a list, in this particular case, it would make the playbook more readable.

I'm interested in the non-abstract use case though.



--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/93fbd2d2-3169-4a7c-aed0-9c36e7ccc4ab%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

DomaNitro

unread,
Sep 16, 2014, 11:30:11 AM9/16/14
to ansible...@googlegroups.com
I use the dev branch. Unfortunately we use chef also. i hope will be replaced soon. Till then i have to use some data bags. So i have a bunch of json file i need to put in a list and start using some of variables.

Michael DeHaan

unread,
Sep 16, 2014, 12:52:24 PM9/16/14
to ansible...@googlegroups.com
Ah ok, I'm wondering if maybe you might want to write a custom lookup plugin that is something like "with_databag" ? 

That's a little abstract, but maybe the cleanest way to model it and show you are looping over a chef databag still.

That's just a thought and may not entirely be appropriate, but would probably read cleaner as well.



On Tue, Sep 16, 2014 at 11:30 AM, DomaNitro <adham...@gmail.com> wrote:
I use the dev branch. Unfortunately we use chef also. i hope will be replaced soon. Till then i have to use some data bags. So i have a bunch of json file i need to put in a list and start using some of variables.
--
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.

DomaNitro

unread,
Sep 17, 2014, 3:10:48 AM9/17/14
to ansible...@googlegroups.com
Michael,

I might go this way. Should  be simple enough 

Thanks
Reply all
Reply to author
Forward
0 new messages