# Conditional loop messes up the following task that does a with\_item

**URL:** <https://forum.ansible.com/t/conditional-loop-messes-up-the-following-task-that-does-a-with-item/19173>\
**Category:** Ansible Project\
**Created:** [September 2, 2015, 7:04pm UTC](https://forum.ansible.com/t/conditional-loop-messes-up-the-following-task-that-does-a-with-item/19173 "2015-09-02T19:04:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sam\_S](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@Sam\_S](https://forum.ansible.com/u/Sam_S)\
**Post date:** [September 2, 2015, 7:04pm UTC](https://forum.ansible.com/t/conditional-loop-messes-up-the-following-task-that-does-a-with-item/19173/1 "2015-09-02T19:04:09Z")

</div>

I store the modules I need to deploy within Consul. It consist of both Perl and PHP modules so on our web servers I only want to install the modules associated with PHP. The following playbook works as long as there are TWO matching modules. In my test case I have two keys with one of them being a key related to PHP deploys. The error I get is:

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

FATAL: all hosts have already failed – aborting

If I remove the “when” clause and only have one key within Consul, it also works. How do I get around the error if I’m using a “when” clause and all that remainds within “deploy\_list\_result” is just one item?

## PLAYBOOK

- consul\_kv: host=indeploy001 action=get key=deploylist/{{ jira\_ticket }} keys=True  
register: modules\_to\_deploy

- name: set fact  
set\_fact: deploy\_list=“{{ item | replace(“deploylist/” + jira\_ticket + “/”,‘’) }}-{{ lookup(‘consul\_kv’,item) }}”  
when: “‘{{ item | replace(‘deploylist/’ + jira\_ticket + ‘/’,’‘) }}’ in php\_modules”  
with\_items: ‘{{ modules\_to\_deploy.value }}’  
register: deploy\_list\_result

- shell: sudo rpm -Uvh {{ rpm\_repo }}/aria-{{ item }}.rpm --force --test  
with\_items: “{{ deploy\_list\_result.results | map(attribute=‘ansible\_facts.deploy\_list’) | sort }}”  
register: php\_command\_result

---

<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:** [September 2, 2015, 8:08pm UTC](https://forum.ansible.com/t/conditional-loop-messes-up-the-following-task-that-does-a-with-item/19173/2 "2015-09-02T20:08:02Z")

</div>

it would help to see the error, I'm going to guess you need a when: on  
the last task that skips the 'skipped' items., also you should not  
need to sudo in shell.

---

<div class="post-metadata">

**Author:** ![Sam\_S](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@Sam\_S](https://forum.ansible.com/u/Sam_S)\
**Post date:** [September 2, 2015, 10:17pm UTC](https://forum.ansible.com/t/conditional-loop-messes-up-the-following-task-that-does-a-with-item/19173/3 "2015-09-02T22:17:22Z")

</div>

The error is within my original msg. I was using the when to build my deploy list.
