# with\_items and array concatenation?

**URL:** <https://forum.ansible.com/t/with-items-and-array-concatenation/15403>\
**Category:** Ansible Project\
**Created:** [June 4, 2014, 9:47pm UTC](https://forum.ansible.com/t/with-items-and-array-concatenation/15403 "2014-06-04T21:47:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dimon](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dimon](https://forum.ansible.com/u/Dimon)\
**Post date:** [June 4, 2014, 9:47pm UTC](https://forum.ansible.com/t/with-items-and-array-concatenation/15403/1 "2014-06-04T21:47:38Z")

</div>

Hi,

I’ve got a usecase where I’d like to iterate over several servers based on groups, so task looks like:

- name: group A & B  
shell: echo {{ item }} \>\> /tmp/log  
with\_items:
- groups[“A”]
- groups[“B”]

so what I get in /tmp/log is:

groups[‘A’]  
groups[‘B’]

Now, eliminating either group I can get:

- name: group A  
shell: echo {{ item }} \>\> /tmp/log  
with\_items: groups[“A”]

I get proper result.

I’m pretty certain I’ve seen this topic come up recently but can’t find it anymore. Any pointers (including links to other topics) are welcome.

P.S.  
for bonus points - can I do set-like concatenation - i.e. eliminate duplicates?

---

<div class="post-metadata">

**Author:** ![adam2](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@adam2](https://forum.ansible.com/u/adam2)\
**Post date:** [June 4, 2014, 9:50pm UTC](https://forum.ansible.com/t/with-items-and-array-concatenation/15403/2 "2014-06-04T21:50:30Z")

</div>

ansible uses jinga2. Look up that syntax. Then, off the top of my head, may not be exactly right, but:

groups["A"].concat(groups["B"]) | set | list

Some of that is python syntax(the contact, check jinga2 and python docs), and the |set|list is jinga2.

---

<div class="post-metadata">

**Author:** ![Dimon](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dimon](https://forum.ansible.com/u/Dimon)\
**Post date:** [June 4, 2014, 9:57pm UTC](https://forum.ansible.com/t/with-items-and-array-concatenation/15403/3 "2014-06-04T21:57:15Z")

</div>

nice try - but no dice:

fatal: [192.168.0.138] =\> with\_items expects a list or a set

FATAL: all hosts have already failed – aborting

I even played a bit and checked python syntax making it:

groups[‘A’].extend(groups[‘B’])|set

with or without last “list” filter with original syntax or with my “correction” it fails as per above. -vvvv doesn’t help much to pinpoint where the problem is.

---

<div class="post-metadata">

**Author:** ![Dimon](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dimon](https://forum.ansible.com/u/Dimon)\
**Post date:** [June 4, 2014, 10:01pm UTC](https://forum.ansible.com/t/with-items-and-array-concatenation/15403/4 "2014-06-04T22:01:50Z")

</div>

thanks to your hint I’m one step closer:

- name: group A & B  
shell: echo {{ item }} \>\> /tmp/log  
with\_items: groups[“A”] + groups[“B”]

however I still can’t figure out how to make a “set” out of the result.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [June 4, 2014, 10:02pm UTC](https://forum.ansible.com/t/with-items-and-array-concatenation/15403/5 "2014-06-04T22:02:22Z")

</div>

You might want to take a look at the ‘set’ filters provided by ansible at [http://docs.ansible.com/playbooks\_variables.html#set-theory-filters](http://docs.ansible.com/playbooks_variables.html#set-theory-filters)

I imagine something like:

with\_items: groups[‘A’]|union(groups[‘B’])|unique

would work.

I doubt it is needed, but you might have to add |list onto the end of that.

I think jinja2 addition may also work:

with\_items: (groups[‘A’] + groups[‘B’])|unique

---

<div class="post-metadata">

**Author:** ![Dimon](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dimon](https://forum.ansible.com/u/Dimon)\
**Post date:** [June 4, 2014, 10:17pm UTC](https://forum.ansible.com/t/with-items-and-array-concatenation/15403/6 "2014-06-04T22:17:04Z")

</div>

Thanks Matt and Adam!

Winning combination turned out to be:

( groups[‘A’]+groups[‘B’] ) | unique

I do like how simple it looks vs

( groups[‘A’]|union (groups[‘B’]))| unique

since in my case I have more groups to add and it is very simple with “+” and less error-prone.

---

<div class="post-metadata">

**Author:** ![C\_Morgan\_Hamill](https://avatars.discourse-cdn.com/v4/letter/c/ebca7d/32.png) [@C\_Morgan\_Hamill](https://forum.ansible.com/u/C_Morgan_Hamill)\
**Post date:** [June 5, 2014, 2:26pm UTC](https://forum.ansible.com/t/with-items-and-array-concatenation/15403/7 "2014-06-05T14:26:16Z")

</div>

Excerpts from Dmitry Makovey's message of 2014-06-04 18:17:04 -0400:

> Winning combination turned out to be:
> 
> ( groups['A']+groups['B'] ) | unique
> 
> I do like how simple it looks vs
> 
> ( groups['A']|union (groups['B']))| unique

Just FYI, the 'unique filter in the latter form is redundant; this  
should work fine:

&nbsp;&nbsp;&nbsp;&nbsp;groups['A']|union(groups['B')

Sets, by definition, have no duplicate items.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [June 6, 2014, 12:36pm UTC](https://forum.ansible.com/t/with-items-and-array-concatenation/15403/8 "2014-06-06T12:36:41Z")

</div>

The first problem here is not about sets or unions or anything.

The issue is with\_items wants a single list to walk over, not a list of two lists.

So

with\_items: variable\_ame

works

as does

with\_items:

- dog
- cat
- fish

With hard coded strings

But if you want to walk two lists, that’s “with\_nested”.

Here, though you do want to add them together.

I just wanted to mention why you were seeing “groups[“A”]” literally in the first logfile above.
